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 BrowserWindow
popping up the default right-click menu after gaining focus.
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 hookWindowMessage
is only available on Windows systems, you must determine mainWindow.hookWindowMessage
whether the method exists to avoid reporting errors.
Welcome to: Tianwen Blog