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也一樣,其它事件沒有測試。
記錄一下。