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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2025/3/11 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 electron 菜单栏_如何在Electron JS中添加任务栏图标菜单? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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,

在本練習中,

  • I will create a tray icon first,

    我將首先創建一個任務欄圖標,

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

    創建一個常數,將菜單項保存在一個數組中,

  • 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 UIlet mainWindow; let tray app.on('ready', _ => {tray = new Tray (path.join ('src', 'Tray.PNG' ) ) // sets tray icon imageconst 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 dimensionsheight : 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中添加任务栏图标菜单?的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。