好吧,代码如下:请各位大侠指教,Thanks
注:TWebBrowser5是就是TWebBrowser,是我另插入的。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, OleCtrls, SHDocVw, AppEvnts,SHDocVw_TLB,ComObj,MSHtml;
type
Tfrmmain = class(TForm)
pgmain: TPageControl;
Label1: TLabel;
btngoto: TButton;
btnexit: TButton;
btnclose: TButton;
ApplicationEvents1: TApplicationEvents;
bar: TStatusBar;
btnrefresh: TButton;
btnfored: TButton;
btnback: TButton;
cbaddress: TComboBox;
procedure btnexitClick(Sender: TObject);
procedure btngotoClick(Sender: TObject);
procedure btncloseClick(Sender: TObject);
procedure BrowserNewWindow2(Sender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
procedure BrowserBeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
procedure BrowserDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure BrowserNavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
procedure btnrefreshClick(Sender: TObject);
procedure btnbackClick(Sender: TObject);
procedure btnforedClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmmain: Tfrmmain;
implementation
{$R *.DFM}
//uses MSHtml;
procedure Tfrmmain.btnexitClick(Sender: TObject);
begin
application.Terminate;
end;
procedure Tfrmmain.btngotoClick(Sender: TObject);
var
ATab: TTabSheet;
i:integer;
ABrowser: TWebBrowser5;
begin
ATab := TTabSheet.Create(Application);
ATab.PageControl := pgMain;
ATab.Caption := 'New'+IntToStr(pgMain.PageCount);
ABrowser := TWebBrowser5.Create(ATab);
ATab.InsertControl(ABrowser);
ABrowser.Align := alClient;
ABrowser.Navigate(cbaddress.Text);
for i:=0 to cbaddress.Items.Count do
begin
if cbaddress.text = cbaddress.Items.Strings then
break;
end;
ABrowser.OnNewWindow2 := BrowserNewWindow2;
ABrowser.OnBeforeNavigate2 := BrowserBeforeNavigate2;
ABrowser.OnDocumentComplete := BrowserDocumentComplete;
ABrowser.OnNavigateComplete2 := BrowserNavigateComplete2;
pgmain.ActivePageIndex:=pgmain.PageCount-1;
end;
procedure Tfrmmain.btncloseClick(Sender: TObject);
begin
if pgmain.PageCount>0 then
pgmain.Pages[pgmain.ActivePageIndex].free;
end;
procedure Tfrmmain.BrowserNewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
var
ATab: TTabSheet;
ABrowser: TWebBrowser5;
begin
ATab := TTabSheet.Create(Application);
ATab.PageControl := pgMain;
ATab.Caption := 'New'+IntToStr(pgMain.PageCount);
ABrowser := TWebBrowser5.Create(ATab);
ATab.InsertControl(ABrowser);
ABrowser.Align := alClient;
ABrowser.OnNewWindow2 := BrowserNewWindow2;
ABrowser.OnBeforeNavigate2 := BrowserBeforeNavigate2;
ABrowser.OnDocumentComplete := BrowserDocumentComplete;
ABrowser.OnNavigateComplete2 := BrowserNavigateComplete2;
ppDisp := ABrowser.Application;
pgmain.ActivePageIndex:=pgmain.PageCount-1;
end;
procedure Tfrmmain.BrowserBeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
begin //
TTabSheet(TWinControl(Sender).Parent).Caption := 'CPC...';
bar.Panels.Items[0].Text:='正在打开...'+URL;
bar.Panels.Items[1].Text:='尚未完成链接';
end;
procedure Tfrmmain.BrowserDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
browser: TWebBrowser5;
doc: IHTMLDocument2;
begin
browser := TWebBrowser5(Sender);
doc := browser.Document as IHTMLDocument2;
if doc <> nil then
begin
TTabSheet(TWinControl(Sender).Parent).Caption := doc.title;
bar.Panels.Items[0].Text:='City Public Computer';
bar.Panels.Items[1].Text:='完成';
end;
end;
procedure Tfrmmain.BrowserNavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
browser: TWebBrowser5;
doc: IHTMLDocument2;
begin
browser := TWebBrowser5(Sender);
doc := browser.Document as IHTMLDocument2;
if doc <> nil then
TTabSheet(TWinControl(Sender).Parent).Caption := doc.title;
end;
procedure Tfrmmain.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
var
Tab: TTabSheet;
I: Integer;
begin
if (Msg.message = WM_CLOSE) then
begin
Tab := pgMain.Pages[pgMain.ActivePageIndex];
for I:=0 to Tab.ControlCount-1 do
begin
if Tab.Controls is TWebBrowser5 then
begin
if Msg.hwnd = TWebBrowser5(Tab.Controls).Handle then
PeekMessage(Msg,Msg.Hwnd,0,0,PM_REMOVE);
Handled := True;
break;
end;
end;
end;
end;
procedure Tfrmmain.btnrefreshClick(Sender: TObject);
var
ABrowser: TWebBrowser5;
I: Integer;
begin
if pgmain.PageCount>0 then
begin
for I:=0 to pgMain.Pages[pgmain.ActivePageIndex].ControlCount-1 do
begin
if pgMain.Pages[pgmain.ActivePageIndex].Controls is TWebBrowser5 then
begin
ABrowser := pgMain.Pages[pgmain.ActivePageIndex].Controls as TWebBrowser5;
try
ABrowser.Refresh2;
except
end;
end;
end;
end;
end;
procedure Tfrmmain.btnbackClick(Sender: TObject);
var
ABrowser: TWebBrowser5;
I: Integer;
begin
if pgmain.PageCount>0 then
begin
for I:=0 to pgMain.Pages[pgmain.ActivePageIndex].ControlCount-1 do
begin
if pgMain.Pages[pgmain.ActivePageIndex].Controls is TWebBrowser5 then
begin
ABrowser := pgMain.Pages[pgmain.ActivePageIndex].Controls as TWebBrowser5;
try
ABrowser.GoBack;
except
end;
end;
end;
end;
end;
procedure Tfrmmain.btnforedClick(Sender: TObject);
var
ABrowser: TWebBrowser5;
I: Integer;
begin
if pgmain.PageCount>0 then
begin
for I:=0 to pgMain.Pages[pgmain.ActivePageIndex].ControlCount-1 do
begin
if pgMain.Pages[pgmain.ActivePageIndex].Controls is TWebBrowser5 then
begin
ABrowser := pgMain.Pages[pgmain.ActivePageIndex].Controls as TWebBrowser5;
try
ABrowser.GoForward;
except
end;
end;
end;
end;
end;
end.