使用 WPF 在 CefSharp Chromium web 浏览器中右键单击时删除默认上下文菜单

Delete default context menu when right click in CefSharp Chromium webbrowser with WPF

在我的 WPF 应用程序中,我正在 CefSharp Web 浏览器中加载 URL。当我用鼠标右键单击时,默认上下文菜单显示如下图

我的 xaml 代码是

<Window x:Class="StoneApp.WebviewWindow" WindowState="Maximized"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" 
    xmlns:local="clr-namespace:StoneApp"
    mc:Ignorable="d"
    Title="MyApp">

<DockPanel>

    <Grid x:Name="mygrid">
        <cefSharp:ChromiumWebBrowser x:Name="my_webview" IsBrowserInitializedChanged ="InitializeBrowser" Grid.Row="0" MouseRightButtonDown="Rightbtn_MouseDown"/>
    </Grid>

</DockPanel>

</Window>

我的xaml.cs代码是

  public WebviewWindow(string webresponse)
    {
        InitializeComponent();
        this.webresponse = webresponse;
    }

    private void InitializeBrowser(object sender, DependencyPropertyChangedEventArgs e)
    {
        my_webview.Load(webresponse);
    }

    private void Rightbtn_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.RightButton == MouseButtonState.Pressed)
        {
            e.Handled = false;
        }
    }

当我右击时,我也无法调用 'Rightbtn_MouseDown' 函数。所以我的问题是我想 delete/hide 右键单击​​网络浏览器视图时的默认上下文菜单。

您只需覆盖默认值 IContextMenuHandler

webView.MenuHandler = new MenuHandler();

internal class MenuHandler : IContextMenuHandler
{
    public bool IContextMenuHandler.RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
    {
        return false;
    }

    public void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
    {
        model.Clear();
    }
}

您可以在此处阅读更多相关信息:https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf.Example/Handlers/MenuHandler.cs