溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

electron最小化托盤怎么設(shè)置

發(fā)布時(shí)間:2023-04-18 09:25:36 來源:億速云 閱讀:113 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“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)。

向AI問一下細(xì)節(jié)

免責(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)容。

AI