onnewwindow2

Usually when we open a new URL in our program using TWebBrowser, it runs  the Internet Explorer(or the program which has been registered to be the default browser) and navigate to the new url. This is always not what we want. Following codes shows how to open the new window by ourself's custom window.

proceduer TForm1.FormShow(Sender :TObject);
begin
  WebBrowser1.Navigate('http://codeback.net');
end;

procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
  var ppDisp: IDispatch; var Cancel: WordBool);
var
  NewWindow: TForm1;
begin
  // a new instance of the form will be created
  NewWindow := TForm1.Create(self);
  NewWindow.Show;

  ppDisp := NewWindow.Webbrowser1.DefaultDispatch;
end;

If you are using C++Builder instead of Delphi, check here for the solusion Open new url in my custom window usging TCppWebBrowser in C-Builder .

 

When we write our program using TCppWebBrowser, we always want it to open a new web page by our custom window. While if you do not handle the OnNewWindow2 event, it would runs the default webbrowser to open the new url. This example shows how to handle the OnNewWindow2 event and get the result of what we want.

Create a new project, put a TCppWebBrowser onto the form. Assign codes to the form's OnShow and CppWebBrowser1's OnNewWindow2 handler. As following.

void __fastcall TForm1::FormShow(TObject *Sender)
{
    CppWebBrowser1->Navigate(L"http://codeback.net");   
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CppWebBrowser1NewWindow2(TObject *Sender,
      LPDISPATCH *ppDisp, VARIANT_BOOL *Cancel)
{
    //ppDisp: Is the new TCppWebBrowser's interface.
    //Cancel: Set this value to true, will cause the new url opened in same window.
    TForm1 *fmNew = new TForm1(this);
    fmNew->Show();
    *ppDisp = fmNew->CppWebBrowser1->ControlInterface;
}
//---------------------------------------------------------------------------

To see how to solve same problem in Delphi, check this article How to open a new web page in your custom window with TWebBrowser in Delphi .