windows - 如何从Windows中的底座或任务栏图标重新打开托盘中的 Electron 应用程序?

标签 windows electron tray

我有一个 Electron 应用程序,将Vue用作模板部分。
如果单击关闭按钮,应用程序将移至纸盘。
我需要从未发生的Dock/app-icon/task-bar图标重新启动应用程序。

无法从纸盘重新打开该应用程序,或者我们可以说它已经处于 Activity 状态。

波纹管是我的 Electron 主要工艺代码:

'use strict'

import fs from 'fs'
import path from 'path'
import { app, BrowserWindow, shell, ipcMain } from 'electron'
import { autoUpdater } from 'electron-updater'
import request from 'request'

if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:3600`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    minWidth: 800,
    width: 1600,
    height: 900,
    center: true,
    frame: false,
    backgroundColor: '#1A1C1F'
  })

  mainWindow.maximize()
  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  mainWindow.webContents.on('new-window', (e, url) => {
    e.preventDefault()
    shell.openExternal(url)
  })

  let cliArgs = process.argv
  ipcMain.on('get-cliArgs', (event, args) => {
    event.sender.send('cliArgs', cliArgs)
  })

  if (process.env.NODE_ENV !== 'production') {
    mainWindow.webContents.openDevTools()
  }
}

var shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
  // Someone tried to run a second instance, we should focus our window.
  if (mainWindow) {
    if (mainWindow.isMinimized()) mainWindow.restore()
    mainWindow.focus()
  }
})

if (shouldQuit) {
  app.quit()
  process.exit()
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  app.quit()
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})

最佳答案

您正在使用哪个命令来最小化窗口到托盘?

在我们的应用程序中,我们执行以下操作以最小化托盘并在需要时还原窗口:

用于最小化:

win.minimize();

用于创建托盘命令以恢复窗口或退出应用程序:

  tray = new Tray('<path to app icon>');
  let contextMenu = Menu.buildFromTemplate([
    {
      label: 'showApp', click:  () => {
        win.maximize();
        win.setAlwaysOnTop(true);
      }
    },
    {
      label: 'quit', click:  () => {
        app.isQuiting = true;
        app.quit();
      }
    }
  ]);
  tray.setToolTip('app name');
  tray.setContextMenu(contextMenu);
  tray.on('right-click', () => {
    tray.popUpContextMenu();
  })
  tray.on('click', () => {
    win.maximize();
    win.setAlwaysOnTop(true);
    // restore overlay icon when going back from tray
    setOverlayIcon(currentOverlayIcon);
  });

关于windows - 如何从Windows中的底座或任务栏图标重新打开托盘中的 Electron 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54684174/

相关文章:

c++ - 在控制台应用程序中与空的 Windows 消息队列作斗争

c++ - 如何使用 GetKeyState 获取按键被按下的那一刻

windows - 为 Windows 7 构建自定义凭证提供程序

javascript - 如何暂停 Node.js 应用程序直到附加调试器

node.js - 捆绑Quasar/Express/Electron App进行生产-Express服务器无法启动

java - 在java中打印机的不同托盘中打印pdf的不同页面

windows - 用于 Windows GUI 的 Perl

javascript - Svelte Electron y Metro UI CSS

python - 使用 pygtk 的简单托盘图标应用程序

c# - 托盘应用程序允许系统在注销或退出时终止我的程序