练习代码git:https://github.com/SmileEricXin/electronPractice.git
//src\main\index.js 做以下修改
// 新增调用
import * as tray from './tray'
// create main BrowserWindow when electron is ready
// 修改ready事件
app.on('ready', () => {
mainWindow = createMainWindow()
// create tray
tray.creatTray()
})
// 增加函数
export function getWin () {
return mainWindow
}
// 新建文件 src\main\tray\index.js
import { Menu, Tray, app, dialog } from 'electron'
import path from 'path'
import { getWin } from '../index'
let tray = null
// 激活窗口
function activeMainWin () {
let win = getWin()
if ( win ) {
if (win.isVisible()) {
win.focus()
}
}
}
// 退出销毁对象
app.on('quit', () => {
tray && tray.destroy()
tray = null
})