electron鼠标经过托盘显示自定义菜单

1.自定义菜单

复制代码
  // 自定义菜单
  var templateMenu = [{
      label: "首页",
      submenu: [{ label: "111" }, { label: '9089' }]
  }, , {
      label: "编辑",
      submenu: [{ label: '123' }]
  }]
  testmenu = Menu.buildFromTemplate(templateMenu);
复制代码

2. 在鼠标经过托盘事件中展示自定义菜单

复制代码
//isLeave 是自定义是否鼠标离开字段,默认为true,   tray是托盘
tray.on('mouse-move', () => {
      if (isLeave) {
          //触发mouse-enter
          isLeave = false;
          checkTrayLeave();//检查是否离开
          testmenu.popup();//打开
      }
  })
复制代码

 3.检查方法

复制代码
//检查是否离开
function checkTrayLeave(){
    clearInterval(leaveInter)
    leaveInter = setInterval(function(){
        trayBounds = tray.getBounds();
        point = screen.getCursorScreenPoint();
        if(!(trayBounds.x < point.x && trayBounds.y < point.y && point.x < (trayBounds.x + trayBounds.width) && point.y < (trayBounds.y  + trayBounds.height))){//触发mouse-leave
            clearInterval(leaveInter);
            isLeave = true;
            testmenu.closePopup();
        }
    }, 100)
}
复制代码

 

posted @   zhang_you_wu  阅读(231)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示