要在同一個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;