有托盘时是否可以将窗户最小化?

问题描述

装有托盘的窗口(Windows OS)上是否可以使用.minimize()?现在,我想将其隐藏到Windows Dock中(或如何称呼它?),但是当我在应用程序中放入任务栏并尝试在浏览器窗口中调用.minimize()时,什么都没有发生。当然,我可以改用.hide(),但我想将其保留在底座中,而不是完全隐藏它。
我正在使用Electron 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',{ 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()

感谢您的帮助,谢谢!

解决方法

如果在应用就绪时创建了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",{ label: "Item3",type: "radio",checked: true },{ label: "Item4",type: "radio" }
  ]);
  tray.setToolTip("This is my application.");
  tray.setContextMenu(contextMenu);

  win.minimize();
});

在尝试创建浏览器窗口而不等待ready事件时,您将收到以下错误:

错误:在应用就绪之前无法创建BrowserWindow