IWebBrowser2:如何强制链接在新窗口中打开? [英] IWebBrowser2: how to force links to open in new window?

查看:93
本文介绍了IWebBrowser2:如何强制链接在新窗口中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关 WebBrowser自定义的MSDN文档说明了如何防止出现新窗口从打开和如何取消导航。在我的情况下,我的应用程序托管一个IWebBrowser2,但我不想让用户导航到我的应用程序中的新页面。相反,我想打开一个新的IE窗口中的所有链接。所需的行为是:用户单击一个链接,并打开一个带有该URL的新窗口。



一个类似的问题被问及回答了在这里,而不是污染那回答的帖子,建议我开一个新的讨论。 / p>

相关帖子的成员建议我应该能够通过捕获DISPID_BEFORENAVIGATE2,设置取消标志,并编写代码打开一个新窗口,但我发现浏览器控件获取很多的BeforeNavigate2事件,似乎是由主页上的脚本启动。例如,amazon.com启动BeforeNavigate2事件,如疯狂,并且它们不是链接调用的结果。



赞赏的回复!



我实际上有MS开发人员支持参与,这种方法他们的建议。他们说这是Outlook用于基于HTML的电子邮件,这是我想模拟的用户体验。他们还证实,没有可靠的方法来过滤由脚本活动导致的用户操作导致的OnBeforeNavigate事件。



希望这有助于面对同样问题的任何人。将代码移植到使用IHTMLDocument并不难。如果你最终这样做,你也可能会发现自己正在寻找一种方法来确定文档何时完成加载。为此,请钩住HTMLDocumentEvents而不是DWebBrowserEvents,然后查找DISPID_HTMLDOCUMENTEVENTS_ONREADYSTATECHANGE事件。它不告诉你什么是就绪状态;您需要调用IHTMLDocument :: get_readyState并解析生成的字符串。 Goofy,但你去。


The MSDN documentation on WebBrowser Customization explains how to prevent new windows from being opened and how to cancel navigation. In my case, my application is hosting an IWebBrowser2 but I don't want the user to navigate to new pages within my app. Instead, I'd like to open all links in a new IE window. The desired behavior is: user clicks a link, and a new window opens with that URL.

A similar question was asked and answered here and rather than pollute that answered post, it was suggested I open a new discussion.

The members on the related post suggested I should be able to do this by trapping DISPID_BEFORENAVIGATE2, setting the cancel flag, and writing code to open a new window, but I've found out that the browser control gets lots of BeforeNavigate2 events that seem to be initiated by scripts on the main page. For example, amazon.com fires BeforeNavigate2 events like crazy, and they are not a result of link invocation.

Replies appreciated!

解决方案

Sorry to answer my own question here, but in the hopes that this may help somebody else, what I ended up doing was using IHTMLDocument directly rather than IWebBrowser. IWebBrowser is a superset of IHTMLDocument, and the navigation model implemented by IWebBrowser isn't customizable to the degree I wanted.

I actually got MS Developer Support involved and this approach was their recommendation. They say this is what Outlook uses for HTML-based email, which is the user experience I wanted to emulate. They also confirmed that there's no reliable way to filter the OnBeforeNavigate events that result from user action from those that result from script activity.

Hope this helps anybody facing the same issues. It wasn't too hard to port the code to use IHTMLDocument. If you end up doing this, you may also find yourself looking for a way to figure out when the document is done loading. To do that, hook HTMLDocumentEvents instead of DWebBrowserEvents, and look for the DISPID_HTMLDOCUMENTEVENTS_ONREADYSTATECHANGE event. It doesn't tell you what the ready state is; you need to call IHTMLDocument::get_readyState and parse the resulting string. Goofy, but there you go.

这篇关于IWebBrowser2:如何强制链接在新窗口中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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