这篇文章主要介绍“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);
})
以上就是electron最小化托盘怎么设置的详细内容,更多关于electron最小化托盘怎么设置的资料请关注九品源码其它相关文章!