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();
});
相关推荐
荣达1 天前
koa洋葱模型理解
前端·后端·node.js
xiaoyan20151 天前
Electron38-Winchat聊天系统|vite7+electron38+vue3电脑端聊天Exe
vue.js·electron·vite
reembarkation1 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation1 天前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf
李明卫杭州1 天前
JavaScript中的dispatchEvent方法详解
javascript
KenXu1 天前
F2C-PTD工具将需求快速转换为代码实践
前端
给月亮点灯|1 天前
Vue3基础知识-setup()、ref()和reactive()
前端·javascript·vue.js
芜青1 天前
【Vue2手录12】单文件组件SFC
前端·javascript·vue.js
冷冷的菜哥1 天前
react实现无缝轮播组件
前端·react.js·typescript·前端框架·无缝轮播
hrrrrb1 天前
【Python】字符串
java·前端·python