electron最小化托盘怎么设置
这篇文章主要介绍“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);
})
相关内容
这些是最新的
热门排行
- THINKPHP5+GatewayWorker+Workerman 开发在线客服系统
- 在手机浏览器网页中点击链接跳转到微信界面的方法
- 尊云网站目录系统 ThinkPHP5网站分类目录程序 v2.2.221011
- CentOS 7安装shadowsock(一键安装脚本)
- AdminTemplate 基于LayUI 2.4.5实现的网站后台管理模板
- 用NW.js(node-webkit)开发多平台的桌面客户端
- PHP生成随机昵称/用户名
- THINKPHP5网站分类目录程序 尊云网站目录系统
- 织梦(DEDECMS)微信支付接口 微信插件
- 基于LayUI开发的 网站后台管理模板 BeginnerAdmin
- 响应式后台网站模板 - AMA.ADMIN
- layuiAdmin后台管理模板 Iframe版
- LayUI 1.0.9 升级 至 LayUI 2.1.4 方法
- 简洁清爽的会员中心模板
- jQuery幸运大转盘抽奖活动代码