当前位置: 首页 > 后端技术 > Node.js

使用Electron的托盘应用

时间:2023-04-04 00:05:44 Node.js

使用Electron将应用放入托盘本系列文章的应用示例已发布在GitHub上:electron-api-demos-Zh_CN。您可以克隆或下载它并运行它来查看。欢迎来到星空。使用托盘模块可以让您在操作系统的通知区域中创建一个图标。此图标还可以附加上下文菜单。在浏览器中查看完整的API文档。托盘支持:Win、macOS、Linux|Process:MainExample按钮使用ipc模块向主进程发送消息。在主进程中,应用程序将被告知在托盘中放置一个带有上下文菜单的图标。在此示例中,可以通过单击托盘图标上下文菜单中的“删除”或再次单击示例按钮来删除托盘图标。主进程constpath=require('path')constelectron=require('electron')constipc=electron.ipcMainconstapp=electron.appconstMenu=electron.MenuconstTray=electron.TrayletappIcon=nullipc.on('put-in-tray',function(event){consticonName=process.platform==='win32'?'windows-icon.png':'iconTemplate.png'consticonPath=path.join(__dirname,iconName)appIcon=newTray(iconPath)constcontextMenu=Menu.buildFromTemplate([{label:'removed',click:function(){event.sender.send('tray-removed')}}])appIcon.setToolTip('Electronexamplein托盘。')appIcon.setContextMenu(contextMenu)})ipc.on('remove-tray',function(){appIcon.destroy()})app.on('window-all-closed',function(){if(appIcon)appIcon.destroy()})渲染进程constipc=require('electron').ipcRendererconsttrayBtn=document.getElementById('put-in-tray')lettrayOn=falserayBtn.addEventListener('click',function(event){if(trayOn){trayOn=falsedocument.getElementById('tray-countdown').innerHTML=''ipc.send('remove-tray')}else{trayOn=trueconstmessage='再次点击示例按钮移除托盘。'document.getElementById('tray-countdown').innerHTML=messageipc.send('put-in-tray')}})//从图标上下文菜单中删除托盘ipc.on('tray-removed',function(){ipc.send('remove-tray')trayOn=falsedocument.getElementById('tray-countdown').innerHTML=''})Linux中的高级提示托盘支持。在仅支持应用程序指示器的Linux发行版上,用户需要安装libappindicator1才能使托盘图标正常工作。关于在Linux上使用托盘的更多信息有关更多详细信息,请查看完整的API文档。如果本文对您有帮助,请在下方点赞或starGitHub:electron-api-demos-Zh_CN支持,谢谢。

最新推荐
猜你喜欢