delphi 怎么判断webbrowser打开网页成功?
2个回答
展开全部
在同一个WebBrowser里显示,可以这样:
再放一个小的WebBrowser2,设置它在WebBrowser1下面(设置Visible为False好象无效),
// 在WebBrowser1的OnNewWindow2事件中:
procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
ppDisp := WebBrowser2.Application; // 新的窗口先指向WebBrowser2
end;
// 在WebBrowser2的OnBeforeNavigate2事件中:
procedure TForm1.WebBrowser2BeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
begin
WebBrowser1.Navigate(string(URL)); // 再指回WebBrowser1
Cancel := True;
end;
可以这样,窗体中放置两个PANEL,其中PANEL1中放WebBrowser1,PANEL2中放WebBrowser2,PANEL1和PANEL2、WebBrowser1和WebBrowser2的LEFT,TOP等所有属性设为一样。PANEL2的VISIBLE设为FALSE。然后添加代码如下即可:
procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
ppDisp:=WebBrowser2.Application;
panel1.Visible:=false;
panel2.Visible:=true;
end;
procedure TForm1.WebBrowser2NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
ppDisp:=WebBrowser1.Application;
panel2.Visible:=false;
panel1.Visible:=true
我只回答第一个问题,看你给多少分:
1 NewWindow2的在接口中定义的原形:
procedure NewWindow2(Sender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
2 通过传递一个WebBrowser的控制借口给ppDisp可以让新网页在指定的Wb中浏览
ppDisp:=WebBrowser1.Browser.ControlInterface;
3 在刚才传递的WebBrowser的OnBeforeNavigate2事件中就可以获得将要打开的
URL了!
function SaveToStream(AStream: TStream): HRESULT;
var
IpStream: IPersistStreamInit;
begin
with browser1 do
begin
while ReadyState <> READYSTATE_COMPLETE do
Forms.Application.ProcessMessages;
if Assigned(Document) then
begin
IpStream := Document as IPersistStreamInit;
Result := IpStream.save(TStreamAdapter.Create(AStream), TRUE);
end else Result := S_FALSE;
end;
end;
function Loadfromstream(AStream:TStream):hresult;
begin
with browser1 do
begin
if not Assigned(Document) then
begin
navigate2('about:blank',0,0,0,0);
while readystate <> READYSTATE_COMPLETE do Forms.Application.ProcessMessages;
end;
AStream.seek(0, 0);
Result := (Document as IPersistStreamInit).Load(TStreamadapter.Create(AStream));
end;
end;
再放一个小的WebBrowser2,设置它在WebBrowser1下面(设置Visible为False好象无效),
// 在WebBrowser1的OnNewWindow2事件中:
procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
ppDisp := WebBrowser2.Application; // 新的窗口先指向WebBrowser2
end;
// 在WebBrowser2的OnBeforeNavigate2事件中:
procedure TForm1.WebBrowser2BeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
begin
WebBrowser1.Navigate(string(URL)); // 再指回WebBrowser1
Cancel := True;
end;
可以这样,窗体中放置两个PANEL,其中PANEL1中放WebBrowser1,PANEL2中放WebBrowser2,PANEL1和PANEL2、WebBrowser1和WebBrowser2的LEFT,TOP等所有属性设为一样。PANEL2的VISIBLE设为FALSE。然后添加代码如下即可:
procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
ppDisp:=WebBrowser2.Application;
panel1.Visible:=false;
panel2.Visible:=true;
end;
procedure TForm1.WebBrowser2NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
ppDisp:=WebBrowser1.Application;
panel2.Visible:=false;
panel1.Visible:=true
我只回答第一个问题,看你给多少分:
1 NewWindow2的在接口中定义的原形:
procedure NewWindow2(Sender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
2 通过传递一个WebBrowser的控制借口给ppDisp可以让新网页在指定的Wb中浏览
ppDisp:=WebBrowser1.Browser.ControlInterface;
3 在刚才传递的WebBrowser的OnBeforeNavigate2事件中就可以获得将要打开的
URL了!
function SaveToStream(AStream: TStream): HRESULT;
var
IpStream: IPersistStreamInit;
begin
with browser1 do
begin
while ReadyState <> READYSTATE_COMPLETE do
Forms.Application.ProcessMessages;
if Assigned(Document) then
begin
IpStream := Document as IPersistStreamInit;
Result := IpStream.save(TStreamAdapter.Create(AStream), TRUE);
end else Result := S_FALSE;
end;
end;
function Loadfromstream(AStream:TStream):hresult;
begin
with browser1 do
begin
if not Assigned(Document) then
begin
navigate2('about:blank',0,0,0,0);
while readystate <> READYSTATE_COMPLETE do Forms.Application.ProcessMessages;
end;
AStream.seek(0, 0);
Result := (Document as IPersistStreamInit).Load(TStreamadapter.Create(AStream));
end;
end;
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure WebBrowser1DownloadBegin(Sender: TObject);
procedure WebBrowser1DownloadComplete(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
form1.WebBrowser1.Navigate('www.ivytc.com');
end;
procedure TForm1.WebBrowser1DownloadBegin(Sender: TObject);
begin
edit1.Text :='开始';
end;
procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
begin
edit1.Text :='完成打开';
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure WebBrowser1DownloadBegin(Sender: TObject);
procedure WebBrowser1DownloadComplete(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
form1.WebBrowser1.Navigate('www.ivytc.com');
end;
procedure TForm1.WebBrowser1DownloadBegin(Sender: TObject);
begin
edit1.Text :='开始';
end;
procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
begin
edit1.Text :='完成打开';
end;
end.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询