如何将电子窗口附加到应用程序屏幕并动态调整大小

IT技术 node.js reactjs electron
2021-04-27 22:03:22

我目前正在编写一个程序,它混合使用 Electron 和 React-Redux 在屏幕/应用程序顶部创建一个覆盖窗口。我成功地创建了透明覆盖窗口并列出了所有有效的媒体流。但是我不知道如何让这个新的覆盖窗口匹配所选流的大小/位置并动态调整大小。最重要的是,我希望叠加层单独位于所选流的顶部。

欢迎任何提示:)

// In MainProcess

ipcMain.on(ELECTRON_CREATE_OVERLAY_WINDOW, (event, windowSettings) => {
  if (overlayWindow !== null) {
    console.error('Trying to create an Overlay window when there is already one!')
    return
  }

  console.log('Creating the Overlay window')
  overlayWindow = new BrowserWindow({
    width: windowSettings.width,
    height: windowSettings.height,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
    },
    transparent: true,
    frame: false,
    alwaysOnTop: true,
  });

  overlayWindow.setIgnoreMouseEvents(true);
  overlayWindow.loadURL("http://localhost:3000/overlay");

  overlayWindow.on('closed', () => {
    console.log('Overlay window closed')
    overlayWindow = null
  })

});
// In React page / RendererProcess

React.useEffect(async () => {
    desktopCapturer
      .getSources({
        types: ["window", "screen"],
      })
      .then((inputSources) => {
        for (let i = 0; i < inputSources.length; i++) {
          let source = inputSources[i];
          const constraints = {
            audio: false,
            video: {
              mandatory: {
                chromeMediaSource: "desktop",
                chromeMediaSourceId: source.id,
              },
            },
          };

          navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
            inputSources[i].stream = stream;
            console.log('stream', stream)
            // When we got all streams, update the state
            if (i == inputSources.length - 1) {
              setSources([...inputSources]);
            }
          });
        }
      });
  }, []);

...

const launchOverlay = (source) => {
  const streamSettings = source.stream.getVideoTracks()[0].getSettings();
  console.log(source)
  console.log(source.stream)
  console.log(streamSettings)
  createOverlayWindow({ width: streamSettings.width, height: streamSettings.height })
};
1个回答

您可以使用电子覆盖窗口包。

自述文件说它支持 Windows 和 Linux

它按标题跟踪目标窗口,并将您的应用程序窗口保持在其正上方。如果您重新启动目标应用程序/游戏,它也会重新连接自身。唯一的缺点 - 它没有很好的记录。但是基本的演示很简单。

// ...
import { overlayWindow as OW } from 'electron-overlay-window'

// ...
const win = new BrowserWindow({
  ...OW.WINDOW_OPTS, // pay attention here
  width: 800,
  height: 600,
  resizable: false,
  webPreferences: {
    nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
  },
})

// ... when ready
OW.attachTo(window, 'Untitled - Notepad')

// listen for lifecycle events
OW.on('attach', ev => { console.log('WO: attach', ev) })
OW.on('detach', ev => { console.log('WO: detach', ev) })
OW.on('blur', ev => { console.log('WO: blur', ev)})
OW.on('focus', ev => { console.log('WO: focus', ev)})
OW.on('fullscreen', ev => console.log('WO: fullscreen', ev))
OW.on('moveresize', ev => console.log('WO: fullscreen', ev))

您可以在此处查找更多示例: