electron主进程监听关闭弹出对话框

const createWindow = () => {
	// Create the browser window.
	const mainWindow = new BrowserWindow({
		width: 800,
		height: 600,
		icon: './src/static/image/maoniu-logo.png',
		webPreferences: {
			preload: path.join(__dirname, 'preload.js'),
			// 集成 node
			nodeIntegration: true,
			// 关闭上下文隔离,兼容 require 引入
			contextIsolation: false,
		}
	});
	// and load the index.html of the app.
	mainWindow.loadFile(path.join(__dirname, 'index.html'));
	// 启用remote模块
	remote.enable(mainWindow.webContents)
	require('./main/ipcMain.js')
	require('./main/menu.js')
	mainWindow.on('close', (event) => {
	const choice = 	dialog.showMessageBoxSync({
			type: "question",
			message: "确定退出吗?",
			buttons: ['确定', '取消', '最小化到任务栏'],
			detail: 'It does not really matter',
			defaultId: 2,
			cancelId: 1,
		})
		// 定义取消关闭的值
		const isCancel = (choice === 1)
		// 最小化到任务栏
		if(choice===2){
			mainWindow.hide()
			event.preventDefault()//取消事件的默认动作
		}
		// 取消关闭
		if (isCancel) {
			event.preventDefault()//取消事件的默认动作
		}
		if (choice===0) {
			 
            
		}

	});
	
	// Open the DevTools.
	// mainWindow.webContents.openDevTools();

};

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注