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

前端 未结 5 1463
-上瘾入骨i
-上瘾入骨i 2021-02-07 01:01

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:

相关标签:
5条回答
  • 2021-02-07 01:49

    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.

    0 讨论(0)
  • 2021-02-07 01:51
    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;
    }
    
    0 讨论(0)
  • 2021-02-07 01:58

    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;
    }
    
    0 讨论(0)
  • 2021-02-07 02:02

    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" />
    
    0 讨论(0)
  • 2021-02-07 02:03

    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();
    
    0 讨论(0)
提交回复
热议问题