C# 禁用上下文菜单webbrowser控件

C# 禁用上下文菜单webbrowser控件,c#,wpf,C#,Wpf,我正在使用WPF WebBrowser控件显示PDF。我想禁用关联菜单 我已经尝试过以下方法: webBrowser.LoadCompleted +=new LoadCompletedEventHandler(webBrowser_LoadCompleted); // Event Handler public void webBrowser_LoadCompleted(object sender, NavigationEventArgs e ) { webBrowser.Context

我正在使用WPF WebBrowser控件显示PDF。我想禁用关联菜单

我已经尝试过以下方法:

webBrowser.LoadCompleted +=new LoadCompletedEventHandler(webBrowser_LoadCompleted);

// Event Handler
public void webBrowser_LoadCompleted(object sender, NavigationEventArgs e )
{
    webBrowser.ContextMenu = null;
    var t = webBrowser.Document as System.Windows.Forms.HtmlDocument;
    // t is always coming as null, even though I can clearly see the pdf in the web browser control.
    if(t != null)
    {
        t.ContextMenuShowing += new System.Windows.Forms.HtmlElementEventHandler(t_ContextMenuShowing);
    }
}
  • -

    在构造函数中,添加了以下内容-

    webBrowser.LoadCompleted +=new LoadCompletedEventHandler(webBrowser_LoadCompleted);
    
    // Event Handler
    public void webBrowser_LoadCompleted(object sender, NavigationEventArgs e )
    {
        webBrowser.ContextMenu = null;
        var t = webBrowser.Document as System.Windows.Forms.HtmlDocument;
        // t is always coming as null, even though I can clearly see the pdf in the web browser control.
        if(t != null)
        {
            t.ContextMenuShowing += new System.Windows.Forms.HtmlElementEventHandler(t_ContextMenuShowing);
        }
    }
    
  • 也检查过了

    使用该方法将注册表-HKEY_LOCAL_MACHINE\Software\Policys\Microsoft\Internet Explorer\Restrictions\NoBrowserContextMenu设置为DWORD 1

    当我设置registry时,PDF无法正确显示

    另外,因为我使用PDF来显示,所以我不能在正文中设置-

    oncontextmenu="return false;"
    
  • 无法设置
    IsWebBrowserContextMenuEnabled
    ,因为我正在使用WPF web浏览器控件


  • 这可能是因为PDF可视化工具想要显示自己的上下文菜单

    你看到的是哪一个上下文菜单?是IE还是PDF可视化工具

    我还将尝试使用(您可以在WPF中使用它)。我在WPF应用程序中使用了它,因为它比WPF的功能更多。

    只需使用它

    // Disable the Context menu inside the web browser
    webBrowser1.IsWebBrowserContextMenuEnabled = false;
    

    我在Windows应用程序中尝试了这一点

    您可以将Windows窗体WebBrowser包装为可绑定的,并以一种简单的方式禁用上下文菜单(以及对其他属性的访问):

    public class WindowsFormsWebBrowser : WindowsFormsHost
    {
        public static readonly DependencyProperty HtmlProperty =
            DependencyProperty.Register(
                "Html",
                typeof(string),
                typeof(WindowsFormsWebBrowser),
                new PropertyMetadata(string.Empty, OnHtmlChanged, null));
    
        public static readonly DependencyProperty IsContentMenuEnabledProperty =
            DependencyProperty.Register(
            "IsContentMenuEnabled",
            typeof(bool),
            typeof(WindowsFormsWebBrowser),
            new PropertyMetadata(true, OnIsContextMenuEnabledChanged));
    
        private readonly System.Windows.Forms.WebBrowser webBrowser = new System.Windows.Forms.WebBrowser();
    
        public WindowsFormsWebBrowser()
        {
            Child = webBrowser;
        }
    
        public string Html
        {
            get { return GetValue(HtmlProperty) as string; }
            set { SetValue(HtmlProperty, value); }
        }
    
        public bool IsContentMenuEnabled
        {
            get { return (bool)GetValue(IsContentMenuEnabledProperty); }
            set { SetValue(IsContentMenuEnabledProperty, value); }
        }
    
        private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var browser = d as WindowsFormsWebBrowser;
    
            if (browser == null)
            {
                return;
            }
    
            browser.webBrowser.DocumentText = (string)e.NewValue;
        }
    
        private static void OnIsContextMenuEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var browser = d as WindowsFormsWebBrowser;
    
            if (browser == null)
            {
                return;
            }
    
            browser.webBrowser.IsWebBrowserContextMenuEnabled = (bool)e.NewValue;
        }
    }
    
    在WPF项目中引用Windows窗体控件的步骤。

    以下是解决方案:

    private void WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        ((WebBrowser)sender).InvokeScript("eval",
            "$(document).contextmenu(function() { return false; });");
    }
    

    从道德上讲,我无法帮助您禁用浏览器中的上下文菜单。它给用户带来了太多的痛苦,而且只需一点技术就可以轻松解决,因此依靠它来实现安全性在道德上也是错误的。他使用的是WPF,而不是Windows窗体。