【问题标题】:Is it possible to minimize window when it has tray?有托盘时是否可以最小化窗口?
【发布时间】:2020-09-18 03:21:09
【问题描述】:

如果有托盘,是否可以在窗口(Windows 操作系统)上使用.minimize()?现在我想将它隐藏到 Windows Dock 中(或者它是如何调用的?),但是当我在应用程序中有托盘并试图在浏览器窗口上调用 .minimize() 时,什么也没有发生。当然,我可以改用 .hide(),但我想将其保留在 Dock 中,而不是完全隐藏它。
我正在使用电子 10.1.1。

例如:

  // First we have to create the window
  const { BrowserWindow, Tray } = require('electron')

  const win = new BrowserWindow({ width: 800, height: 600 });
  win.loadURL('https://github.com');

  // Then we have to create tray
  const 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)

  // Here we expect application will be hidden into windows dock, but nothing happens.
  win.minimize()

将不胜感激任何帮助,谢谢!

【问题讨论】:

  • 你写const = new Tray(..)。当然,这会产生错误:Uncaught SyntaxError: Unexpected token '='。没有它,它运行良好,并且窗口在启动后立即最小化。
  • @snwflk 最好有语法错误。 :) 当我做了一个例子时,这只是我的错误。真正的源代码没有语法错误。

标签: electron


【解决方案1】:

如果在应用准备就绪时创建 BrowserWindow,您的代码运行良好。

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

let win, tray;

app.on("ready", function() {

  win = new BrowserWindow({ width: 800, height: 600 });
  win.loadURL("https://github.com");

  const trayIcon = path.join(__dirname, "icon.png");
  tray = new Tray(trayIcon);

  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);

  win.minimize();
});

在不等待ready 事件的情况下尝试创建 BrowserWindow 时,您应该收到以下错误:

错误:在应用准备好之前无法创建 BrowserWindow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多