TWebBrowser 与 MSHTML(2): 获取 window 对象的时机


这是在页面下载完毕后即刻获取:


unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, OleCtrls, SHDocVw, StdCtrls;



type

  TForm1 = class(TForm)

    WebBrowser1: TWebBrowser;

    Button1: TButton;

    Button2: TButton;

    procedure FormCreate(Sender: TObject);

    procedure WebBrowser1DocumentComplete(ASender: TObject;

      const pDisp: IDispatch; var URL: OleVariant);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



uses MSHTML;

var window: IHTMLWindow2;



{建立时同时打开页面}

procedure TForm1.FormCreate(Sender: TObject);

begin

  WebBrowser1.Align := alTop;

  WebBrowser1.Navigate('http://del.cnblogs.com');

end;



{当页面打开完毕时获取 window 对象}

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;

  const pDisp: IDispatch; var URL: OleVariant);

begin

  if pDisp = (ASender as TWebBrowser).Application then

  begin

    window := ((ASender as TWebBrowser).Document as IHTMLDocument2).parentWindow;

  end;

end;



end.


 
   

你可能感兴趣的:(WebBrowser)