WPF WebBrowser 控件的自定义上下文菜单 [英] Custom context menu for WPF WebBrowser Control

查看:32
本文介绍了WPF WebBrowser 控件的自定义上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 wpf 中的 Web 浏览器控件创建自定义上下文菜单.这是我的 xaml 代码,它不起作用:

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:

<WebBrowser x:Name="EmailBox"  ap:BrowserBehavior.HtmlString="{Binding Message, Mode=OneWay}">
    <WebBrowser.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Copy" Command="ApplicationCommands.Copy"/>
            <MenuItem Header="Copy to Customer Reference ID" 
                  Command="{Binding CopyID}"
                  CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, 
                  Path=PlacementTarget.Selection.Text}">
                <MenuItem.Icon>
                    <Image Source="{StaticResource CopyImageSource}" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy to Comments"
                  Command="{Binding CopyToCommentsCommand}"
                  CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, 
                  Path=PlacementTarget.Selection.Text}">
                <MenuItem.Icon>
                    <Image Source="{StaticResource NoteCopyI}" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
    </WebBrowser.ContextMenu>
</WebBrowser>

我从其他地方复制了上下文菜单代码.这适用于其他控件,但不适用于 webbrowser 控件.有可能完成这项工作吗?

I copied the context menu code from somewhere else. This works in other controls but not for the webbrowser control. Is it possible to make this work?

推荐答案

不,这是不可能的.
WebBrowser 控件是本机 WebBrowser ActiveX 组件的一个非常薄的包装器,它是 Internet Explorer 子系统的一部分.它托管在它自己的窗口主机中(WPF 窗口和 WebBrowser 具有不同的 HWND),因此 WPF 只知道焦点进入和离开 WebBrowser,但不知道任何键盘/鼠标事件.此外,还有所谓的空域问题":WPF 渲染的屏幕区域和本机区域不能重叠.
因此,您不能将 WPF ContextMenu 与 WebBrowser 一起使用,因为:

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 未收到鼠标右键单击事件以打开上下文菜单
  2. WPF 无法在 WebBrowser 上方绘制上下文菜单

另外,我认为在浏览器的内容中使用 html/js 模拟 ContextMenu 没有简单的方法 - 我记得,ActiveX 组件使用 IE5 (quirk) 呈现模式,并且在不更改注册表的情况下无法更改它文件.
您可以尝试使用带有 WebBrowser.Document 对象的 ActiveX API 来禁用本机上下文菜单并通过 WinAPI 自己绘制另一个菜单,这不是一件容易的事.
因此,我建议寻找其他纯 WPF 浏览器控件或 HTML 渲染器,例如 awesomium

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

这篇关于WPF WebBrowser 控件的自定义上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆