我知道如何使用ipcRenderer.send()ipcMain.on()将事件从渲染器进程发送到主进程。我还可以使用event.sender.send()将回复发送回渲染器进程,但是陷入了如何将事件从主进程发送到所有渲染器进程的麻烦,更像是广播。

最佳答案

您可以创建对BrowserWindow实例的引用数组,当需要全局事件时,可以使用sender函数将其映射,例如:

let windows = [];

let backgroundComputation = new BrowserWindow(options);
let webInteractions = new BrowserWindow(different_options);
let imageProcessing = new BrowserWindow(another_options);

windows.push(backgroundComputation)
windows.push(webInteractions)
windows.push(imageProcessing)

let sender = (message, windows) =>
        windows.map((ref) => ref.webContents.send('event_name', message))

如果您有一整堆它们,这可能会很方便。您还可以将顶部窗口中的alwaysOnTop:true选项中的flag设置为true,以便其他任何窗口都位于其下方。希望这可以帮助!

09-30 22:22
查看更多