2005/10/28 | 自制一个MyIE
类别(语言类学习笔记) | 评论(4) | 阅读(313) | 发表于 01:13
procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
var
newtab : TTabSheet;
newweb : TWebBrowser;
begin
newtab := TTabSheet.Create(Self) ;
newtab.PageControl := PageControl1;
newtab.Caption := 'PageControl1';
newweb := TWebBrowser.Create(self);
newweb.ParentWindow := newtab.Handle;
TOleControl(newweb).Parent :=newtab; //加入此行
newweb.OnNewWindow2 := WebBrowser1.OnNewWindow2
newweb.Align := alClient;
ppDisp := newweb.DefaultDispatch;
end;

發表於 - 2003/03/03 : 10:36:35
--------------------------------------------------------------------------------
要取得瀏覽器中網頁的 title 必須要在網頁下載完成後,
取用其 document 中的 title 即可. 所以你可以先寫在 DocumentComplete event 中, 如下,
即可取得文件中的 title!!


procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
hd : IHTMLDocument2;
begin
hd := TWebBrowser(Sender).Document as IHTMLDocument2;
TTabSheet(TOleControl(Sender).Parent).Caption := hd.title;
end;





================
以下是本人写的例子
unit uMain_URL;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, OleCtrls, SHDocVw,activex,MSHTML;//MSHTML_TLB;

type
TForm1 = class(TForm)
PageControl1: TPageControl;
StatusBar1: TStatusBar;
Panel1: TPanel;
Edit1: TEdit;
TabSheet1: TTabSheet;
WebBrowser1: TWebBrowser;
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure WebBrowser1NewWindow2(Sender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
procedure WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);

private
{ Private declarations }
first:integer;
web:TWebBrowser;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=13 then
begin
self.web.Navigate(self.Edit1.Text);
first:=1;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
self.web:=TWebBrowser.Create(self);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
self.web.Free;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
self.web:=self.WebBrowser1;
end;


procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
var
tab:TTabSheet;
subweb:TWebBrowser;
begin
if first=0 then Exit;
tab:=TTabSheet.Create(self);
tab.Parent:=self.PageControl1;
tab.PageControl:=self.PageControl1;
Self.PageControl1.ActivePage:=tab;
tab.Align:=alClient;
tab.Visible:=True;

subweb:=TWebBrowser.Create(tab);
subweb.ParentWindow := tab.Handle;
TOleControl(subweb).Parent :=tab;
subweb.OnNewWindow2:=self.WebBrowser1NewWindow2;
subweb.OnDocumentComplete:=self.WebBrowser1DocumentComplete;
subweb.Align:=alClient;
subweb.Visible:=True;
ppDisp := subweb.DefaultDispatch;
Self.web:=subweb;
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
hd : iHTMLDocument2;
begin
hd := TWebBrowser(Sender).Document as IHTMLDocument2;
TTabSheet(TOleControl(Sender).Parent).Caption := hd.title;
end;

end.

0

评论Comments