Custom context menu for WPF WebBrowser Control

后端 未结 4 545
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 15:02

Hi I need to create a custom context menu for a web browser control in wpf. Here is my xaml code which is not working:



        
4条回答
  •  离开以前
    2021-01-21 15:29

    No, It's not possible to make this work.
    WebBrowser control is a very thin wrapper around native WebBrowser ActiveX component, which is a part of Internet Explorer subsystem. It is hosted in it's own window host (WPF window and WebBrowser have different HWNDs), so WPF knows only about focus entering and leaving WebBrowser, but has no knowledge of any keyboard/mouse events. Also, there is so called 'airspace problem': WPF-rendered and native regions of screen area cannot overlap.
    Therefore you cannot use WPF ContextMenu with WebBrowser, because:

    1. WPF doesn't recieve Mouse Right Click event to open Context Menu
    2. WPF cannot draw Context Menu above WebBrowser

    Also, I don't think there is easy way to emulate ContextMenu with html/js in browser's content - as I recall, ActiveX component uses IE5 (quirk) rendering mode, and it is not possible to change that without changing registry files.
    You can try to use ActiveX API with WebBrowser.Document object to disable native context menu and draw another one yourself through WinAPI, which is not an easy task.
    So, I would recommend to look for other, pure-WPF browser controls or HTML renderers, such as awesomium

提交回复
热议问题