关闭确认

This commit is contained in:
chenqiang 2025-09-09 02:35:44 +08:00
parent be857ff36a
commit c9f6062258

View File

@ -57,6 +57,48 @@ async function createWindow() {
// 然后显示窗口
mainWindow.show();
// 添加窗口关闭事件监听
mainWindow.on('close', (event) => {
console.log('检测到窗口关闭事件');
// 阻止默认的关闭行为
event.preventDefault();
console.log('已阻止默认关闭行为');
// 使用同步方式显示对话框Electron 11.5支持的方式)
try {
// 直接显示对话框并获取结果
const response = dialog.showMessageBoxSync(mainWindow, {
type: 'warning',
title: '确认关闭',
message: '确认要退出软件吗?退出后,未完成的考试将不会被保存。',
buttons: ['取消', '确认关闭'],
defaultId: 0,
cancelId: 0
});
console.log('用户选择了response:', response);
// 检查用户选择的按钮索引
if (response === 1) {
console.log('用户确认关闭,准备退出应用');
// 移除所有事件监听器以防止任何干扰
mainWindow.removeAllListeners();
// 强制关闭窗口
mainWindow.destroy();
// 然后退出应用
setTimeout(() => {
console.log('强制退出应用');
app.quit();
}, 100);
} else {
console.log('用户取消关闭');
}
} catch (error) {
console.error('显示对话框时出错:', error);
}
});
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await mainWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
@ -85,6 +127,9 @@ app.on('activate', () => {
// 添加退出确认逻辑
app.on('before-quit', (event) => {
console.log('检测到应用退出事件');
// 注释掉这部分代码因为我们已经在窗口的close事件中处理了确认逻辑
/*
// 如果是MacOS系统让系统默认处理
if (process.platform === 'darwin') {
return
@ -102,7 +147,7 @@ app.on('before-quit', (event) => {
dialog.showMessageBox(mainWindow, {
type: 'warning',
title: '确认关闭',
message: '系统关闭后,已进行的试题将不会被保存,是否确认要关闭?',
message: '确认要退出软件吗?退出后,未完成的考试将不会被保存。',
buttons: ['取消', '确认关闭'],
defaultId: 0,
cancelId: 0
@ -114,6 +159,7 @@ app.on('before-quit', (event) => {
}).catch((err) => {
console.error('显示退出确认对话框失败:', err)
})
*/
})
// This method will be called when Electron has finished