6

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Filipe Miguel
  • 519
  • 1
  • 6
  • 13
  • If disabling right click at all is not a problem then take a look at this http://stackoverflow.com/questions/4412915/how-to-deactivate-right-click-on-wpf-webbrowser-control – user1016945 Mar 06 '13 at 09:13

2 Answers2

8

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/

Sandy
  • 89
  • 2
  • 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. – kzfabi May 07 '12 at 18:52
  • If you want to load pages from internet you can try following: Get you html page using WebRequest and then -> yourHtml = yourHtml.Replace(" – user1016945 Feb 18 '13 at 12:44
  • 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 – NitinSingh Jul 04 '17 at 14:58
3

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

Dave White
  • 3,451
  • 1
  • 20
  • 25
  • 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 Apr 01 '11 at 08:49
  • 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... – Filipe Miguel Apr 01 '11 at 12:51
  • 1
    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. – Dave White Apr 01 '11 at 13:38
  • 2
    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. – John Gardner Feb 07 '12 at 00:13