如何在系统默认浏览器中打开来自 TCPpWebBrowser 组件的链接
我们在程序中使用 TCppWebBrowser 组件作为一种聊天窗口,但由于 TCppwebbrowser 使用 IExplorer 引擎,因此单击的所有链接都会在 IExplorer 中打开。 我的一个想法是取消 Onbeforenavigate2 中的导航并执行 Shell.execute,但希望有一个更优雅的解决方案,例如我可以处理的 windowsmessage 或事件或其他东西。
We are using a TCppWebBrowser Component in our program as a kind of chatwindow, but since the TCppwebrowser is using the IExplorerengine all links that are clicked is opening in IExplorer.
One idea I have is to cancel the navigation in Onbeforenavigate2 an do a Shell.execute, but where hoping for a more elegant solution like a windowsmessage i could handle or an event or something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设 TCppWebBrowser 就像 Delphi 中的 TWebBrowser,类似下面的代码应该可以帮助您。
OnBeforeNavigate2 事件 在 TWebBrowser 导航到新的位置之前被触发网址。
您要做的就是取消该导航,然后 使用 ShellExecute 将 URL 重定向到外部应用程序(这是 Windows 中配置的默认 Web 浏览器)。
为了使下面的代码正常工作,请双击表单,然后输入 FormCreate 事件方法内容。
然后放下一个 TWebBrowser,转到对象检查器的事件页面,双击 OnBeforeNavigate2 事件并输入该代码。
玩得开心!
——杰罗恩
Assuming that TCppWebBrowser is like TWebBrowser in Delphi, something like the code below should get you going.
The OnBeforeNavigate2 event gets fired before the TWebBrowser navigates to a new URL.
What you do is cancel that navigation, and redirect the URL with ShellExecute to an external application (which is the default web browser as configured in Windows).
In order to get the code below working, double click on your form, then enter the FormCreate event method content.
Then drop a TWebBrowser, go do the events page of the object inspector and double click on the OnBeforeNavigate2 event and enter that code.
Have fun with it!
--jeroen
Jeroen 给出的示例是正确的,但它不是 C++,我认为您可能会发现您正在使用的语言的示例很有帮助。 TCppWebBrowser 组件与 TWebBrowser 组件类似,并且具有相同的事件。 (不过,当您尝试访问某些内部结构时,它会变得更加复杂。)
这是我使用的 OnBeforeNavigate2 方法的编辑版本:
它取消 Web 浏览器内的导航,除了
about:blank
(如果它不是您的控件的合法页面,您可以删除该位)以及我希望其锁定的 URLm_strWebPage
。您可以使此检查更加灵活,例如,允许它导航某个域上的任何位置,但在另一个窗口中打开指向另一个域的链接。该代码也是为 C++Builder 2009 / 2010 编写的,因为它使用
UnicodeString
和L
字符串前缀。您没有说明您正在使用什么版本,但如果您使用的是 2007 或之前,请转换为WideString
。干杯,
大卫
The example Jeroen gave is right, except it's not C++, and I thought you might find an example in the language you're using helpful. The TCppWebBrowser component is similar to the TWebBrowser component and has the same events. (It get more complicated when you try to access some of the internals, though.)
Here's an edited version of an OnBeforeNavigate2 method I use:
It cancels navigation within the web browser, except to
about:blank
(you could remove that bit if it's not a legal page for your control) and the URLm_strWebPage
that is the one I want it locked to. You could make this check more flexible, allowing it to, say, navigate anywhere on a certain domain but opening links to another domain in another window, for example.The code is also written for C++Builder 2009 / 2010, because of its use of
UnicodeString
and theL
string prefix. You don't say what version you're using, but if you are using 2007 or before cast toWideString
instead.Cheers,
David
事实上,当您没有处理
TCppWebBrowser
的“webNewWindow2”时,您打开的TCppWebBrowser
中的链接将使用系统默认浏览器。您无需执行任何操作。有关
TCppWebBrowser
的更多代码,请参阅我找到的这个链接:http://codeback.net/tag/tcppwebbrowser
In fact, when you did not handle the "webNewWindow2" of the
TCppWebBrowser
, the link in theTCppWebBrowser
you open will be used the system default browser. There is nothing you need to do.For more codes of
TCppWebBrowser
, see this link I found:http://codeback.net/tag/tcppwebbrowser