本文整理自网络,侵删。
要在同一个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;
相关阅读 >>
Delphi10.3构造一个json数据的第二种方法,并格式化输出
Delphi removewhitespaces 过滤字符串所有空格
更多相关阅读请进入《Delphi》频道 >>