在新选项卡(Web 浏览器控件)中打开链接
有谁知道如何单击 WinForms 应用程序中 WebBrowser 控件中的链接,然后在 TabControl 内的新选项卡中打开该链接?
我已经搜索了几个月,看过很多教程/文章/代码示例,但似乎以前没有人在 C# 中尝试过这个。
非常感谢任何建议/样品。
谢谢。
Does anybody know how to click on a link in the WebBrowser control in a WinForms application and then have that link open in a new tab inside my TabControl?
I've been searching for months, seen many tutorials/articles/code samples but it seems as though nobody has ever tried this in C# before.
Any advice/samples are greatly appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据您的评论,我了解到您希望捕获 WebBrowser 控件的“在新窗口中打开”操作,并覆盖默认行为以在应用程序内的新选项卡中打开。
为了可靠地完成此操作,您需要获取 NewWindow2 事件,该事件公开 ppDisp(指向应打开新窗口的 WebBrowser 控件的可设置指针)。
所有其他潜在的破解解决方案(例如获取用户在 OpenWindow 事件之前选择的最后一个链接)都不是最佳的,并且在极端情况下必然会失败。
幸运的是,有一种(相对)简单的方法可以完成此任务,同时仍然使用 System.Windows.Forms.WebBrowser 控件作为基础。您需要做的就是扩展 WebBrowser 并拦截 NewWindow2 事件,同时提供对 ActiveX 实例的公共访问(用于在新选项卡中传递到 ppDisp)。这之前已经完成了,Mauricio Rojas 有一个很好的例子,有一个完整的工作类“ExtendedWebBrowser”:
http://blogs.artinsoft.net/mrojas/archive/2008/09/18/newwindow2-events-in-the-c-webbrowsercontrol.aspx
一旦您拥有了 ExtendedWebBrowser 类,您所需要做的就是为 NewWindow2 设置处理程序并将 ppDisp 指向新选项卡中的浏览器。这是我放在一起的一个示例:(
假设 TabControl 名为“Tabs”,初始选项卡包含子控件,停靠的 ExtendedWebBrowser 名为“InitialWebBrowser”)
不要忘记在选项卡关闭时取消注册事件!
Based on your comments, I understand that you want to trap the "Open In New Window" action for the WebBrowser control, and override the default behavior to open in a new tab inside your application instead.
To accomplish this reliably, you need to get at the NewWindow2 event, which exposes ppDisp (a settable pointer to the WebBrowser control that should open the new window).
All of the other potential hacked together solutions (such as obtaining the last link selected by the user before the OpenWindow event) are not optimal and are bound to fail in corner cases.
Luckily, there is a (relatively) simple way of accomplishing this while still using the System.Windows.Forms.WebBrowser control as a base. All you need to do is extend the WebBrowser and intercept the NewWindow2 event while providing public access to the ActiveX Instance (for passing into ppDisp in new tabs). This has been done before, and Mauricio Rojas has an excellent example with a complete working class "ExtendedWebBrowser":
http://blogs.artinsoft.net/mrojas/archive/2008/09/18/newwindow2-events-in-the-c-webbrowsercontrol.aspx
Once you have the ExtendedWebBrowser class, all you need to do is setup handlers for NewWindow2 and point ppDisp to a browser in a new tab. Here's an example that I put together:
(Assumes TabControl named "Tabs" and initial tab containing child control docked ExtendedWebBrowser named "InitialWebBrowser")
Don't forget to unregister the events when the tabs are closed!
以下代码有效,只需按照第一个回复创建 ExtendedWebBrowser 类即可。
我用它来打开一个新选项卡,但它也可以使用浏览器而不是 IE 打开一个新窗口。
希望有帮助。
The following code works, just follow the first reply for creating the ExtendedWebBrowser class.
I'm using this to open a new tab but it also works to open a new window using your browser and not IE.
Hope it helps.
我对这个主题做了一些研究,不需要执行 ExtendedWebBrowser 类中存在的所有 COM 管道,因为该代码已经存在于生成的 Interop.SHDocVw 中。因此,我能够使用下面更自然的构造来订阅 NewWindow2 事件。在 Visual Studio 中,我必须添加对“Microsoft Internet Controls”的引用。
请阅读 http://bit.ly/IDWm5A 了解更多信息。这是该系列中的第 5 页,为了完整理解,我必须返回并阅读第 3 页 -> 第 5 页。 5.
I did a bit of research on this topic and one does not need to do all the COM plumbing that is present in the ExtendedWebBrowser class, as that code is already present in the generated Interop.SHDocVw. As such, I was able to use the more natural construct below to subscribe to the NewWindow2 event. In Visual Studio I had to add a reference to "Microsoft Internet Controls".
Please read http://bit.ly/IDWm5A for more info. This is page #5 in the series, for a complete understanding I had to go back and read pages 3 -> 5.
您只需取消新窗口事件并自行处理导航和选项卡内容即可。
这是一个完整的工作示例。这假设您有一个选项卡控件和至少 1 个选项卡页。
You simply cancel the new window event and handle the navigation and tab stuff yourself.
Here is a fully working example. This assumes you have a tabcontrol and at least 1 tab page in place.
Web 浏览器控件中没有选项卡,因此您需要自己处理选项卡。在 Web 浏览器控件上方添加选项卡控件,并在打开新选项卡时创建新的 Web 浏览器控件。当用户打开新窗口并打开新选项卡时捕获并取消。
There is no tabbing in the web browser control, therefor you need to handle the tabs yourself. Add a tab control above the web browser control and create new web browser controls when new tabs are being opened. Catch and cancel when the user opens new windows and open new tabs instead.