更新时间:2023-01-30 08:36:10
因此,您需要某种最小化为
以通常的方式初始化应用程序,而不是 mainWindow.show()
调用 mainWindow.minimize()
初始化mainWindow之后,然后为mainWiondw的 minimize
和 restore 事件,可通过
mainWindow.setSkipTaskbar()
隐藏或显示应用程序的任务栏图标:
...
mainWindow.on('restore',()=> {
mainWindow.setSkipTaskbar(false)
}}
mainWindow.on('minimize',()=> {
mainWindow.setSkipTaskbar(true)
})
...
添加纸盘菜单,如文档,但m确保添加菜单项以还原应用程序窗口,否则最终将得到一个在最小化后无法访问的应用程序:
...
const trayMenu = Menu.buildFromTemplate([
{
label:'Show',
click:()= > {
mainWindow.restore()
}
},
{
标签:'Quit',
角色:'quit'
}
])
tray.setContextMenu(trayMenu)
...
I am using node-auto-launch to launch my application after computer is restarted. This application is only for windows. I want this application by default to be launched minimized as it works in the background. HOw can I achieve this?
let bizAnalystAutoLauncher = new AutoLaunch({
name: 'BizAnalystDesktop'
});
bizAnalystAutoLauncher.enable();
bizAnalystAutoLauncher.isEnabled()
.then(function (isEnabled: boolean) {
if (isEnabled) {
return;
}
bizAnalystAutoLauncher.enable();
})
.catch(function (err: any) {
// handle error
console.log(err);
});
I don't want the application to be hidden. The application icon should be visible in the system tray in the taskbar.
So you want to have some kind of "minimize to tray" behaviour.
Initialize your app the usual way but instead of mainWindow.show()
you call mainWindow.minimize()
after initializing the mainWindow, then add EventListeners for the mainWiondw's minimize
and restore
events to hide or show the taskbar icon for your app via mainWindow.setSkipTaskbar()
:
...
mainWindow.on('restore', () => {
mainWindow.setSkipTaskbar(false)
})
mainWindow.on('minimize', () => {
mainWindow.setSkipTaskbar(true)
})
...
Add a Tray menu like in the documentation but make sure you add a menu item to restore the app window, otherwise you will end up with an app that is not accessible after it is minimized:
...
const trayMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: () => {
mainWindow.restore()
}
},
{
label: 'Quit',
role: 'quit'
}
])
tray.setContextMenu(trayMenu)
...