首页 > 其他分享 >electron加载远程和本地右键菜单冲突问题处理

electron加载远程和本地右键菜单冲突问题处理

时间:2023-04-14 11:13:08浏览次数:40  
标签:function 菜单 window electron 右键 myrightmenu

electron 加载远程页面,远程页面有自己的右键,但是electron也有自己的菜单,为了防止两个右键菜单同时显示,使用window.myrightmenu 进行限制,window.myrightmenu非空则显示远程的,window.myrightmenu为空则显示electron的默认菜单。 下面是远程界面代码:

    // 右键
    rightClick (row, column, event) {
      window.myrightmenu=function(){}

   ...
}


   // 列表右击事件     async TableRightFn (EvnName) {        window.myrightmenu=null;    ... }       // 鼠标移出 右键按钮     modifyReportMouseLeave (refName) {       window.myrightmenu=null      ...     }
 

 

electron 中监测 window.myrightmenu ,window.myrightmenu==null || window.myrightmenu==undefined 则会插入默认的右键菜单,否则不会显示。 以下为electron中的代码:    
window.document.addEventListener('contextmenu', function (e) {
     // 右键事件触发
     console.log('window.myrightmenu->', window.myrightmenu);
     if (window.myrightmenu == null || window.myrightmenu == undefined) {
         e.preventDefault();
         var m = e.path[0].type === "text" || e.path[0].tagName.toLowerCase() === "textarea" || e.path[0].isContentEditable;
         data = {
             msg: getSelection(),
             input: m
         };
         ipcRenderer.send('contextMenu', data);
     }
 })


 window.document.addEventListener('mousedown', function (e) {
    console.log('mousedown', e.button)
     if (e.button == 0) {
         window.myrightmenu = null
     } else if (e.button == 1) {
        window.myrightmenu = null
         //alert('你按下了鼠标滚轮!');
     } else {
         //alert('您按下了鼠标右键!')
     }
     //e.preventDefault();
 })

 

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

 function getContextMenu () {
  //new一个菜单
//   // 监听contextmenu,实现自定义右键菜单
//   window.addEventListener('contextmenu', function (e) {
    // 监听contextmenu,实现自定义右键菜单
  window.addEventListener('contextmenu', function (e) {
    e.preventDefault()
    let menu = new Menu()
    //添加菜单功能, label: 菜单名称, accelerator:快捷键,click:点击方法
    menu.append(new MenuItem({ label: '复制', accelerator: 'CommandOrControl+C', click: copyText }))
    //添加菜单分割线
    menu.append(new MenuItem({ type: 'separator' }))
    //添加菜单功能
    menu.append(new MenuItem({ label: '粘贴', accelerator: 'CommandOrControl+V', click: printText }))
    menu.popup(remote.getCurrentWindow())
  }, false)
  // 写入剪贴板方法
  function copyText () {
    const str = getSelection() // 获取选中内容
    clipboard.writeText(str) // 写入剪贴板
  }
  // 获取剪贴版内容写入当前焦点元素中
  function printText(){
    if (document.activeElement) {
      const str = clipboard.readText() // 获取剪贴板内容
      var ev = new Event('input', { bubbles: true});
      ev.simulated = true;
      document.activeElement.value = str; // 写入焦点元素
      document.activeElement.dispatchEvent(ev);
      // clipboard.clear() // 清空剪贴板,一般都不做清空,爱粘贴就粘贴,干嘛要粘贴完就清空呢
    }
  }
// 获取选中内容
function getSelection () {
    var text = ''
    if (window.getSelection) { // 除IE9以下 之外的浏览器
      text = window.getSelection().toString()
    } else if (document.selection && document.selection.type !== 'Control') { //IE9以下,可不考虑
      text = document.selection.createRange().text
    }
    if (text) {
      return text
    }
  }

}


module.exports={ getContextMenu }

 

标签:function,菜单,window,electron,右键,myrightmenu
From: https://www.cnblogs.com/lecone/p/17317691.html

相关文章

  • Electron
    用前端开发桌面程序资料Electron深入填坑之路https://blog.csdn.net/weixin_33897722/article/details/91476254 跨越平台桌面应用开发框架electron使用的心路历程https://blog.csdn.net/wang839305939/article/details/80741100他让我摆脱了不同浏览器之间的差异和版本......
  • 六、WPF 动态设置菜单,非MVVM模式
     随手建了WPF窗口<Windowx:Class="WpfAppTest.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://sch......
  • electron+antdesignpro+react
    如果你想在Electron应用中使用AntDesignPro和React框架,可以按照以下步骤进行:创建一个基于React的AntDesignPro项目。将该项目作为渲染进程代码集成到Electron中。根据需要添加Electron的主进程代码。在第二步中,有两种方法将React和AntDesignPro项目......
  • ScreenSaverNow for Mac(菜单栏防休眠工具) 1.75激活版
    想要一款菜单栏屏幕工具?推荐您使用ScreenSaverNowMac中文版,安装后可自定义的全局热键或鼠标单击立即激活您的屏幕保护程序。当您离开Mac时但仍需要保持工作状态时,ScreenSaverNowforMac将屏幕保护程序与密码一起使用很可能是保护您的信息免受他人侵害的唯一方法。ScreenSaverNow......
  • win11更改桌面菜单
    改成win10按“win键+X”,在弹出的快捷菜单中,选择“Windows终端(管理员)”:regadd"HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32"/f/ve重启一下“Windows资源管理器”恢复win11按“win键+X”,在弹出的快捷菜单中,选择“W......
  • 挂件模式用挂载菜单的方式使用上手视频教程
    挂件模式用挂载菜单的方式使用上手视频教程jjj......
  • 左边可拖出菜单(页面)效果
    这个效果现在很多软件都实现了,其实说穿了很简单的。就是一个动画,做的地道点的加一个手势拖拉效果。我写的代码(未参考任何代码,不知道别人怎么实现的。):importjava.util.ArrayList;importandroid.content.Context;importandroid.os.Bundle;importandroid.util.Log;import......
  • 微信公众号开发--获取网页授权并自定义菜单点击获得openid跳转
    之前做了个自定义菜单的click事件,但是订阅号升级为服务号,相应的有了网页的基础授权(需要认证的服务号)其中使用到了自定义菜单接口:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html网页授权获取用户基本信息接口:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0......
  • 6 菜单
    WM_COMMAND点击菜单,点击加速键,点击子窗口按钮,点击工具栏按钮。这些时候都有command消息产生。其中MSG种消息MSG消息intwmId=LOWORD(wParam);//低16位位菜单IDMSGtypedefstructtagMSG{HWNDhwnd;//句柄UINTmessage;WPARAMwPara......
  • 基本下拉菜单
    基本下拉菜单当鼠标移动到指定元素上时,会出现下拉菜单。实例<style>.dropdown{position:relative;display:inline-block;}.dropdown-content{display:none;position:absolute;margin:0px;padding:0px;color:gray;">#f9f9f9;min-width:160px;box-shad......