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

hubpan picture hubpan · Dec 12, 2015 · Viewed 11.6k times · Source

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?

Answer

Jay. picture Jay. · Nov 7, 2016

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();