delphi webbrowser用法集錦

delphi webbrowser用法集錦

(2012-05-13 08:29:00)
標籤:

it

分類: 軟件_Software
WebBrowser1.GoHome;  //到瀏覽器默認主頁
WebBrowser1.Refresh; //刷新
WebBrowser1.GoBack; //後退
WebBrowser1.GoForward; //前進
WebBrowser1.Navigate('...'); //打開指定頁面
WebBrowser1.Navigate('about:blank'); //打開空頁面

//打開空頁面, 並寫入...

WebBrowser1.Navigate('about:<head><title>標題</title><body>頁面內容</body>');

//讀取網頁腳本中的變量:

procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
i: Integer;
begin
s := WebBrowser1.OleObject.document.Script.str;
i := WebBrowser1.OleObject.document.Script.num;
ShowMessage(s); //Hello
ShowMessage(IntToStr(i)); //99

//也能夠這樣讀:
s := WebBrowser1.OleObject.document.parentWindow.str;
i := WebBrowser1.OleObject.document.parentWindow.num;
ShowMessage(s); //Hello
ShowMessage(IntToStr(i)); //99
end;
&amp;amp;lt;br&amp;amp;gt;假如網頁中有這樣的語句:&amp;amp;lt;br&amp;amp;gt;&amp;amp;amp;amp;lt;script&amp;amp;amp;amp;gt;&amp;amp;lt;br&amp;amp;gt;var&amp;amp;lt;br&amp;amp;gt;
  str = 
&amp;amp;quot;Hello&amp;amp;quot;;&amp;amp;lt;br&amp;amp;gt;
  i = 
99;&amp;amp;lt;br&amp;amp;gt;&amp;amp;amp;amp;lt;/script&amp;amp;amp;amp;gt;

//調用網頁腳本中的函數:

procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.OleObject.document.parentWindow.MB(); //HTML-Js

//如需指定腳本語言, 須要:
WebBrowser1.OleObject.document.parentWindow.execScript('MB()','JavaScript'); //HTML-Js
end;
&amp;amp;lt;br&amp;amp;gt;假若有這樣的腳本:&amp;amp;lt;br&amp;amp;gt;&amp;amp;amp;amp;lt;script&amp;amp;amp;amp;gt;&amp;amp;lt;br&amp;amp;gt;function
 MB(){ &amp;amp;lt;br&amp;amp;gt;  
alert('HTML-Js');&amp;amp;lt;br&amp;amp;gt;}&amp;amp;lt;br&amp;amp;gt;&amp;amp;amp;amp;lt;/script&amp;amp;amp;amp;gt;

//判斷網頁及內部框架網頁是否所有下載完畢

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if WebBrowser1.Application = pDisp then
begin
Text := '網頁下載完畢!';
end;
end;

//改變背景色或背景圖片:
WebBrowser1.OleObject.document.body.bgcolor := '#FF0000';
WebBrowser1.OleObject.document.body.background := '...圖片地址';

//操做有 ID 標籤的對象:
var
s: string;
begin
s := WebBrowser1.OleObject.document.getElementByIdx_x('span1').innerText;
ShowMessage(s); //這是 span1 標籤中的內容

//或者:
s := WebBrowser1.OleObject.document.parentWindow.span1.innerText;
ShowMessage(s); //這是 span1 標籤中的內容

//隱藏它:
WebBrowser1.OleObject.document.parentWindow.span1.style.display := 'none';
end;
&amp;amp;lt;br&amp;amp;gt;假如網頁中有這樣的內容:&amp;amp;lt;br&amp;amp;gt;&amp;amp;amp;amp;lt;span
 id=span1&amp;amp;amp;amp;gt;這是 span1 
標籤中的內容&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;lt;br&amp;amp;gt;

//獲取網頁源代碼
var
s: string;
begin
s := WebBrowser1.OleObject.document.body.innerHTML; //body內的全部代碼
s := WebBrowser1.OleObject.document.body.outerHTML; //body內的全部代碼, 包含body標籤
s := WebBrowser1.OleObject.document.documentElement.innerHTML; //html內的全部代碼
end;

//獲取網頁所有源代碼
uses ActiveX;
var
ms: TMemoryStream;
begin
if not Assigned(WebBrowser1.Document) then Exit;
ms := TMemoryStream.Create;
(WebBrowser1.Document as IPersistStreamInit).Save(TStreamAdapter.Create(ms), True);
ms.Position := 0;
Memo1.Lines.LoadFromStream(ms, TEncoding.UTF8);
// Memo1.Lines.LoadFromStream(ms, TEncoding.Default); {GB2312 等雙字節}
ms.Free;
end;

//WebBrowser 中的右鍵菜單

//先要添加ApplicationEvents1,指定其Message事件

//屏蔽右鍵菜單
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
begin
with Msg do
begin
if not IsChild(WebBrowser1.Handle, hWnd) then Exit;
Handled := (message = WM_RBUTTONDOWN) or (message = WM_RBUTTONUP) or (message = WM_CONTEXTMENU);
end;
end;

//替換右鍵菜單
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var mPoint: TPoint;
begin
if IsChild(WebBrowser1.Handle, Msg.Hwnd) and
((Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONUP)) then
begin
GetCursorPos(mPoint); //獲得光標位置
PopupMenu1.Popup(mPoint.X, mPoint.Y); //彈出popupmenu1的菜單
Handled:=True;
end;
end;

//新頁面寫入
begin
WebBrowser1.Navigate('about:blank');
WebBrowser1.OleObject.Document.Writeln('ok');
end;

//從流中寫入:
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
Memo1.Lines.SaveToStream(ms);
ms.Position := 0;
(WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
ms.Free;
end;

//禁止提示腳步錯誤
procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
WebBrowser1.Silent := True;
end;

//禁止彈出窗口
procedure TForm1.WebBrowser1NewWindow2(ASender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool);
begin
Cancel := True;
end;

注: 出處 http://blog.sina.com.cn/s/blog_4ad042e50102e0z0.html
相關文章
相關標籤/搜索