58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
|
const { app, BrowserWindow, ipcMain } = require('electron')
|
||
|
|
||
|
// 定义全局变量存放主窗口 Id
|
||
|
let mainWinId = null
|
||
|
|
||
|
const createWindow = () => {
|
||
|
let mainWin = new BrowserWindow({
|
||
|
show: false,
|
||
|
width: 800,
|
||
|
height: 400,
|
||
|
webPreferences: {
|
||
|
nodeIntegration: true,
|
||
|
enableRemoteModule: true
|
||
|
}
|
||
|
})
|
||
|
|
||
|
mainWin.loadFile('index.html')
|
||
|
mainWinId = mainWin.id
|
||
|
mainWin.on('ready-to-show', () => {
|
||
|
mainWin.show()
|
||
|
})
|
||
|
mainWin.on('close', () => {
|
||
|
mainWin = null
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// 接受其他进程发送的数据,完成后续的逻辑
|
||
|
ipcMain.on('openWin2', (_, data) => {
|
||
|
// 接收到渲染进程中按钮点击信息之后完成窗口2的打开
|
||
|
let subWin1 = new BrowserWindow({
|
||
|
width: 400,
|
||
|
height: 300,
|
||
|
parent: BrowserWindow.fromId(mainWinId), // 或定义全局变量
|
||
|
webPreferences: {
|
||
|
nodeIntegration: true,
|
||
|
enableRemoteModule: true
|
||
|
}
|
||
|
})
|
||
|
subWin1.loadFile('subWin1.html')
|
||
|
subWin1.on('close', () => {
|
||
|
subWin1 = null
|
||
|
})
|
||
|
// 转交给进程
|
||
|
subWin1.webContents.on('did-finish-load', () => {
|
||
|
subWin1.webContents.send('its', data)
|
||
|
})
|
||
|
})
|
||
|
|
||
|
ipcMain.on('stm', (ev, data) => {
|
||
|
// 转交给进程
|
||
|
let mainWin = BrowserWindow.fromId(mainWinId)
|
||
|
mainWin.webContents.send('mti', data)
|
||
|
})
|
||
|
|
||
|
app.on('ready', createWindow)
|
||
|
app.on('window-all-closed', () => {
|
||
|
app.quit()
|
||
|
})
|