electron menu click params 主菜单点击事件回调参数

本文详细介绍了Electron应用中菜单点击事件的使用方法及回调参数的作用,包括事件对象、当前窗口对象和web内容对象的具体用途。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

electron menu click params menu主菜单点击事件回调参数

#在官方的menu click事件教程中,我没有找到回调参数的用法,遂作此文以便查阅(未经本人许可请勿转载)。
#click: function (event, focusedWindow, focusedWebContents)

event:事件对象
focusedWindow:当前打开的窗口对象
focusedWebContents:web内容对象

参数值样本
// event
MenuItem {
  label: '鏂扮獥鍙?,
  accelerator: 'CommandOrControl+n',
  click: [Function (anonymous)],
  submenu: null,
  type: 'normal',
  role: null,
  icon: null,
  sublabel: '',
  toolTip: '',
  enabled: true,
  visible: true,
  checked: false,
  acceleratorWorksWhenHidden: true,
  registerAccelerator: true,
  commandId: 32,
  userAccelerator: [Getter],
  menu: EventEmitter {
    insertItem: [Function: insertItem],
    insertCheckItem: [Function: insertCheckItem],
    insertRadioItem: [Function: insertRadioItem],
    insertSeparator: [Function: insertSeparator],
    insertSubMenu: [Function: insertSubMenu],
    setIcon: [Function: setIcon],
    setSublabel: [Function: setSublabel],
    setToolTip: [Function: setToolTip],
    setRole: [Function: setRole],
    clear: [Function: clear],
    getIndexOfCommandId: [Function: getIndexOfCommandId],
    getItemCount: [Function: getItemCount],
    getCommandIdAt: [Function: getCommandIdAt],
    getLabelAt: [Function: getLabelAt],
    getSublabelAt: [Function: getSublabelAt],
    getToolTipAt: [Function: getToolTipAt],
    isItemCheckedAt: [Function: isItemCheckedAt],
    isEnabledAt: [Function: isEnabledAt],
    worksWhenHiddenAt: [Function: worksWhenHiddenAt],
    isVisibleAt: [Function: isVisibleAt],
    popupAt: [Function: popupAt],
    closePopupAt: [Function: closePopupAt],
    _getAcceleratorTextAt: [Function: _getAcceleratorTextAt],
    commandsMap: { '32': [Circular *1], '33': [MenuItem], '34': [MenuItem] },
    groupsMap: {},
    items: [ [Circular *1], [MenuItem], [MenuItem] ]
  }
} 
// focusedWindow
BrowserWindow {
  setBounds: [Function (anonymous)],
  _events: [Object: null prototype] {
    blur: [Function (anonymous)],
    focus: [Function (anonymous)],
    show: [Function: visibilityChanged],
    hide: [Function: visibilityChanged],
    minimize: [Function: visibilityChanged],
    maximize: [Function: visibilityChanged],
    restore: [Function: visibilityChanged]
  },
  _eventsCount: 7,
  devToolsWebContents: [Getter]
} 
//focusedWebContents
{
  shiftKey: false,
  ctrlKey: true,
  altKey: false,
  metaKey: false,
  triggeredByAccelerator: true
}

样例

/*
 * @Description:
 * @Version: 1.0
 * @Autor: Tj
 */
const bytenode = require("bytenode");
const { app, Menu, BrowserView, screen } = require("electron");
const {
  createWindow,
  historyUrl,
  homePageUrl,
} = require("../dist-js/main.jsc");
let width = screen.getPrimaryDisplay().workAreaSize.width;
let height = screen.getPrimaryDisplay().workAreaSize.height;
//顶部菜单
var template = [
  {
    label: "文件",
    submenu: [
      // {
      //   label: "新建文件",

      //   accelerator: "ctrl+n",

      //   click: function () {
      //     console.log("ctrl+n");
      //     view.webContents.loadURL(homePageUrl);
      //   },
      // },
      {
        label: "新窗口",
        accelerator: "CommandOrControl+n",
        click: function (event, focusedWindow, focusedWebContents) {
          console.log("new window");
          console.log(event, focusedWindow, focusedWebContents);
          createWindow();
        },
      },
      {
        type: "separator",
      },
      {
        label: "关闭",
        role: "close",
      },
    ],
  },
  {
    type: "separator",
  },
  {
    label: "首页",
    click: function (event, focusedWindow, focusedWebContents) {
      console.log("Go home");
      console.log(historyUrl);
      openUrl(focusedWindow, homePageUrl);
    },
  },
  {
    label: "后退",
    click: function (event, focusedWindow, focusedWebContents) {
      historyCount = historyUrl.length;
      console.log("Back Page: " + historyCount);
      if (historyCount > 0 && historyCount - 2 >= 0) {
        openUrl(focusedWindow, historyUrl[historyCount - 2]);
        historyUrl.pop();
        historyUrl.pop();
      }
    },
  },

  {
    label: "刷新",
    role: "reload",
  },
  {
    label: "强制刷新",
    role: "forceReload",
  },
  {
    label: "编辑",
    submenu: [
      {
        label: "全选",
        role: "selectAll",
      },
      {
        type: "separator",
      },
      {
        label: "复制",
        role: "copy",
      },
      {
        label: "剪切",
        role: "cut",
      },
      {
        type: "separator",
      },
      {
        label: "粘贴",
        role: "paste",
      },
    ],
  },
  {
    label: "调试",
    submenu: [
      {
        label: "打开",
        accelerator: "F12",
        click: function (event, focusedWindow, focusedWebContents) {
          focusedWindow.openDevTools();
        },
      },
      {
        label: "关闭",
        accelerator: "CommandOrControl+F12",
        click: function (event, focusedWindow, focusedWebContents) {
          focusedWindow.closeDevTools();
        },
      },
    ],
  },
];

var m = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(m);

function openUrl(win, url) {
  win.loadURL(url);
}
function openViewUrl(win, url) {
  const view = new BrowserView({});
  win.setBrowserView(view);
  console.log(win);
  view.setBounds({ x: 0, y: 0, width: width, height: height });
  view.setAutoResize({
    width: true,
    height: true,
    horizontal: true,
    vertical: true,
  });
  view.setBackgroundColor("#FF6666");
  view.webContents.loadURL(url);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值