Disable context menu in WebBrowser in WPF

I have a WPF application that will work in a kiosk, on a touch screen PC. The application runs in full screen mode to hide the OS. On one of my pages, I have a WebBrowser control that allows the user to browse some web pages (navigation is limited to certain pages). Since the computer will be located in an open place, I should not allow the user to access the operating system. The fact is that the touch screen allows you to right-click in a web browser, and this ultimately leads to the appearance of the taskbar ... not good ..!

I have been trying to disable this context menu for the past days without much success. Basically where I am now:

  • Added COM link to SHDocVw.dll to get IWebBrowser2 interface (you need to disable the launch 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"); 
  • Get 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 disabling oncontextmenu in the document.

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

Still no sucess ... any ideas?

Thanks in advance.

+6
c # wpf com-interop
source share
2 answers

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

 <body oncontextmenu="return false;"> 

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

+6
source share

Why aren't you using the WPF WebBrowser control ? There are many Touch events in it that you can capture and process and possibly stop ContextMenu from ever appearing. Or you can provide your own ContextMenu for use by the browser.

Preview events are below on the page and can be used to intercept events that trigger the context menu.

OnPreviewTouchDown

OnPreviewTouchMove

OnPreviewTouchUp

+3
source share

All Articles