添加自定义上下文菜单到托管Web浏览器控件 [英] add custom context menu to hosted web browser control

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

问题描述

我托管一个网络浏览器控件,并且想要提供我自己的上下文菜单。

I am hosting a web browser control, and want to provide my own context menu.

理想情况下,我想展示我自己的上下文菜单,浏览器的上下文菜单(所有加载项等)作为子菜单。

Ideally, I want to present my own context menu, that contains the original browser's context menu (with all addins etc.) as a sub menu.

如果这不可能/棘手,通常显示我的上下文菜单,并且当用户按SHIFT时显示原始菜单。

If that's not possible / to tricky, I'd be ok with e.g. normally showing my context menu, and showing the original one when the user presses SHIFT.

我需要实施 IDocHostUIHandler ?如果是,如何指定自定义上下文菜单,如何强制原始的?

Do I need to implement IDocHostUIHandler? if yes, how do I specify a custom context menu, how can I force the original one? How do I get the control to use my implementation?

该控件是这样创建的(错误处理省略):

The control is created as such (error handling omitted):

HRESULT hr=AtlAxCreateControlEx(
   L"Shell.Explorer",m_wndWebCtrl.m_hWnd,
   NULL,NULL,(IUnknown**)&unk,
   IID_IWebBrowser2, NULL); // (IPersistStreamInit*)this);

hr = AtlAdviseSinkMap( this, true);

IUnknownPtr unk;
AtlAxGetControl(m_wndWebCtrl.m_hWnd, &unk);

IWebBrowser2Ptr browser2 = unk;


推荐答案

是的,你需要实现 IDocHostUIHandler

好吧,我想你可以拦截右键点击,击键,以及通常显示上下文菜单的其他消息...但这可能会很快崩溃或以后;至少,我期望它打破可访问性。

Ok, i guess you could intercept right-clicks, keystrokes, and that other message that'll normally display a context menu... But that's probably gonna break badly sooner or later; at very least, i'd expect it to break accessibility.

一旦您拦截了 IDocHostUIHandler :: ShowContextMenu() ,您可以选择返回 S_OK 在菜单后显示你自己的。你可以使用正常的Win32菜单例程为此目的,自定义控件,甚至花哨的HTML,如果这是它为你。根据文档,提供足够的上下文以允许您确定请求什么元素上下文以及默认上下文菜单。

Once you've intercepted IDocHostUIHandler::ShowContextMenu(), you have the option of returning S_OK to squelch the built-in menu after showing your own. You can use the normal Win32 menu routines for this purpose, a custom control, or even fancy HTML if that's what does it for you. Per the documentation, enough context is provided to allow you to determine what element context is requested for, and what the default context menu would be.

不幸的是,我知道没有办法获得内置菜单的句柄。你可以通过显示你的上下文菜单,然后返回 S_FALSE 如果用户选择原始选项,但即使没有办法将结果菜单附加到现在的弹出菜单(这真的应该是在你返回,如果你运行模态循环共同的这样的弹出窗口的时间)。可以添加选项到内置菜单

Unfortunately, I know of no way to get a handle to the built-in menu. You could probably fake it by showing your context menu and then returning S_FALSE if the user chose the "original" option, but even then there's no way to attach the resulting menu to an existing popup menu (which really should be gone by the time you return anyway if you're running the modal-loop common to such popups). It is possible to add options to the built-in menus.

您应该能够使用 GetKeyboardState() 以确定请求菜单时的shift键的状态。

You should be able to use GetKeyboardState() to determine the state of the shift key when the menu was requested.


假设您只需要一部分的正常浏览器功能 / em>,可以通过重新实现所需的选项(返回,转发,打印)以及如果用户选择相应的命令来更好地提供服务。或者,如果你只想在一个非常特定的场景中使用正常的菜单(例如:在textarea中编辑命令),使用 ShowContextMenu() S_FALSE 以触发默认值。我有很好的运气与后一种技术;毕竟他们 应该是上下文菜单...

Assuming you only want a subset of the normal browser functionality anyway, you might be better served by just re-implementing the options you want (back, forward, print) and invoking the appropriate command if the user chooses them. Alternately, if you only want normal menus in a very specific scenario (for instance: editing commands in a textarea), use the ShowContextMenu() arguments to identify this and only then return S_FALSE to trigger the default. I've had pretty good luck with this latter technique; after all, they are supposed to be context menus...

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

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