Reputation: 497
I am using an Electron frameless window with a custom titlebar, but there is a context menu when right clicking on it. How can I disable showing this menu?
this is electron version:
"electron": "^11.3.0",
and this is electron start program:main.js,but system-context-menu not working here after app start.
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
show: false,
frame: false,
useContentSize: true,
hasShadow: true,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
preload: path.join(__dirname, 'preload.js'),
},
});
mainWindow.loadURL(
process.env.NODE_ENV === 'development'
? 'http://localhost:5000'
: url.format({
pathname: path.join(__dirname, '../build/index.html'),
protocol: 'file:',
slashes: true,
})
);
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools();
}
mainWindow.on('system-context-menu', (event, _point) => {
event.preventDefault();
});
}
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// just work in macOS
// https://www.electronjs.org/docs/api/app#%E4%BA%8B%E4%BB%B6-activate-macos
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
}
Upvotes: 0
Views: 3292
Reputation: 1
Base on Jour answer Do following codes it works on my side,key points are two ( 0x0116 and make buildFormTemplate as null),of course, Meanwhile, you wanna have your own right_click menu, you can follow electron doc to develop,
const WM_INITMENU = 0x0116
const right_menu = Menu.buildFromTemplate([])
mainWindow.hookWindowMessage(WM_INITMENU, () => {
mainWindow.setEnabled(false)
mainWindow.setEnabled(true)
])
Sample about own menu
const WM_INITMENU = 0x0116
const right_menu = Menu.buildFromTemplate([{"label":"About",...,function(){}},{"label":"Exit",...,function(){}}])
mainWindow.hookWindowMessage(WM_INITMENU, () => {
mainWindow.setEnabled(false)
mainWindow.setEnabled(true)
right_menu.popup()
])
Upvotes: 0
Reputation: 23
Electron still not fix this bug, I'm so tired that Electron have lots of bug on windows .
and it works , try it :
const WM_INITMENU = 0x0116;
const menu = Menu.buildFromTemplate([]);
win.hookWindowMessage(WM_INITMENU, () => {
win.setEnabled(false);
win.setEnabled(true);
menu.popup();
});
https://github.com/electron/electron/issues/24893#issuecomment-1109262719
Upvotes: 2
Reputation: 10199
On Windows, you should be able to intercept the system-context-menu
event of the BrowserWindow
and cancel it.
mainWindow.on("system-context-menu", (event, _point) => {
event.preventDefault();
});
This behavior was added in Electron 11 in this PR.
Edit: this event actually doesn't fire for frameless windows, because of this Electron bug. It will need to be fixed before my solution works. I'm not aware of any workaround to this.
Upvotes: 2