Electron 自定义软件顶部菜单、右键菜单以及绑定快捷键

摘要:
顶部菜单main/menu.js//创建菜单/*varelectron=require;varMenu=electron.Menu;vscode里面实现代码提示功能:在当前项目安装一下electron模块。=='darwin'){app.quit();}});//macosapp.on;右键菜单render/menu.js//创建菜单/*vscode里面实现代码提示功能:在当前项目安装一下electron模块。

顶部菜单

main/menu.js

//创建菜单/*
var electron=require('electron');

var Menu=electron.Menu;

vscode里面实现代码提示功能:
    在当前项目安装一下  electron模块。

      "dependencies": {
        "electron": "^2.0.4"
    }


https://electronjs.org/docs/api/menu-item
*/
const { Menu } = require('electron');


//订单菜单
var template =[
    {

        label: '文件',
        submenu: [
            {

                label: '新建文件',

                accelerator: 'ctrl+n',

                click: function() {
                    console.log('ctrl+n');
                }
            },
            {

                label: '新建窗口',
                click: function() {

                    console.log('new window');
                }
            }
        ]
    },
    {

        label: '编辑',
        submenu: [

            {

                label: '复制',
                role: 'copy'},
            {

                label: '截切',
                role: 'cut'}
        ]
    }


]
var m =Menu.buildFromTemplate(template);

Menu.setApplicationMenu(m);

引入menu.js到主进程index.js

//主进程

//引入electron模块
var electron =require('electron');

//nodejs中的path模块
var path=require('path');

//创建electron引用     控制应用生命周期的模块
var app=electron.app;     

//创建electron BrowserWindow的引用          窗口相关的模块
var BrowserWindow=electron.BrowserWindow;

//变量 保存对应用窗口的引用

var mainWindow=null;


functioncreateWindow(){
    //创建BrowserWindow的实例 赋值给mainWindow打开窗口   
    //软件默认打开的宽度高度 {400,height:400}
    mainWindow=new BrowserWindow({800,height:600,webPreferences: {
        nodeIntegration: true}}); 

    mainWindow.loadURL(path.join('file:',__dirname,'index.html'));

    
    //开启渲染进程中的调试模式
mainWindow.webContents.openDevTools();

    console.log(path.join('file:',__dirname,'index.html'));

    mainWindow.on('closed',()=>{
        mainWindow=null;
    })    

    //执行设置菜单操作
    require('./main/menu.js');

}

app.on('ready',createWindow)


//当所有的窗口被关闭后退出应用   Quit when all windows are closed.
app.on('window-all-closed', () =>{
    //On OS X it is common for applications and their menu bar
    //to stay active until the user quits explicitly with Cmd + Q

    //对于OS X系统,应用和相应的菜单栏会一直激活直到用户通过Cmd + Q显式退出
    if (process.platform !== 'darwin') {
      app.quit();
    }
  });
  
//macos
app.on('activate', () =>{
//对于OS X系统,当dock图标被点击后会重新创建一个app窗口,并且不会有其他
    if (mainWindow === null) {
        createWindow();
    }
});

右键菜单

render/menu.js

//创建菜单/*vscode里面实现代码提示功能:
    在当前项目安装一下  electron模块。

    "dependencies": {
        "electron": "^2.0.4"
    }


https://electronjs.org/docs/api/menu-item
*/

var remote=require('electron').remote;

const Menu=remote.Menu;


//定义菜单
var template=[
    {

        label:'文件',
        submenu:[
            {

                label:'新建文件',

                accelerator:'ctrl+n',

                click:function(){ 
                    console.log('ctrl+n');
                }
            },
            {

                label:'新建窗口',
                click:function(){ 

                    console.log('new window');
                }
            }
        ]
    },
    {

        label:'编辑',
        submenu:[

            {

                label:'复制',
                role:'copy'},
            {

                label:'截切',
                role:'cut'}
        ]
    }


]



var m=Menu.buildFromTemplate(template);

Menu.setApplicationMenu(m);


//右键菜单
window.addEventListener('contextmenu',function(e){

    //alert('1212')

    //阻止当前窗口默认事件
e.preventDefault();
    //在当前窗口点击右键的时候弹出  定义的菜单模板
m.popup({window:remote.getCurrentWindow()})
},false)

引入到:index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <style>.content{

             100%;

            height: 400px;

            background: orange;


            overflow-y: auto;
        }
    </style>
  
</head>
<body>
       
    <button id="btn">打开新窗口
    </button>

    <input type="text" />

    <br>

    <p> 我是一个测试内容</p>
    
    <script src="http://t.zoukankan.com/renderer/menu.js"></script>   
我是内容

</body>
</html>

免责声明:文章转载自《Electron 自定义软件顶部菜单、右键菜单以及绑定快捷键》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇antd框架tree树动态插入,解决新版Antd无法使用TreeNodes问题ios--进程/多线程/同步任务/异步任务/串行队列/并行队列(对比分析)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

如何把Electron做成一个Runtime,让多个应用共享同一个Electron

这个问题涉及到很多知识,而且要想把这个Runtime做好很绕。 下面我就说一下我的思路:(以下内容以Windows平台为基础,Mac平台和Linux平台还得去调查一下,才能确定是否可行) 首先,我们先区分三类用户: Runtime建设者(就是我们) Runtime使用者(就是使用Runtime的开发者) 最终用户(就是使用Runtime开发者开发的应用的...

【转】使用electron开发,修改文件后如何做到自动刷新

转, 原文:https://segmentfault.com/q/1010000009419827# ------------------ 平时写WEB页面,可以配合构建工具,让页面自动刷新,或者平时写node的时候用会类似forever这样的工具自动启动,而不用手动重新启动一遍。最近在用electron就遇到这个问题了,查了不少资料也没找到解决方法。现在...

Electron 7.x以上版本安装提示错误解决方法

升级使用最新的Electron 7.x以上版本,运行提示electron不存在,其实是因为electron install时候对应平台的可执行文件没有下载成功,错误提示如下: (node:18000) UnhandledPromiseRejectionWarning: Error: EPERM: operation not permitted, lstat...

二、Electron + Webpack + Vue 搭建开发环境及打包安装

目录 Webpack + Vue 搭建开发环境及打包安装 ------- 打包渲染进程 Electron + Webpack  搭建开发环境及打包安装 ------- 打包主进程 Electron + Webpack + Vue 搭建开发环境及打包安装 ---- 打包electron应用 二、打包主线程 1. 项目目录结构   首先创建一个项目文件夹...

vue项目使用electron打包成exe

1、环境准备 系统版本 windows 10 λ node -vv12.19.0 λ vue -V@vue/cli 4.5.7 λ npm -v6.14.8  2、将electron官方打包例子克隆下来 git clone https://github.com/electron/electron-quick-start 3、测试官方例子 安装c...

Electron remote 模块、通过 BrowserWindow 打开新窗口

Electron 主进程和渲染进程中的模块 Electron remote 模块 remote 模块提供了一种在渲染进程(网页)和主进程之间进行进程间通讯(IPC)的简便途 径。 Electron 中, 与 GUI 相关的模块(如 dialog, menu 等)只存在于主进程,而不在渲染进程中 。 为了能从渲染进程中使用它们,需要用ipc模块来...