1.打开开发者工具,窗口拖动就会有问题
2.窗口关闭之前请置空Tray.setTitle(title) 中的值,否则就会弹出退出异常
// 引入electron并创建一个Browserwindow
const { app, BrowserWindow, globalShortcut, Tray, Menu,ipcMain,dialog} = require('electron')
const path = require('path')
const url = require('url')
const child_process = require('child_process')
const pathName = path.join(__dirname, 'demo/javascript/src/ScreenCapture.app')
const niuniupath=path.join(__dirname, './ScreenCapture.app')
// 保持window对象的全局引用,避免JavaScript对象被垃圾回收时,窗口被自动关闭.
let mainWindow
let tray = null;
//var count = 0,timer=null;
//var count1 = 0,timer1=null;
//设置快捷键
app.on('ready', function () {
globalShortcut.register('ctrl+alt+o', function () {
mainWindow.webContents.send('main-process-messages', 'StartCapture')
});
// var t1=setInterval(function(){
// if(!mainWindow.isMinimized()){
// clearInterval(timer);
// }
// })
});
function createWindow() {
//创建浏览器窗口,宽高自定义
mainWindow = new BrowserWindow({ width: 1024, height: 700, minWidth: 712, minHeight: 505, maximizable: true, icon: 'icon.png', backgroundColor: '#ffffff', frame: false,show:false,movable:true,skipTaskbar:true
})
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
//加载应用----- electron-quick-start中默认的加载入口
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
const exec = child_process.exec
exec(`open+${niuniupath}`)
// 打开开发者工具,默认不打开
//mainWindow.webContents.openDevTools()
// 关闭window时触发下列事件.
mainWindow.on('closed', function (e) {
e.preventDefault()
//// dialog.showErrorBox('一条信息', '关闭完了')
mainWindow = null;
mainWindow.destroy();
mainWindow.setSkipTaskbar(false)
//mainWindow.webContents.send('main-outlogin')
})
//关闭
mainWindow.on('close', (event) => {
mainWindow.hide();
event.preventDefault();
//dialog.showErrorBox('一条信息', '关闭')
mainWindow.webContents.send('main-outlogin')
mainWindow.setSkipTaskbar(false)
mainWindow.destroy();
mainWindow.hide()
});
//显示
mainWindow.on('show', () => {
///tray.setHighlightMode('always');
// dialog.showErrorBox('一条信息', "展示")
})
mainWindow.on('restore', () => {
////////dialog.showErrorBox('一条信息', '从小道大'
})
//隐藏
mainWindow.on('hide', () => {
////tray.setHighlightMode('always')
//dialog.showErrorBox('一条信息', "隐藏")
})
//托盘
tray = new Tray(path.join(__dirname, './tray.png'))
//处理mac系统下快捷键问题
// const contextMenu = Menu.buildFromTemplate([
// {
// label:["Edit"],
// submenu: [
// { label: "Undo", accelerator: "CmdOrCtrl+Z", role: "undo" },
// { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", role: "redo" },
// { label: "Cut", accelerator: "CmdOrCtrl+X", role: "cut" },
// { label: "Copy", accelerator: "CmdOrCtrl+C", role: "copy" },
// { label: "Paste", accelerator: "CmdOrCtrl+V", role: "paste" },
// { label:"Select All", accelerator: "CmdOrCtrl+A", role: "selectAll" }
// ]
// }
// ])
// Menu.setApplicationMenu(contextMenu)
ipcMain.on("open-error-dialog", function(event,num) {
////dialog.showErrorBox('一条信息', '都读完了')
if (num*1>0) {
if(num*1>99){
app.dock.setBadge("99+");
tray.setTitle(`99+`);
}else{
app.dock.setBadge(num);
tray.setTitle(`${num}`);
}
} else {
app.dock.setBadge("");
tray.setTitle('');
}
})
ipcMain.on("open-jietu", function(event) {
//dialog.showErrorBox('一条信息', '截图完成')
mainWindow.focus();
mainWindow.show();
})
ipcMain.on("out-login", function(event) {
//dialog.showErrorBox('一条信息', '截图完成')
app.dock.setBadge("");
tray.setTitle('');
})
if (process.platform === "darwin") {
let contents = mainWindow.webContents;
globalShortcut.register("CommandOrControl+C", () => {
contents.copy();
});
globalShortcut.register("CommandOrControl+V", () => {
contents.paste();
});
}
}
//单例进程
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
//app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// 当运行第二个实例时,将会聚焦到mainWindow这个窗口
if (mainWindow) {
mainWindow.show();
if (mainWindow.isMinimized()) {
mainWindow.restore();
mainWindow.focus();
}
}
});
}
// 当 Electron 完成初始化并准备创建浏览器窗口时调用此方法
app.on('ready', createWindow)
// 所有窗口关闭时退出应用.
app.on('window-all-closed', function () {
/// dialog.showErrorBox('一条信息', '所有窗口都关闭')
// macOS中除非用户按下 `Cmd + Q` 显式退出,否则应用与菜单栏始终处于活动状态.
///if (process.platform !== 'darwin') {
///app.quit();
///}
})
app.on('before-quit', e => {
// 只在 win 系统下销毁托盘图标
///dialog.showErrorBox('一条信息', '开始关闭')
///if (process.platform === 'win32') {
tray.setTitle('');
//tray.hide();
app.dock.hide()
//}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
// 这是mac打包appdmg appdmg.json ~/Desktop/xxx.dmg
appdmg.json
{
"title":"xxx",
"icon":"icon.icns",
"background":"icon.png",
"icon-size":96,
"window":{
"position":{
"x":200,"y":300
},
"size":{
"width":500,"height":300
}
},
"contents":[
{ "x":400, "y":128, "type":"link", "path" :"/Applications"},
{ "x":150 , "y":128,"type": "file","path":"./webApp/xxx-darwin-x64/xxx.app"},
{ "x":1000 , "y":7000,"type": "position","name":"icon","path":"icon.icns"},
{ "x":1000 , "y":7000,"type": "position","name":"background","path":"icon.png"}
]
}
mac踩坑之路
最新推荐文章于 2025-05-06 10:56:25 发布