This commit is contained in:
chenqiang 2025-09-09 22:27:03 +08:00
parent ee3ee77e31
commit 4b82de5374
2 changed files with 99 additions and 120 deletions

View File

@ -43,20 +43,27 @@ let mainWindow = null;
async function createWindow() { async function createWindow() {
console.log('开始创建应用窗口...'); console.log('开始创建应用窗口...');
try {
// 创建浏览器窗口 // 创建浏览器窗口
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
// 确保在Windows 7上能正常显示
show: false, show: false,
// 在Windows 7上使用更兼容的配置
webPreferences: { webPreferences: {
// 使用Node.js 12兼容的路径解析方式 // 使用绝对路径确保preload.js能被找到
preload: path.join(__dirname, '../src/preload.js'), preload: path.resolve(__dirname, '../src/preload.js'),
nodeIntegration: false, nodeIntegration: false,
contextIsolation: true, contextIsolation: true,
// 为Windows 7添加额外的兼容性配置
enableRemoteModule: false,
sandbox: false,
webSecurity: false
} }
}); });
console.log('Preload path:', path.resolve(__dirname, '../src/preload.js'));
// 设置隐藏菜单栏 // 设置隐藏菜单栏
mainWindow.setMenu(null); mainWindow.setMenu(null);
@ -143,6 +150,11 @@ async function createWindow() {
// 生产模式加载本地HTML文件 // 生产模式加载本地HTML文件
mainWindow.loadURL("app://./index.html"); mainWindow.loadURL("app://./index.html");
} }
} catch (windowError) {
console.error('创建窗口失败:', windowError);
// 显示错误对话框
dialog.showErrorBox('创建窗口失败', `无法创建应用窗口: ${windowError.message}`);
}
} }
// Quit when all windows are closed. // Quit when all windows are closed.

View File

@ -12,40 +12,7 @@ module.exports = {
pluginOptions: { pluginOptions: {
electronBuilder: { electronBuilder: {
// 基础配置 mainProcessFile: 'background/main.js',
nodeIntegration: false,
contextIsolation: true,
preload: "src/preload.js",
mainProcessFile: "background/main.js",
lintPreloadFiles: false,
externals: ["fontkit", "pdfkit"],
// 主进程webpack配置
chainWebpackMainProcess: (config) => {
config.module
.rule("babel")
.test(/\.js$/)
.use("babel-loader")
.loader("babel-loader")
.options({
presets: ["@babel/preset-env"],
plugins: [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-class-properties",
],
})
.end();
// 添加对mjs文件的处理 - 兼容Electron 11
config.module
.rule("mjs")
.test(/\.mjs$/)
.include.add(/node_modules/)
.end()
.type("javascript/auto");
},
// 所有构建选项都放在这里
builderOptions: { builderOptions: {
// 应用基本信息 // 应用基本信息
appId: 'com.example.exam11', appId: 'com.example.exam11',