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

5

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?

Bragi answered 12/12, 2015 at 19:52 Comment(2)
Please show some research effort, and tell us what you've already thought ofDevaluate
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.Bragi
D
54

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();
Diversiform answered 7/11, 2016 at 17:18 Comment(4)
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!Crawfish
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.Desolate
Still works with CefSharp 93.1.140Devol
To use CustomMenuHandler in XAML: <wpf:ChromiumWebBrowser ><wpf:ChromiumWebBrowser.MenuHandler><local:CustomMenuHandler /> </wpf:ChromiumWebBrowser.MenuHandler></wpf:ChromiumWebBrowser>Luhey
W
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;
}
Weide answered 29/1, 2019 at 9:8 Comment(1)
Keep in mind the context menu can still be opened with "Context" keyboard button or Shift+F10Luhey
L
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.

Leveller answered 13/12, 2015 at 21:58 Comment(0)
D
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" />
Den answered 10/1, 2020 at 21:1 Comment(0)
A
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;
}
Amazonite answered 19/9, 2017 at 1:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.