61 lines
1.4 KiB
JavaScript
61 lines
1.4 KiB
JavaScript
|
const { app, BrowserWindow, Menu } = require("electron");
|
||
|
console.log(process.platform) // 平台判断
|
||
|
const createWindow = () => {
|
||
|
let mainWin = new BrowserWindow({
|
||
|
show: false,
|
||
|
width: 800,
|
||
|
height: 400,
|
||
|
title: "自定义菜单",
|
||
|
webPreferences: {
|
||
|
nodeIntegration: true,
|
||
|
enableRemoteModule: true,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
// 定义自己需要的菜单项
|
||
|
let menuTemp = [
|
||
|
{
|
||
|
label: "文件",
|
||
|
submenu: [
|
||
|
{
|
||
|
label: "打开文件",
|
||
|
click() {
|
||
|
console.log("打开文件夹")
|
||
|
}
|
||
|
},
|
||
|
{
|
||
|
type: 'separator'
|
||
|
},
|
||
|
{
|
||
|
label: "关闭文件夹"
|
||
|
},
|
||
|
{
|
||
|
label: "关于",
|
||
|
role: 'about'
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
{ label: "编辑" },
|
||
|
];
|
||
|
|
||
|
// 利用上述模板生成一个菜单项
|
||
|
let menu = Menu.buildFromTemplate(menuTemp);
|
||
|
|
||
|
// 添加到应用里
|
||
|
Menu.setApplicationMenu(menu);
|
||
|
|
||
|
mainWin.loadFile("index.html");
|
||
|
mainWin.on("ready-to-show", () => {
|
||
|
mainWin.show();
|
||
|
});
|
||
|
mainWin.on("close", () => {
|
||
|
mainWin = null;
|
||
|
});
|
||
|
};
|
||
|
|
||
|
app.on("ready", createWindow);
|
||
|
|
||
|
app.on("window-all-closed", () => {
|
||
|
app.quit();
|
||
|
});
|