Disable context menu web browser control

I am using a WPF WebBrowser control to display a PDF. I want to disable the context menu.

I have already tried the following -

  • Disable context menu in Internet Explorer Management -

    The following is added to the constructor -

    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); } } 
  • Also noted is http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7c283faf-16c8-4b4e-a362-f292e3032abb/ .

    The approach used to install the registry is HKEY_LOCAL_MACHINE \ Software \ Policies \ Microsoft \ Internet Explorer \ Restrictions \ NoBrowserContextMenu for DWORD 1.

    When I set up the registration, the PDF is not displayed properly.

    Also, since I use PDF for display, I cannot set - in the body -

     oncontextmenu="return false;" 
  • It is not possible to set IsWebBrowserContextMenuEnabled because I am using WPF web browser control.

+4
source share
4 answers

This may be due to the PDF renderer that wants to display its own context menu.

What context menu do you see? One of IE or one of the PDF renderer?

I will also try to use System.Windows.Forms.WebBrowser (you can use it in WPF). I used it in a WPF application because it has more functionality than WPF.

0
source

You can bind WebBrowser Windows Forms to the bind and disable the context menu (and access to other properties) in a simple way:

 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 are the steps for binding Windows Forms controls in a WPF project.

0
source

Here is the solution:

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

Just use

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

I tried this in windows applications

-1
source

All Articles