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:
If you implement IContextMenuHandler
you can control the ContextMenu
. The two links below demo what's required (and some other useful features).
https://github.com/cefsharp/CefSharp/blob/935d3900ba2147f4786386596b62339087ff61b0/CefSharp.WinForms.Example/Handlers/MenuHandler.cs#L15
https://github.com/cefsharp/CefSharp/blob/c18f951a97a515df112d67775c767d4222f88c23/CefSharp.WinForms.Example/BrowserTabUserControl.cs#L31
In general the CefSharp.WinForms.Example
project demos quite a few features, check it out if you require other features.
webBrowser.PreviewMouseRightButtonDown += HandleWebBrowserPreviewMouseRightButton;
webBrowser.PreviewMouseRightButtonUp += HandleWebBrowserPreviewMouseRightButton;
private void HandleWebBrowserPreviewMouseRightButton(object sender, MouseButtonEventArgs e) {
// Preventing right-click until https://github.com/cefsharp/CefSharp/issues/1915 is fixed
e.Handled = true;
}
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;
}
You can do it like this....
ChromiumWebBrowser browser = new ChromiumWebBrowser();
browser.LoadingStateChanged += (sender, args) =>
{
if (args.IsLoading == false)
{
_browser.ExecuteScriptAsync("document.oncontextmenu = function() { return false; };");
}
};
I'm using this version:
<package id="CefSharp.Wpf" version="78.3.10-CI3386" targetFramework="net48" />
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();