问题
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?
回答1:
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();
回答2:
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.
回答3:
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;
}
回答4:
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;
}
来源:https://stackoverflow.com/questions/34244244/how-do-i-hide-the-cefsharp-winforms-chromiumwebbrowser-right-click-context-menu