来自:阿狗, 时间:2005-6-57:41:23, ID:3094647
这个问题也曾经困扰过我一段时间啊!呵呵……
给你一个解决办法,肯定能成功!
procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel:WordBool);
begin
Cancel:=true;
WebBrowser1.Navigate (NewURL);
end;
其中的NewURL是你在网页中点击的链接,它的值在以下事件中取得:
procedure TForm1.WebBrowser1StatusTextChange(Sender: TObject;
const Text: WideString);
begin
NewURL :=text;
end;
这样,当你点击网页中的连接时,就在本窗口中打开.NewURL应定义为全局变量.实现不了找我哦!
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pucoov/archive/2006/02/04/591806.aspx
Delphi中始终用WebBrowser打开网页
Delphi 2010-01-2613:38:17 阅读172评论0 字号:大中小
要在同一个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 asIPersistStreamInit).Load(TStreamadapter.Create(AStream)); end; end; |