electron disables right-click menu in BrowserWindow

Recently, I used electron + vite + solid.js to make a small tool for real-time monitoring of network traffic, which needs to prohibit the user from BrowserWindowpopping up the default right-click menu after gaining focus.

electron net-stats

solution

Add the following code after new BrowserWindow:

  // 禁止右键菜单弹出 start
  mainWindow.hookWindowMessage &&
  mainWindow.hookWindowMessage(278, function () {
    
    
    mainWindow.setEnabled(false) //窗口禁用
    let timer = setTimeout(() => {
    
    
      mainWindow.setEnabled(true)
      clearTimeout(timer)
    }, 100) // 延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间
    return true
  })
  // 禁止右键菜单弹出 end

Tip: Because it hookWindowMessageis only available on Windows systems, you must determine mainWindow.hookWindowMessagewhether the method exists to avoid reporting errors.


Welcome to: Tianwen Blog

Guess you like

Origin blog.csdn.net/tiven_/article/details/131714032