const { app, BrowserWindow ,ipcMain, Menu } = require('electron') const path = require('path') var mainWindow = null var pageconfig = [ { title:'登录', path:'./login.html', config:{ width:300, height:440, // 是否可以调界面大小 // resizable: false, webPreferences: { nodeIntegration: true, nodeIntegrationInWorker: true, contextIsolation: false } } }, { title:'首页', path:'./index.html', config:{ width:800, height:600, webPreferences: { nodeIntegration: true, nodeIntegrationInWorker: true, contextIsolation: false } } } ] var pageIndex = 0 const createWindow = () => { mainWindow = new BrowserWindow(pageconfig[pageIndex].config) mainWindow.loadFile(pageconfig[pageIndex].path) //取消默认菜单 Menu.setApplicationMenu(null) //开发者工具打开 mainWindow.webContents.openDevTools() } ipcMain.on("openIndex", (event, arg) => { console.log("主进程收到的消息:", arg); pageIndex = arg; mainWindow.close(); createWindow() }) ipcMain.on("startShake", (event, arg) => { var position = mainWindow.getPosition(); var newPosition = [ position[0] - 30,position[0] + 60, position[0] - 30,position[0] + 60, position[0] - 30,position[0] + 60, position[0] - 30,position[0] + 60, position[0] - 30,position[0] + 60, position[0] - 30,position[0] + 60, position[0] - 30 ] for (let i = 0; i < newPosition.length; i++) { setTimeout(() => { mainWindow.setPosition( newPosition[i], position[1] ) }, 30 * i) } newPosition.forEach((v, i) => { mainWindow.setPosition( v, position[1] ) }) }) app.whenReady().then(() => { createWindow() app.on('activate', () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) // 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })