Electron系统托盘使用
前面步骤已省略
主要更改主程序main.js:
const {app, BrowserWindow, Menu, ipcMain,Tray} = require('electron') const { autoUpdater } = require('electron-updater') const path = require('path') const url = require('url') // 创建全局变量并在下面引用,避免被GC let win; function createWindow () { // 创建浏览器窗口并设置宽高 win = new BrowserWindow({ width: 460, height: 780, webPreferences: { nodeIntegration:true, contextIsolation: false, preload: path.join(__dirname, 'preload.js'), preload: path.join(__dirname, 'updata.js'),//前端js }, }) //创建托盘--并且指定图标 const tray = new Tray('logo.png') //设置托盘标题 tray.setToolTip('酉玖OA'); //创建托盘菜单 const menu= Menu.buildFromTemplate([ { label:'显示主界面', click:()=>{ //显示主窗口 win.show(); } }, { label:'退出', click:()=>{ app.quit(); } } ]) //绑定托盘菜单 tray.setContextMenu(menu); // 点击托盘图标,显示主窗口 tray.on("click", () => { win.show(); }) //双击显示主窗口 tray.on('double-click', () => { win.show(); }) //Menu.setApplicationMenu(null) // 加载页面 // win.loadFile('./index.html') win.loadURL(url.format({ pathname: path.join(__dirname, 'h5/index.html'), protocol: 'file:', slashes: true, })) // 打开开发者工具 // win.webContents.openDevTools() // 添加window关闭触发事件 win.on('closed', () => { win = null // 取消引用 }) } // 初始化后 调用函数 app.on('ready', createWindow) // 当全部窗口关闭时退出。 app.on('window-all-closed', () => { // 在 macOS 上,除非用户用 Cmd + Q 确定地退出, // 否则绝大部分应用及其菜单栏会保持激活。 if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', () => { // 在macOS上,当单击dock图标并且没有其他窗口打开时, // 通常在应用程序中重新创建一个窗口。 if (win === null) { createWindow() } })

浙公网安备 33010602011771号