electron 菜单栏

If you are new here, please consider checking out my recent articles on Electron JS including Tray Icons.

如果您是新来的,请考虑查看我最近关于Electron JS的文章, 包括托盘图标 。

In this tutorial, we will set up 2 menu items for a tray icon which when a user right-clicks, the menu appears just like the image below.

在本教程中,我们将为托盘图标设置2个菜单项,当用户右键单击时,菜单如下图所示。

Adding a menu to a system tray icon is the functionality of a tray method known as the tray.setContextMenu(), where the parameter is simply a variable or constant of the menu items passed in Menu.buildFromTemplates([]).

将菜单添加到系统任务栏图标是称为tray.setContextMenu()的任务栏方法的功能,其中参数只是在Menu.buildFromTemplates([])中传递的菜单项的变量或常量。

In this exercise,

在本练习中,

  1. I will create a tray icon first,

    我将首先创建一个任务栏图标,

  2. Create a constant which holds the menu items in an array,

    创建一个常数,将菜单项保存在一个数组中,

  3. And finally, pass it as a parameter to the tray method setContextMenu().

    最后,将其作为参数传递给任务栏方法setContextMenu() 。

We are also going to use the path module which is a Node.Js built-in module to locate the source of our tray icon image and the menu module where the method Menu.buildFromTemplates([]) is derived from.

我们还将使用path模块(它是Node.Js的内置模块)来定位托盘图标图像的源以及菜单模块,该菜单模块是方法Menu.buildFromTemplates([])的来源。

Finally, let us write some code: Open your main JavaScript file and type,

最后,让我们编写一些代码:打开您的主JavaScript文件并键入,

//system tray icon menu//
const electron = require ('electron')  // imports electron
const path = require ('path') // imports path module
const {app, Menu, Tray} = electron // imports menu and tray modules
const BrowserWindow = electron.BrowserWindow //enables UI
let mainWindow;
let tray
app.on('ready', _ => {tray = new Tray (path.join ('src', 'Tray.PNG' ) ) // sets tray icon image
const contextMenu = Menu.buildFromTemplate([   // define menu items
{label: 'Help',
click: () => console.log ('Help') // click event
},
{label: 'System',
click: () => console.log ('System')
}
])
tray.setContextMenu(contextMenu)
mainWindow = new BrowserWindow({ // sets browser window dimensions
height : 600,
width : 600,
})
})

Finally, run your code and enjoy the output.

最后,运行您的代码并享受输出。

Output:

输出:

Hey! The browser window displays too... I just did not take a screenshot because it is blank.

嘿! 浏览器窗口也显示...我只是没有截图,因为它是空白的。

Thanks for reading.

谢谢阅读。

Drop your comments if in need of help.

如果需要帮助,请删除您的评论。

翻译自: https://www.includehelp.com/electron-js/add-tray-icon-menu-in-electron-js.aspx

electron 菜单栏

electron 菜单栏_如何在Electron JS中添加任务栏图标菜单?相关推荐

  1. java pdf添加透明水印_如何在PDF文件中添加透明水印

    原标题:如何在PDF文件中添加透明水印 有些文件添加水印,但是又不想水印影响文件的使用有时候会设置透明水印,那么PDF怎么设置透明水印呢,应该有很多的小伙伴们都很好奇应该怎么做吧,接下来就为大家分享一 ...

  2. electron快捷键_如何在Electron JS应用程序中添加键盘快捷键?

    electron快捷键 Just like in any other native desktop application, keyboard shortcuts save time and make ...

  3. wps启用编辑按钮在哪里_如何在wps工具栏中添加按钮 如何在Excel中添加删除命令按钮...

    延伸:如何在Excel中添加删除命令按钮 描述:在Excel 2003中,很多常用的命令按钮都放置在工具栏中,用户可以很方便地进行操作.添加命令主要是通过拖动的方式完成的,其具体的操作如下:骤一,在视 ...

  4. win10图标重建缓存_如何在Windows 10中重建损坏的图标缓存

    win10图标重建缓存 The icons that Windows uses for your documents and programs are saved in an icon cache, ...

  5. python tkinter 滚动条_如何在tkinter窗口中添加滚动条?

    我有一个tkinter程序:import urllib.request from tkinter import * root = Tk() root.iconbitmap(default='icon. ...

  6. rails 添加外键_如何在Rails后端中添加功能强大的搜索引擎

    rails 添加外键 by Domenico Angilletta 通过多梅尼科·安吉列塔(Domenico Angilletta) In my experience as a Ruby on Rai ...

  7. excel表格不够怎么添加_如何在Excel表格中添加特殊符号?借用快捷键,一步到位...

    新年假期也即将到来,相信很多小伙伴们也都会回家过年.不过在这之前,我们还是需要做好手头上的工作的,对一些办公技巧还不熟练的朋友,今天小编给大家整理的了一些关于在Excel表格中添加特殊符号的小技巧,希 ...

  8. java生成xml 换行符_如何在XML文件中添加换行符(换行符)?

    甲换行符(又名行中断或结束时的线,EOL)是标记文本行的末尾特殊字符或字符序列.所使用的确切代码因操作系统而异:LF:    Unix CR:    Mac OS up to version 9 CR ...

  9. threejs 三面体_如何在Three.js中构造空心圆柱体

    I'm having difficulties constructing a hollow cylinder in Three.js. Should I go and construct it usi ...

最新文章

  1. IPMI从驱动到应用(中篇 )
  2. JS 获取 鼠标 坐标
  3. leetcode majority number
  4. 如何查看服务器并发请求连接数
  5. asp.net core 发布到 docker 容器时文件体积过大及服务端口的配置疑问
  6. Fix chrome 下flash crash的问题
  7. OJ1035: 分段函数求值
  8. 2021牛客寒假算法基础集训营1,签到题ABFIJ
  9. Acwing第 38 场周赛
  10. 虚拟机安装苹果系统(Mac OSX 10.15包含所有资源)
  11. 1. HikariCP连接池优化配置小结
  12. Ubuntu14.04安装wineqq国际版
  13. NoClassDefFoundError: Could not initialize class org.apache.kafka.common.record.Compressio
  14. 利用VMWare和软路由多播实现校园网带宽叠加
  15. 电话聊天狂人 (34 分)
  16. 使用Keras基于RCNN类模型的卫星/遥感地图图像语义分割
  17. 【JavaWEB】项目实战-黑马面面
  18. JimuReport积木报表1.1.09 版本发布,免费的企业级 Web 报表工具
  19. 要点回顾|2020PGConf.Asia大会精彩落幕,一文带您要点回顾
  20. 音频对游戏玩家体验的影响有多大?

热门文章

  1. go get 的不再src目录中_GO语言基础进阶教程:包的使用
  2. #中队列的数据结构_数据结构与算法拓展(一)
  3. oracle备份脚本
  4. websocket心跳检测前后端架构
  5. Angular gitlab持续集成之runner配置
  6. PC介绍之PCIE、总线、内存、电源
  7. mycat 1.6.5 for mysql 8分表攻略
  8. 调用打开另外一个APK
  9. codeMirror配置
  10. TODO:macOS上ThinkPHP5和Semantic-UI集成