繁体   English   中英

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

[英]Disable context menu webbrowser control

我正在使用WPF WebBrowser控件显示PDF。 我想禁用上下文菜单。

我已经尝试了以下方法-

  1. 在Internet Explorer控件中禁用上下文菜单 -

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

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

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

    设置注册表时,PDF无法正确显示。

    另外,由于我使用PDF进行显示,因此无法设置-正文-

     oncontextmenu="return false;" 
  3. 我正在使用WPF Web浏览器控件时,无法设置IsWebBrowserContextMenuEnabled

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

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

我还将尝试使用System.Windows.Forms.WebBrowser (可以在WPF中使用它)。 我在WPF应用程序中使用了它,因为它比WPF拥有更多的功能。

您可以将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;
    }
}

这里是在WPF项目中引用Windows窗体控件的步骤。

这是一个解决方案:

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

只需使用

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

我在Windows应用程序中尝试过

暂无
暂无

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

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