Electron 系统托盘图标

一、在入口文件electron.js中引入Tray, Menu, nativeImage

const { app, BrowserWindow, Tray, nativeImage, Menu, ipcMain } = require('electron');

二、在初始化完成后添加图片

app.whenReady().then(() => {
//上面创建窗体代码省略
let mainWindow = createWindow()
  const icon = nativeImage.createFromPath(path.join(__dirname, 'favicon.ico'))
  let tray = new Tray(icon)
  const contextMenu = Menu.buildFromTemplate([
    { label: '主界面', type: 'normal' ,click:(menuItem, browserWindow, event)=>{
      mainWindow.show()
    }},
    { label: '退出', type: 'normal' ,click:(menuItem, browserWindow, event)=>{
      app.quit();
    } },
  ])

  tray.setContextMenu(contextMenu)
  tray.setToolTip('定时休息')
  //托盘双击
  tray.on('double-click', (event) => {
    mainWindow.show()
  })
  
  //tray.setTitle('定时休息')
});

你可能感兴趣的:(electron学习,electron,前端,javascript)