【问题标题】:How do I remove the menu bar from a specific window in electron?如何从电子的特定窗口中删除菜单栏?
【发布时间】:2019-11-03 21:19:06
【问题描述】:

我的应用程序中有一个菜单,当您单击文档属性时会弹出另一个窗口,但应用程序菜单也被此窗口继承,因此您可以从文档属性窗口打开文档属性窗口。我只想禁用文档属性窗口的菜单,我能够实现这一点的唯一方法是使窗口无框,但我仍然希望显示标题栏,所以这不是我正在寻找的解决方案为。

我尝试过使用 docProps.removeMenu()、docProps.setMenu(null),甚至 docProps.setApplicationMenu(null)。我已经移动了它,尝试将 docProps 设为全局变量,但没有任何效果。

这是我的代码:

//Create references for modules that require electron
const { app, BrowserWindow, Menu } = require('electron')

//Create a global reference for the main window
let mainWindow

function createWindow () {
  //Create the browser window
  mainWindow = new BrowserWindow({
    minWidth: 300,
    minHeight: 300,
    backgroundColor: '#888888'
  })

  //Load the index.html file
  mainWindow.loadFile('index.html')

  //Reload the main window on resize
  mainWindow.on('resize', function () {
    mainWindow.reload()
  })
}

function createAppMenu () {
  //Create application menu template
  const template = [
    {
      label: 'File',
      submenu: [
        {
          label: 'Document Properties...',
          click: function () {
            docProps = new BrowserWindow({
              width: 250,
              height: 300,
              resizable: false,
              title: 'Document Properties'
            })
            //This isn't working and I'm not sure why
            docProps.removeMenu()
          }
        }
      ]
    },
    {
      label: 'Edit'
    },
    {
      label: 'View'
    },
    {
      label: 'Window'
    },
    {
      label: 'Help'
    }
  ]

  //Build app menu from template
  const menu = Menu.buildFromTemplate(template)
  Menu.setApplicationMenu(menu)
}

//Call the createWindow function once electron has finished initializing
app.on('ready', function () {
  createWindow()
  mainWindow.maximize()
  createAppMenu()
})

您可以在https://github.com/Leglaine/ElectroText查看整个项目

我得到的唯一错误消息是当我尝试调用 docProps.setApplicationMenu(null) 时,它说 setApplicationMenu 不能在 docProps 上调用,但我真的没想到它会起作用。提前感谢您的帮助!

【问题讨论】:

    标签: javascript html css electron


    【解决方案1】:

    当您已经通过Menu.setApplicationMenu() 设置应用程序菜单时,win.removeMenu()win.setMenu(null) 目前似乎在 Electron 中被破坏了

    尝试像这样设置一个空菜单

    docProps.setMenu(Menu.buildFromTemplate([]))
    

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 2020-01-22
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      相关资源
      最近更新 更多