electron的托盘Tray

1.在主进程文件background.js中引入需要的文件

bash 复制代码
import { Tray, Menu } from "electron";
const path = require("path");

2.获取托盘图标

bash 复制代码
const baseSRC = process.cwd(); //这里不能使用__dirname,使用dirname会直接获取dist_electron中的文件,我们需要的文件在public文件中
const filePath = path.join(baseSRC, "/public/favicon.ico");

3.创建tray

bash 复制代码
let tray = null;
app.on("ready", async () => {
  tray = new Tray(filePath);
  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);

  tray.on("click", () => {
    //给托盘绑定点击事件
  });
  createWindow();
});
相关推荐
IT_陈寒16 分钟前
Vite动态导入把我坑惨了,原来要这样用才对
前端·人工智能·后端
DFT计算杂谈19 分钟前
KPROJ编译教程
java·前端·python·算法·conda
觅_22 分钟前
前端学习后端的时候 选择一个技术
前端·学习
独泪了无痕25 分钟前
CryptoJS:数据安全的JavaScript加密利器
前端·vue.js·node.js
i_am_a_div_日积月累_34 分钟前
1.创建electron项目
electron
发现一只大呆瓜1 小时前
一文搞懂 Vite 处理CommonJS包、按需编译逻辑及 Rollup 插件兼容规则
前端
Edwardwu1 小时前
写了个y-mxgraph:给 draw.io 接上了 Yjs,顺便解决了部署在 iframe 里的一堆问题
前端·typescript
其实防守也摸鱼1 小时前
软件安全与漏洞--软件安全编码
java·前端·网络·安全·网络安全·web·工具
发现一只大呆瓜2 小时前
Vite 开发预构建机制详解,搞懂 esbuild 与 Rollup 分工差异
前端·面试·vite
熊猫_豆豆2 小时前
一个模拟四轴飞行器在随机气流扰动下悬停飞行的交互式3D仿真网页,包含飞行器建模与PID控制算法
javascript·3d·html·四轴无人机模拟飞行