class ElectronApi {static mainWindow;//主窗口createWindow() {try {// Create the browser window.this.mainWindow = new BrowserWindow({width: 1200,height: 800,minHeight: 800,minWidth: 1200,webPreferences: {preload: preloadPath,// nodeIntegration: true,// contextIsolation: false//webSecurity: false},autoHideMenuBar: false})this.mainWindow.on('show', () => {dialog.showMessageBoxSync(this.mainWindow, {message: 'message:' + process.resourcesPath,type: 'info',buttons: ['好的', 'OK'],detail: 'details???',icon: './src/assets/img/logo.png',})})this.mainWindow.on('hide', () => {console.log('mainWindow was hidden')})this.mainWindow.on('close', () => {console.log('mainWindow will be closing')})// this.mainWindow.setMenu(null)// 在开发模式下加载 Vite 开发服务器const startUrl = process.env.NODE_ENV === 'development'? 'http://localhost:5173' // Vite dev server URL: `file://${path.join(__dirname, '../../../dist/index.html')}`;this.mainWindow.loadURL(startUrl)// 打开开发工具this.mainWindow.webContents.openDevTools()this.mainWindow.show() //必须调用 show方法,才会触发show事件。} catch (error) {logger.error('(createWindow)>>' + error.message)throw error}}/*** @description 带Cookie打开指定网址*/createPublishWindowWithCookies() {try {// Create the browser window.this.mainWindow = new BrowserWindow({width: 1200,height: 800,minHeight: 800,minWidth: 1200,webPreferences: {preload: path.resolve(__dirname, '../../../preload/preload.js'),//webSecurity: false},autoHideMenuBar: true})this.mainWindow.setMenu(null)// 在开发模式下加载 Vite 开发服务器// 在生产模式下加载编译后的静态文件console.log('process.env.NODE_ENV>>>' + process.env.NODE_ENV)const startUrl = process.env.NODE_ENV === 'development'? 'http://localhost:5173' // Vite dev server URL: `file://${path.join(__dirname, '../../../dist/index.html')}`;// 加载 index.htmlthis.mainWindow.loadURL(startUrl)// mainWindow.loadFile('./src/index.html')// 打开开发工具this.mainWindow.webContents.openDevTools()} catch (error) {logger.error('(createWindow)>>' + error.message)throw error}}}
注意这一行代码
this.mainWindow.show() //必须调用 show方法,才会触发show事件。
必须调用BrowserWindow的show方法,才会触发show事件,hidden也一样,其它事件没有测试。
记录一下。