简体   繁体   English

禁用上下文菜单Web浏览器控件

[英]Disable context menu webbrowser control

I am using the WPF WebBrowser control to display a PDF. 我正在使用WPF WebBrowser控件显示PDF。 I want to disable the context menu. 我想禁用上下文菜单。

I have already tried the following - 我已经尝试了以下方法-

  1. Disable context menu in Internet Explorer control - 在Internet Explorer控件中禁用上下文菜单 -

    In the constructor, added the following - 在构造函数中,添加以下内容-

     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); } } 
  2. Also checked http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7c283faf-16c8-4b4e-a362-f292e3032abb/ . 还检查了http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7c283faf-16c8-4b4e-a362-f292e3032abb/

    Used the approach to set the registry - HKEY_LOCAL_MACHINE\\Software\\Policies\\Microsoft\\Internet Explorer\\Restrictions\\NoBrowserContextMenu to DWORD 1. 使用此方法将注册表-HKEY_LOCAL_MACHINE \\ Software \\ Policies \\ Microsoft \\ Internet Explorer \\ Restrictions \\ NoBrowserContextMenu设置为DWORD 1。

    When I set the regisrty, the PDF does not get displayed properly. 设置注册表时,PDF无法正确显示。

    Also, since I am using PDF to display, I cannot set - in body - 另外,由于我使用PDF进行显示,因此无法设置-正文-

     oncontextmenu="return false;" 
  3. Cannot set IsWebBrowserContextMenuEnabled as I am using WPF web browser control. 我正在使用WPF Web浏览器控件时,无法设置IsWebBrowserContextMenuEnabled

It could be because of the PDF visualizer that wants to show it's own context menu. 可能是因为PDF可视化工具想要显示其自己的上下文菜单。

Which context menu are you seeing? 您看到哪个上下文菜单? The one of IE or the one of the PDF visualizer? IE之一还是PDF可视化工具之一?

I would also try to use the System.Windows.Forms.WebBrowser (you can use it in WPF). 我还将尝试使用System.Windows.Forms.WebBrowser (可以在WPF中使用它)。 I used it in a WPF application because it has more functionality than the one of WPF. 我在WPF应用程序中使用了它,因为它比WPF拥有更多的功能。

You can wrap the Windows Forms WebBrowser to be bindable and disable the context menu (and access to other properties) in an easy way: 您可以将Windows Forms 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;
    }
}

Here the steps to reference Windows Forms controls in a WPF project. 这里是在WPF项目中引用Windows窗体控件的步骤。

Here is a solution: 这是一个解决方案:

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

Just use 只需使用

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

i tried this in Windows Applications 我在Windows应用程序中尝试过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM