Electron: Minimize the application when launched after computer is restarted

陌路散爱 提交于 2019-12-11 08:07:39

问题


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.


回答1:


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



回答2:


The way I would do it is I would create a shortcut in the start menu Programs > startup with an argument instead of using node-auto-launch. Then when the app runs check for that argument in process.argv.

So to create a start menu shortcut with an argument of startMinimized you can use this module called windows-shortcuts

require('windows-shortcuts').create(
    '%APPDATA%/Microsoft/Windows/Start Menu/Programs/Startup/myApp.lnk', {
        target: process.execPath,
        args:   'startMinimized',
        icon:   'path/to/icon'
    }, function (err) {
        if (err) {
            throw Error(err);
        }
    }
);

Then you could write some script like this to minimize the window at startup:

var startMinimized = false;
if (process.argv[2] && process.argv[2].indexOf('startMinimized') != -1) {
    startMinimized = true;
}

var mainWindow = new BrowserWindow({show: !startMinimized});
if (startMinimized) {
    mainWindow.minimize();
}

process.argv is an array of arguments the app starts with. The first one is the .exe path. The second is the squirrel argument.



来源:https://stackoverflow.com/questions/45014473/electron-minimize-the-application-when-launched-after-computer-is-restarted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!