Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hide the CefSharp.WinForms.ChromiumWebBrowser right click context menu?

Tags:

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:

dialog picture

But I don't want to show anything. What should I do?

like image 937
hubpan Avatar asked Dec 12 '15 19:12

hubpan


2 Answers

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(); 
like image 101
Jay. Avatar answered Oct 11 '22 07:10

Jay.


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; } 
like image 41
Trần Tiến Hoàng Avatar answered Oct 11 '22 07:10

Trần Tiến Hoàng