Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Context Menu on WebBrowser in WPF [duplicate]

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.

like image 855
Filipe Miguel Avatar asked Mar 31 '11 23:03

Filipe Miguel


2 Answers

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/

like image 60
Sandy Avatar answered Sep 17 '22 19:09

Sandy


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

like image 30
Dave White Avatar answered Sep 18 '22 19:09

Dave White