How do I open links from a TCppWebBrowser component in the systems default browser

不想你离开。 提交于 2019-12-11 09:52:10

问题


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.


回答1:


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.



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/1558768/how-do-i-open-links-from-a-tcppwebbrowser-component-in-the-systems-default-brows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!