150分求助 TWebBrowser 问题: 用 PageControl + TabSheet + WebBrowser做浏览器,前进后退按纽在程序中要如何解局

D

djrj

Unregistered / Unconfirmed
GUEST, unregistred user!
150分求助 TWebBrowser 问题: 用 PageControl + TabSheet + WebBrowser做浏览器,前进后退按纽在程序中要如何解局决?(100分)<br />我用 PageControl + TabSheet + WebBrowser做一个类似tencent的浏览器,
所有TabSheet和WebBrowser控件都在运行时动态创建。
1、程序用一个记录类型的数组来跟踪所有WebBrowser.
如下
//定义记录类型
WebType = Record
Web : TWebBrowser;
Url : String;
StatusTex : String;
HistoryList : TStringList;//用来登记访问过的网页地址列表
HistoryIndex : Integer;//用来指明当前访问网页地址(在初始值为-1)
……
var
Form1 : TForm1;
//下面定义一个记录类型的数组,
//数组长度在程序启动时通过SetLength(MAX)来指定最多可打开的网页数
//其中MAX是我预定义的一个常量
WebArray : array of WebType;
……
implementation
{$R *.dfm}
……
2、以下为动态创建TabSheet和WebBrowser:
var
NewTab : TTabSheet;
NewWeb : TWebBrowser;
CurrentTabIndex : Integer;
begin
NewTab:=TTabSheet.Create(self);
NewTab.PageControl := self.page;
NewTab.Parent:=self.page;
NewWeb:=TWebBrowser.Create(self);
NewTab.InsertControl(NewWeb);
NewWeb.Align := alclient;
WebArray[CurrentTabIndex].Web := NewWeb;
WebArray[CurrentTabIndex].HistoryIndex := WebArray[CurrentTabIndex].HistoryIndex+1;
WebArray[CurrentTabIndex].HistoryList:=TStringList.Create;
WebArray[CurrentTabIndex].Web.Navigate(Urls.Text);
WebArray[CurrentTabIndex].HistoryList.Add(Urls.Text);
WebArray[CurrentTabIndex].HistoryIndex :=CurrentTabIndex;
ppDisp:=NewWeb.Application ;
Cancel:=false;
//执行相关事件
WebArray[CurrentTabIndex].Web.OnTitleChange := Form1.WebTitleChange ;
WebArray[CurrentTabIndex].Web.OnProgressChange := Form1.WebProgressChange ;
WebArray[CurrentTabIndex].Web.OnDocumentComplete := Form1.WebDocumentComplete ;
WebArray[CurrentTabIndex].Web.OnNewWindow2 := Form1.WebNewWindow2 ;
WebArray[CurrentTabIndex].Web.OnBeforeNavigate2 := Form1.WebBeforeNavigate2 ;
end;
3、我的程序用以上代码能正常执行(包括网页弹出的新窗口,在新窗口中打开网页等
均会在我的浏览器中打开,也就是说执行过程中不会弹出任何的IE窗口)。
---------------------------------------------------------------------
但当我用下面的代码想对前进后退按纽进行控制时却不能正常执行:
procedure TForm1.WebBeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
var
CurrentTabIndex,NewHistoryIndex : integer;
begin
CurrentTabIndex:= Page.ActivePageIndex;
NewHistoryIndex:=WebArray[CurrentTabIndex].HistoryList.IndexOf(URL);
if NewHistoryIndex = -1 then //若是个新的址址则加到地址列表中
begin
if (WebArray[CurrentTabIndex].HistoryIndex>=0) and (WebArray[CurrentTabIndex].HistoryIndex < WebArray[CurrentTabIndex].HistoryList.Count - 1 ) then
while WebArray[CurrentTabIndex].HistoryList.Count > WebArray[CurrentTabIndex].HistoryIndex do
WebArray[CurrentTabIndex].HistoryList.Delete(WebArray[CurrentTabIndex].HistoryIndex);
WebArray[CurrentTabIndex].HistoryList.Add(URL);
WebArray[CurrentTabIndex].HistoryIndex:= WebArray[CurrentTabIndex].HistoryIndex+1 ;
end
else WebArray[CurrentTabIndex].HistoryIndex := NewHistoryIndex ;
//以下为重置前进后退按纽的状态
if WebArray[CurrentTabIndex].HistoryIndex >0 then Form1.BackBtn.Enabled := true;
if WebArray[CurrentTabIndex].HistoryIndex <=0 then Form1.BackBtn.Enabled := false;
if (WebArray[CurrentTabIndex].HistoryIndex < (WebArray[CurrentTabIndex].HistoryList.Count -1)) then Form1.PreviousBtn.Enabled :=true
else Form1.PreviousBtn.Enabled :=false;
//以下为重置状态
WebArray[CurrentTabIndex].Url := URL;
Urls.Text := URL ;
WebArray[CurrentTabIndex].StatusTex := '正在打开网页...'+ Urls.Text;
StatusBar1.Panels[0].Text:='正在打开网页...'+String(URL);
end;

因为在打开一个网页时(如www.163.com),WebBrowser的WebBeforeNavigate2事件会执行多次,
导致无法正常控制按纽的状态,也导到地址列表中有多个无用的地址。
--------------------------------------------------------------
若能觖决问题可以再送50分。本人向来康慨。从不食言。
 
没人理吗?呜……呜……
上面的问题我自己已经解决了(谢谢上帝!^_^)
但现在又有一个新的问题要求助:(-_-……)
1、PageControl上的TabSheet如何响应鼠标的双击事件???(我要像tencent一样双击时关闭当前浏览窗口)
2、当关闭当前窗口时要如何释放资源???(包括相应的Tabsheet和WebBrowser控件)

在线等待!解决了就给分。
 
求求各位帮小弟解决下面的问题啊,小弟感激不尽。(在线等待)
1、PageControl上的TabSheet如何响应鼠标的双击事件???(我要像tencent一样双击时关闭当前浏览窗口)
2、我的程序启动后不管什么时候,只要点了窗口右上角的“关闭”,就会出错,并出来下面的提示:
Project Browser325.exe raised exception class EInvalidPointer with
message 'Invalid pointer operation'.Process stopped. Use Step or
Run to Continue
这到底是怎么回事呀?在程序中我根本就没有用任何的指针啊!!!谁能帮我????
 
现在只剩下这个问题了:
PageControl上的TabSheet如何响应鼠标的双击事件???(我要像tencent浏览器一样双击时关闭当前浏览窗口),高手就帮小弟一把啊。
 
出错,说明你的程序有问题。显然是“无效的指针操作”。
这可能和以下语句有关:
NewWeb:=TWebBrowser.Create(self);
NewTab.InsertControl(NewWeb);
/////////////// 到底谁负责内存释放????????

我觉得你有点画蛇添足,TWebBrowser 本身就有这些方法:GoBack、GoForward。
为什么还要自己处理?

 
呵呵, 从上往下看了一遍,都是自己解决的嘛,还要上来提问做什么?[:D]

>>PageControl上的TabSheet如何响应鼠标的双击事件???
很简单, 我把我以前写的代码贴给你:
private
FOldTabControlWndProc: TWndMethod;
procedure TabControlWndProc(var Message: TMessage);

procedure TfrmMain.FormCreate(Sender: TObject);
begin
FOldTabControlWndProc := TabControl1.WindowProc;
TabControl1.WindowProc := TabControlWndProc;
end;

procedure TfrmMain.TabControlWndProc(var Message: TMessage);
begin
if FCloseWinOnDblClick and (Message.Msg = WM_LBUTTONDBLCLK) then
WindowClose.Execute //这里关闭你的TabSheet
else
FOldTabControlWndProc(Message);
end;

 
可以实现在PageControl上加一个panel,让Panel充满每个页。Panel有相应双击的事件。
然后
procedure TForm1.Panel1Click(Sender: TObject);
begin
PageControl1.ActivePage.Free;
end;
因为PageControl没有OnDblClick事件,所以在上面加个panel,试试吧,看行不行。
 
接受答案了.
 
顶部