最小化到系统托盘

const {
  app,
  BrowserWindow,
  globalShortcut,
  webContents,
  Tray,
  Menu,
} = require("electron");

let win = null;

const createWindow = () => {
  win = new BrowserWindow({
    width: 1600,
    height: 900,
    // transparent: true, // 让窗口透明
    // frame: false, // 无边框
    webPreferences: {
      scrollBounce: false, // 禁用滚动条的反弹效果
      // nodeIntegration: true, // 允许在渲染进程中使用Node.js
    },
    icon: "./ico/logo.png",
  });

  win.loadFile("index.html");

  win.on("minimize", function (event) {
    event.preventDefault();
    win.hide();
    createTray();
  });
};
function createTray() {
  tray = new Tray("./ico/logo.ico"); // 使用你的应用图标

  const contextMenu = Menu.buildFromTemplate([
    {
      label: "Show",
      click: function () {
        win.show();
        tray.destroy();
      },
    },
    {
      label: "Quit",
      click: function () {
        app.quit();
      },
    },
  ]);
  tray.on("double-click", () => {
    win.show();
    tray.destroy();
  });
  tray.setToolTip("PowerPAL");
  tray.setContextMenu(contextMenu);
}

posted @ 2024-12-25 10:05  暖暖De幸福  阅读(42)  评论(0)    收藏  举报