您好,登錄后才能下訂單哦!
這篇文章主要介紹“electron最小化托盤怎么設(shè)置”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“electron最小化托盤怎么設(shè)置”文章能幫助大家解決問題。
注意
icon地址一定要正確,否則托盤出不來,要報(bào)錯(cuò)
icon地址需要絕對路徑
報(bào)錯(cuò):
Error: Failed to load image from path './assets/json.png'
官網(wǎng)示列代碼:
const { app, Menu, Tray } = require('electron') let tray = null app.whenReady().then(() => { tray = new Tray('/path/to/my/icon') const contextMenu = Menu.buildFromTemplate([ { label: 'Item1', type: 'radio' }, { label: 'Item2', type: 'radio' }, { label: 'Item3', type: 'radio', checked: true }, { label: 'Item4', type: 'radio' } ]) tray.setToolTip('This is my application.') tray.setContextMenu(contextMenu) })
我在ready周期中對托盤進(jìn)行設(shè)置,大家可以在網(wǎng)上去下載一些圖標(biāo),我是在iconfont網(wǎng)站去下載的,尺寸選擇的是16;感覺剛剛好。
啟動(dòng)服務(wù)器是在服務(wù)器執(zhí)行以后顯示屏幕
退出登錄是直接關(guān)閉應(yīng)用
當(dāng)用戶點(diǎn)擊圖標(biāo)的時(shí)候展示應(yīng)用
這兒需要注意一個(gè)點(diǎn):圖標(biāo)路徑不能直接寫死需要通過path引入;
static指的是 public 文件下的static(將圖標(biāo)放置到該文件夾即可)
app.on('ready', async () => { if (isDevelopment && !process.env.IS_TEST) { // Install Vue Devtools try { // await installExtension(VUEJS_DEVTOOLS) session.defaultSession.loadExtension(path.resolve(__dirname, "../devTools/chrome")); } catch (e) { console.error('Vue Devtools failed to install:', e.toString()) } } createWindow(); tray = new Tray(path.join(__static, './static/json.png')) const contextMenu = Menu.buildFromTemplate([ { label: '啟動(dòng)服務(wù)器', icon: path.join(__static, './static/start.png'), click:()=>{ win.webContents.send('start-server'); win.show(); } }, { label: '退出登錄', icon: path.join(__static, './static/quit.png'), click:()=>{ win.close(); } }, ]) // 點(diǎn)擊圖標(biāo)展示 tray.on('click',() => { win.show(); }); // 鼠標(biāo)放置上去顯示的文本 tray.setToolTip('PDF管理工具'); tray.setContextMenu(contextMenu); })
關(guān)于“electron最小化托盤怎么設(shè)置”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。