日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Electron中实现菜单、子菜单、以及自带操作事件

發(fā)布時間:2025/3/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Electron中实现菜单、子菜单、以及自带操作事件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

場景

用HTML和CSS和JS構(gòu)建跨平臺桌面應(yīng)用程序的開源庫Electron的介紹以及搭建HelloWorld:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106413828

Electron怎樣進(jìn)行渲染進(jìn)程調(diào)試和使用瀏覽器和VSCode進(jìn)行調(diào)試:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106414541

在上面搭建好項(xiàng)目以及知道怎樣進(jìn)行調(diào)試后,那么Electron怎樣實(shí)現(xiàn)菜單項(xiàng)。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。

實(shí)現(xiàn)

首先在renderer.js中引入Menu和MenuItem

const {remote} = require('electron'); const {Menu, MenuItem} = remote;

然后為了觸發(fā)按鈕彈出的操作,在index.html中添加一個按鈕,并設(shè)置id

<button id="menuItem">彈出菜單</button>

然后在renderer.js中獲取這個按鈕并設(shè)置其點(diǎn)擊事件,在點(diǎn)擊事件中添加菜單項(xiàng)

var btnMenuItem=document.getElementById('menuItem'); btnMenuItem.onclick = PopMenu;function PopMenu() {const template = [{ label: "公眾號:" },{ label: "霸道的程序猿" , click: () => {console.log("點(diǎn)擊事件OK");}},{ role: "undo"},{ role: "redo"},{ label: "旅游", type: "checkbox", checked: true},{ label: "吃", type: "checkbox", checked: true},{ label: "逛街", type: "checkbox", checked: false},new MenuItem({label: "這是menuItem生成的菜單", click: ()=> {console.log("您點(diǎn)擊了menuItem的菜單");}}),{label: "子菜單測試",submenu: [{label: "子菜單-1"},{label: "子菜單-2"},{label: "子菜單-3"}]} ]; const menu = Menu.buildFromTemplate(template); menu.popup(); }

效果

?

上面的undo就是執(zhí)行撤銷的操作,redo就是重新執(zhí)行撤銷的操作,類似的操作還有

const template = [{label: 'Edit',submenu: [{role: 'undo'},{role: 'redo'},{type: 'separator'},{role: 'cut'},{role: 'copy'},{role: 'paste'},{role: 'pasteandmatchstyle'},{role: 'delete'},{role: 'selectall'}]},{label: 'View',submenu: [{role: 'reload'},{role: 'forcereload'},{role: 'toggledevtools'},{type: 'separator'},{role: 'resetzoom'},{role: 'zoomin'},{role: 'zoomout'},{type: 'separator'},{role: 'togglefullscreen'}]},{role: 'window',submenu: [{role: 'minimize'},{role: 'close'}]},{role: 'help',submenu: [{label: 'Learn More',click () { require('electron').shell.openExternal('https://electron.atom.io') }}]} ]if (process.platform === 'darwin') {template.unshift({label: app.getName(),submenu: [{role: 'about'},{type: 'separator'},{role: 'services', submenu: []},{type: 'separator'},{role: 'hide'},{role: 'hideothers'},{role: 'unhide'},{type: 'separator'},{role: 'quit'}]})// Edit menutemplate[1].submenu.push({type: 'separator'},{label: 'Speech',submenu: [{role: 'startspeaking'},{role: 'stopspeaking'}]})// Window menutemplate[3].submenu = [{role: 'close'},{role: 'minimize'},{role: 'zoom'},{type: 'separator'},{role: 'front'}] }const menu = Menu.buildFromTemplate(template)menu.popup();

效果

?

?

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Electron中实现菜单、子菜单、以及自带操作事件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。