29

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?

maazza
  • 7,016
  • 15
  • 63
  • 96
hubpan
  • 291
  • 1
  • 3
  • 3
  • Please show some research effort, and tell us what you've already thought of – Sebastian Walla Dec 12 '15 at 19:59
  • I want my software user didn't konw this context menu exist.but when they select some text on webbrowser, and do right click, they can use "copy" context menu.I only disable page default right context menu. – hubpan Dec 12 '15 at 20:09

5 Answers5

53

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();
Jay.
  • 531
  • 4
  • 3
  • 1
    Great idea - I would like to improve that 1. Need create new cs file: CustomMenuHandler.cs 2. Write complete CustomMenuHandler 3. Save It! 4. chromeBrowser.menuHandler = new CustomMenuHandler(); Still working with v53.0.1 too Yay! – SourceSkyBoxer Dec 30 '16 at 18:34
  • Working for me too (in Cefsharp v63.0), as a means of ensuring the default context menu does not appear when right-clicking on the Chromium browser control. – AndyUK Dec 07 '18 at 07:42
  • Still works with CefSharp 93.1.140 – fviktor Oct 24 '21 at 03:04
  • To use CustomMenuHandler in XAML: ` ` – Justas Jan 25 '22 at 09:39
6

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;
}
  • Keep in mind the context menu can still be opened with "Context" keyboard button or Shift+F10 – Justas Jan 25 '22 at 09:13
4

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.

amaitland
  • 4,073
  • 3
  • 25
  • 63
3

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" />
SteffenSH
  • 570
  • 1
  • 5
  • 9
1
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;
}