Disable Context Menu on WebBrowser in WPF

10,594

Solution 1

Adding an oncontextmenu attribute to the document body tag worked for me.

<body oncontextmenu="return false;">

From this article here. http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7c283faf-16c8-4b4e-a362-f292e3032abb/

Solution 2

Why aren't you using the WPF WebBrowser control? It has many Touch events that you could capture and handle and potentially stop the ContextMenu from ever popping up. Or you could supply your own ContextMenu for the browser to use.

The preview events are lower down on the page and can be used to intercept events that would cause the context menu to pop-up.

OnPreviewTouchDown

OnPreviewTouchMove

OnPreviewTouchUp

Share:
10,594
Filipe Miguel
Author by

Filipe Miguel

Senior .NET Engineer at Paddle. C#, .NET, .NET Core, AWS, UWP, XAML, WPF, Python, Go.

Updated on June 04, 2022

Comments

  • Filipe Miguel
    Filipe Miguel almost 2 years

    I have a WPF App that is going to run on a kiosk, on a PC with a touchscreen. The App runs in fullscreen mode, to hide the OS. On one of my pages I have a WebBrowser control that allows a user to view some web pages (navigation is limited to some pages). Since the computer will be placed in a public spot I mustn't let the user access the operating system. The thing is, the touchscreen allows for a right click on the web browser, and that eventually leads to the task bar appearing... not good..!

    I've been trying to disable that context menu for the past days without much success. Basically where i'm at right now is:

    • Added a COM reference to SHDocVw.dll to get the IWebBrowser2 interface (needed to disable the launching of new windows.

      [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
      internal interface IServiceProvider
      {
          [return: MarshalAs(UnmanagedType.IUnknown)]
          object QueryService(ref Guid guidService, ref Guid riid);
      }
      static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
      
    • Fetch the IWEbBrowser2 interface

                  IServiceProvider _serviceProvider = null;
      
                  if (_browser.Document != null)
                  {
                      _serviceProvider = (IServiceProvider)_browser.Document;
      
                      Guid _serviceGuid = SID_SWebBrowserApp;
                      Guid _iid = typeof(SHDocVw.IWebBrowser2).GUID;
      
                      SHDocVw.IWebBrowser2 _webBrowser = (SHDocVw.IWebBrowser2)_serviceProvider.QueryService(ref _serviceGuid, ref _iid);
      
    • And try and disable the oncontextmenu on the document.

                      HTMLDocument doc = _webBrowser.Document as HTMLDocument;
                      mshtml.HTMLDocumentEvents2_Event ev = doc as mshtml.HTMLDocumentEvents2_Event;
      
                      ev.oncontextmenu += (arg) => { return false; };
      

    So far, no sucess... any ideas?

    Thanks in advance.

  • Filipe Miguel
    Filipe Miguel about 13 years
    The context menu doesn't come from the WPF WebBrowser, so i can't disable it from those events (actually I only tried the OnPreviewMouse... events, but I should get the same results). I could probably add my own context menu, maybe it'll make the other one disappear.
  • Filipe Miguel
    Filipe Miguel about 13 years
    It's as I feared, the context menu doesn't belong to the WPF WebBrowser but to the native browser it wraps. It's not a WPF ContextMenu. Attempting to add or change the context menu yields no results.. I read I could disable the context menu from the windows registry, although that's a bit overkill...
  • Dave White
    Dave White about 13 years
    I don't think disabling the context menu in the registry on a kiosk computer is overkill. I think that is a great idea. The more you can lock down this particular PC, the better.
  • John Gardner
    John Gardner about 12 years
    strangely, the windows FORMS version of the browser control has the ability to do IsWebBrowserContextMenuEnabled="False" AllowWebBrowserDrop="False" WebBrowserShortcutsEnabled="False" but not the WPF version? so odd.
  • kzfabi
    kzfabi almost 12 years
    This worked for me as I'm the one creating the web pages. This won't work if you have no control over what the user is looking in the browser. Unless you can somehow manipulate the DOM and add that line to the body element before it is displayed in the WebBrowser control.
  • user1016945
    user1016945 about 11 years
    If you want to load pages from internet you can try following: Get you html page using WebRequest and then -> yourHtml = yourHtml.Replace("<body", "<body oncontextmenu='return false;' "); webBrowser.NavigateToString(yourHtml);
  • NitinSingh
    NitinSingh almost 7 years
    What if we have a web app which needs to be opened for certain cases embedded in a web browser control. We cannot disable for web app, but need to disable for embedded version