电子:重新启动计算机后启动时最小化应用程序 [英] Electron: Minimize the application when launched after computer is restarted

查看:86
本文介绍了电子:重新启动计算机后启动时最小化应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node-auto-launch 在计算机启动后启动我的应用程序重新启动。此应用程序仅适用于Windows。我希望此应用程序默认在后台运行时最小化启动。我该如何实现?

  let bizAnalystAutoLauncher = new AutoLaunch({
name:'BizAnalystDesktop'
});

bizAnalystAutoLauncher.enable();
bizAnalystAutoLauncher.isEnabled()
.then(function(isEnabled:boolean){
if(isEnabled){
return;
}
bizAnalystAutoLauncher.enable ();
})
.catch(function(err:any){
//处理错误
console.log(err);
});

我不想隐藏应用程序。应用程序图标应该在任务栏的系统托盘中可见。

解决方案

因此,您需要某种最小化为



以通常的方式初始化应用程序,而不是 mainWindow.show()调用 mainWindow.minimize()初始化mainWindow之后,然后为mainWiondw的 minimize restore <添加事件监听器/ code>事件,可通过 mainWindow.setSkipTaskbar()隐藏或显示应用程序的任务栏图标:



< pre class = lang-js prettyprint-override> ...
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)
...

这篇关于电子:重新启动计算机后启动时最小化应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆