目的及效果:
点击窗口右上角的叉叉的时候,弹出下面这个,点击确认则程序结束,取消则提示框关闭,程序继续
,
环境:electron version: 11.0.4main.js代码 var electron = require('electron')app.on('ready', () => { mainWindow = new BrowserWindow({ width: 1500, height: 800, minHeight: 800, minWidth: 1500, // fullscreen: true, webPreferences: { nodeIntegration: true, enableRemoteModule: true, webviewTag: true, }, }) mainWindow.loadFile('index.html') //mainWindow要关闭时的方法↓ mainWindow.on('close', e => { e.preventDefault(); //先阻止一下默认行为,不然直接关了,提示框只会闪一下 electron.dialog.showMessageBox({ type: 'info', title: '提示', message:'确认退出?', buttons: ['确认', '取消'], //选择按钮,点击确认则下面的idx为0,取消为1 cancelId: 1, //这个的值是如果直接把提示框×掉返回的值,这里设置成和“取消”按钮一样的值,下面的idx也会是1 }).then(idx => { //注意上面↑是用的then,网上好多是直接把方法做为showMessageBox的第二个参数,我的测试下不成功 console.log(idx) if (idx.response == 1) { console.log('index==1,取消关闭') e.preventDefault(); } else { console.log('index==0,关闭') mainWindow = null app.exit(); } }) });})