欢迎访问WDPHP素材源码!今天是2024年04月30日 星期二,下午好!别打盹哦!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 其他教程 > 

electron最小化托盘怎么设置
栏目分类:其他教程    发布日期:2023-09-29    浏览次数:287次     收藏

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

注意

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

  • icon地址需要绝对路径

报错:

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

官网示列代码:

const { app, Menu, Tray } = require('electron')

let tray = null
app.whenReady().then(() => {
  tray = new Tray('/path/to/my/icon')
  const contextMenu = Menu.buildFromTemplate([
    { label: 'Item1', type: 'radio' },
    { label: 'Item2', type: 'radio' },
    { label: 'Item3', type: 'radio', checked: true },
    { label: 'Item4', type: 'radio' }
  ])
  tray.setToolTip('This is my application.')
  tray.setContextMenu(contextMenu)
})

修改后的托盘

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

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

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

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

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

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

app.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    // Install Vue Devtools
    try {
      // await installExtension(VUEJS_DEVTOOLS)
      session.defaultSession.loadExtension(path.resolve(__dirname, "../devTools/chrome"));
    } catch (e) {
      console.error('Vue Devtools failed to install:', e.toString())
    }
  }
  createWindow();

  tray = new Tray(path.join(__static, './static/json.png'))
  const contextMenu = Menu.buildFromTemplate([
    { 
      label: '启动服务器', 
      icon: path.join(__static, './static/start.png'),
      click:()=>{
        win.webContents.send('start-server');
        win.show();
      }
    },
    { 
      label: '退出登录', 
      icon: path.join(__static, './static/quit.png'),
      click:()=>{
        win.close();
      }
    },
  ])
  // 点击图标展示
  tray.on('click',() => {
    win.show();
  });
  // 鼠标放置上去显示的文本
  tray.setToolTip('PDF管理工具');
  tray.setContextMenu(contextMenu);
})
源码 模板 特效 素材 资源 教程 站长