electron最小化托盘怎么设置

其他教程   发布日期:2025年03月02日   浏览次数:154

这篇文章主要介绍“electron最小化托盘怎么设置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“electron最小化托盘怎么设置”文章能帮助大家解决问题。

注意

  • icon地址一定要正确,否则托盘出不来,要报错

  • icon地址需要绝对路径

报错:

Error: Failed to load image from path './assets/json.png'

官网示列代码:

  1. const { app, Menu, Tray } = require('electron')
  2. let tray = null
  3. app.whenReady().then(() => {
  4. tray = new Tray('/path/to/my/icon')
  5. const contextMenu = Menu.buildFromTemplate([
  6. { label: 'Item1', type: 'radio' },
  7. { label: 'Item2', type: 'radio' },
  8. { label: 'Item3', type: 'radio', checked: true },
  9. { label: 'Item4', type: 'radio' }
  10. ])
  11. tray.setToolTip('This is my application.')
  12. tray.setContextMenu(contextMenu)
  13. })

修改后的托盘

我在ready周期中对托盘进行设置,大家可以在网上去下载一些图标,我是在iconfont网站去下载的,尺寸选择的是16;感觉刚刚好。

  • 启动服务器是在服务器执行以后显示屏幕

  • 退出登录是直接关闭应用

  • 当用户点击图标的时候展示应用

这儿需要注意一个点:图标路径不能直接写死需要通过path引入;

static指的是 public 文件下的static(将图标放置到该文件夹即可)

  1. app.on('ready', async () => {
  2. if (isDevelopment && !process.env.IS_TEST) {
  3. // Install Vue Devtools
  4. try {
  5. // await installExtension(VUEJS_DEVTOOLS)
  6. session.defaultSession.loadExtension(path.resolve(__dirname, "../devTools/chrome"));
  7. } catch (e) {
  8. console.error('Vue Devtools failed to install:', e.toString())
  9. }
  10. }
  11. createWindow();
  12. tray = new Tray(path.join(__static, './static/json.png'))
  13. const contextMenu = Menu.buildFromTemplate([
  14. {
  15. label: '启动服务器',
  16. icon: path.join(__static, './static/start.png'),
  17. click:()=>{
  18. win.webContents.send('start-server');
  19. win.show();
  20. }
  21. },
  22. {
  23. label: '退出登录',
  24. icon: path.join(__static, './static/quit.png'),
  25. click:()=>{
  26. win.close();
  27. }
  28. },
  29. ])
  30. // 点击图标展示
  31. tray.on('click',() => {
  32. win.show();
  33. });
  34. // 鼠标放置上去显示的文本
  35. tray.setToolTip('PDF管理工具');
  36. tray.setContextMenu(contextMenu);
  37. })

以上就是electron最小化托盘怎么设置的详细内容,更多关于electron最小化托盘怎么设置的资料请关注九品源码其它相关文章!