对TWebBrowser编程时出错(200分)

  • 主题发起人 主题发起人 ln_jinsong
  • 开始时间 开始时间
L

ln_jinsong

Unregistered / Unconfirmed
GUEST, unregistred user!
动态创建一个TTabSheet在此页上动态创建一个TWebBrowser,开始使用时没问题,大约
十多分钟后,出现非法操作,提示访问非法地址。
 
怎么没人回答,嫌分少?可以再加150分,(我可是倾尽所有了!)
 
太少了,贴点代码出来
 
动态创建?好危险哟!不如预先放几个:)
 
好吧,代码如下:请各位大侠指教,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.
 
代码也贴上了。
请大家多帮忙呀,我还有100分,都加上了,快来取呀! 
 
for i:=0 to cbaddress.Items.Count do
begin
if cbaddress.text = cbaddress.Items.Strings then
break;
end;
這段有問題,應該是 for i:=0 to cbaddress.Items.Count-1 do

再來,請教一下, TabSheet 釋放時,會不會連其下 Child(非TabSheet 自動產生的)
一併釋放?
 
To :jiichen
谢谢你的参与,我想主要问题不在添加地址处,可能还在动态创建TWebBrowser的地方。
对你的“TabSheet 釋放時,會不會連其下 Child(非TabSheet 自動產生的)
一併釋放?”问题,应该能一併釋放吧,再请教请教高手吧。
 
procedure Tfrmmain.btngotoClick(Sender: TObject);
var
ATab: TTabSheet;
i:integer;
ABrowser: TWebBrowser5;
begin


ATab := TTabSheet.Create(Application);

atab.parentwindow := pgmain.handle;

ATab.PageControl := pgMain;
ATab.Caption := 'New'+IntToStr(pgMain.PageCount);
ABrowser := TWebBrowser5.Create(ATab);

abrowser.parentwindow:= atab.handle;
abrowser.visible := true;

//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;
 
出现问题的可能性很多.
我认为你先把 application.Terminate; 改成 form1.close;
比较好,我也因为这个崩溃过.

------------------------------------------------
http://www3.msstudio.net/xuqian
 
我曾经也有这种问题,是因几个TTabSheet中打开同一组网页后出现非法,我也正在想办法呢!
 
接受答案了.
 
后退
顶部