I'm using CefSharp.WinForms.ChromiumWebBrowser
v45 in my project. When I right click into the web browser, the default context menu will show up:
But I don't want to show anything. What should I do?
This is the implementation for lazy people like me. It is based on CefSharp v53.0.0
public class CustomMenuHandler : CefSharp.IContextMenuHandler { public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) { model.Clear(); } public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags) { return false; } public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame) { } public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback) { return false; } }
How to use it
ChromiumWebBrowser browser = new ChromiumWebBrowser(); browser.MenuHandler = new CustomMenuHandler();
The simplest way for you is set event PreviewMouseRightButtonUp and PreviewMouseRightButtonDown with the same function have e.Handle = true. This solution will not show context menu of cefsharp when you right click.
XAML:
<wpf:ChromiumWebBrowser Grid.Row="1" x:Name="Browser" Margin="30,0" IsBrowserInitializedChanged="Browser_IsBrowserInitializedChanged" PreviewMouseRightButtonDown="Browser_PreviewMouseRightButton" PreviewMouseRightButtonUp="Browser_PreviewMouseRightButton"/>
And the function:
private void Browser_PreviewMouseRightButton(object sender, MouseButtonEventArgs e) { e.Handled = true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With