import { app, BrowserWindow, Tray, Menu } from "electron";
import { fileURLToPath } from "url";
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let tray = null;
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
});
win.loadURL("http://localhost:5173");
win.webContents.openDevTools();
win.on("close", (e) => {
e.preventDefault(); // 阻止退出程序
win.setSkipTaskbar(true); // 取消任务栏显示
win.hide(); // 隐藏主程序窗口
});
tray = new Tray(path.join(__dirname, "icons", "favicon.ico"));
const contextMenu = Menu.buildFromTemplate([
{
label: "退出",
click: function () {
win.destroy();
app.quit();
}
}
]);
tray.setToolTip("Vite+Vue3+Electron");
tray.setContextMenu(contextMenu);
tray.on("click", () => {
win.show();
});
};
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
标签:__,窗口,win,app,Electron,import,后台,const,tray
From: https://www.cnblogs.com/Himmelbleu/p/18448552