如何在系统默认浏览器中打开来自 TCPpWebBrowser 组件的链接

发布于 2024-08-07 12:00:56 字数 203 浏览 7 评论 0原文

我们在程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖风昔人 2024-08-14 12:00:56

假设 TCppWebBrowser 就像 Delphi 中的 TWebBrowser,类似下面的代码应该可以帮助您。

OnBeforeNavigate2 事件 在 TWebBrowser 导航到新的位置之前被触发网址。
您要做的就是取消该导航,然后 使用 ShellExecute 将 URL 重定向到外部应用程序(这是 Windows 中配置的默认 Web 浏览器)。

为了使下面的代码正常工作,请双击表单,然后输入 FormCreate 事件方法内容。
然后放下一个 TWebBrowser,转到对象检查器的事件页面,双击 OnBeforeNavigate2 事件并输入该代码。

玩得开心!

——杰罗恩

unit MainFormUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
        var URL, Flags, TargetFrameName, PostData, Headers: OLEVariant; var Cancel:
        WordBool);
  private
    RedirectUrls: Boolean;
  end;

var
  Form1: TForm1;

implementation

uses
  ShellAPI;

{$R *.dfm}

procedure TForm1.Create(Sender: TObject);
begin
  WebBrowser1.Navigate('http://www.stackoverflow.com');
  RedirectUrls := True;
end;

procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp:
    IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OLEVariant;
    var Cancel: WordBool);
var
  UrlString: string;
begin
  if not RedirectUrls then
    Exit;
  UrlString := URL;
  ShellExecute(Self.WindowHandle, 'open', PChar(UrlString), nil, nil, SW_SHOWNORMAL);
  Cancel := True;
end;

end.

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

unit MainFormUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
        var URL, Flags, TargetFrameName, PostData, Headers: OLEVariant; var Cancel:
        WordBool);
  private
    RedirectUrls: Boolean;
  end;

var
  Form1: TForm1;

implementation

uses
  ShellAPI;

{$R *.dfm}

procedure TForm1.Create(Sender: TObject);
begin
  WebBrowser1.Navigate('http://www.stackoverflow.com');
  RedirectUrls := True;
end;

procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp:
    IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OLEVariant;
    var Cancel: WordBool);
var
  UrlString: string;
begin
  if not RedirectUrls then
    Exit;
  UrlString := URL;
  ShellExecute(Self.WindowHandle, 'open', PChar(UrlString), nil, nil, SW_SHOWNORMAL);
  Cancel := True;
end;

end.
孤单情人 2024-08-14 12:00:56

Jeroen 给出的示例是正确的,但它不是 C++,我认为您可能会发现您正在使用的语言的示例很有帮助。 TCppWebBrowser 组件与 TWebBrowser 组件类似,并且具有相同的事件。 (不过,当您尝试访问某些内部结构时,它会变得更加复杂。)

这是我使用的 OnBeforeNavigate2 方法的编辑版本:

void __fastcall TMyForm::CppWebBrowserBeforeNavigate2(TObject* /*Sender*/,
        LPDISPATCH /*pDisp*/, Variant* URL, Variant* /*Flags*/, Variant* /*TargetFrameName*/,
        Variant* /*PostData*/, Variant* /*Headers*/, VARIANT_BOOL *Cancel)
{
    UnicodeString strURL(URL->operator UnicodeString());
    if (strURL != m_strWebPage && strURL != L"about:blank") { // if navigating somewhere else, ie the user clicked a link
        *Cancel = true;
        if ((int)::ShellExecute(NULL, L"open", strURL.c_str(), NULL, NULL, SW_SHOWNORMAL) <= 32) {
            MyMessageBox(L"There was an error opening the link '" + strURL + L"'.");
        }
    }
}

它取消 Web 浏览器内的导航,除了 about:blank (如果它不是您的控件的合法页面,您可以删除该位)以及我希望其锁定的 URL m_strWebPage。您可以使此检查更加灵活,例如,允许它导航某个域上的任何位置,但在另一个窗口中打开指向另一个域的链接。

该代码也是为 C++Builder 2009 / 2010 编写的,因为它使用 UnicodeStringL 字符串前缀。您没有说明您正在使用什么版本,但如果您使用的是 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:

void __fastcall TMyForm::CppWebBrowserBeforeNavigate2(TObject* /*Sender*/,
        LPDISPATCH /*pDisp*/, Variant* URL, Variant* /*Flags*/, Variant* /*TargetFrameName*/,
        Variant* /*PostData*/, Variant* /*Headers*/, VARIANT_BOOL *Cancel)
{
    UnicodeString strURL(URL->operator UnicodeString());
    if (strURL != m_strWebPage && strURL != L"about:blank") { // if navigating somewhere else, ie the user clicked a link
        *Cancel = true;
        if ((int)::ShellExecute(NULL, L"open", strURL.c_str(), NULL, NULL, SW_SHOWNORMAL) <= 32) {
            MyMessageBox(L"There was an error opening the link '" + strURL + L"'.");
        }
    }
}

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 URL m_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 the L string prefix. You don't say what version you're using, but if you are using 2007 or before cast to WideString instead.

Cheers,

David

幸福还没到 2024-08-14 12:00:56

事实上,当您没有处理TCppWebBrowser的“webNewWindow2”时,您打开的TCppWebBrowser中的链接将使用系统默认浏览器。您无需执行任何操作。

有关TCppWebBrowser的更多代码,请参阅我找到的这个链接:
http://codeback.net/tag/tcppwebbrowser

In fact, when you did not handle the "webNewWindow2" of the TCppWebBrowser, the link in the TCppWebBrowser 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文