天天看點

Delphi WebBrowser浏覽器的制作方法

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;
<p>假如網頁中有這樣的語句:</p><p></p>

--------------------------------------------------------------------------------

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

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

  //如需指定腳本語言, 需要:
  WebBrowser1.OleObject.document.parentWindow.execScript('MB()','JavaScript'); //HTML-Js
end;
<p>假如有這樣的腳本:</p><p></p>

--------------------------------------------------------------------------------

//判斷網頁及内部架構網頁是否全部下載下傳完畢

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.getElementByID('span1').innerText;
  ShowMessage(s);  //這是 span1 标簽中的内容

  //或者:
  s := WebBrowser1.OleObject.document.parentWindow.span1.innerText;
  ShowMessage(s);  //這是 span1 标簽中的内容

  //隐藏它:
  WebBrowser1.OleObject.document.parentWindow.span1.style.display := 'none';
end;
<p>假如網頁中有這樣的内容:<span id="span1">這是 span1 标簽中的内容</span></p><p> </p>
//提取網頁所有連結位址

procedure TForm1.Button1Click(Sender: TObject);
var  i: Integer;
begin 
    ListBox1.Clear;  
    for i :=0to WebBrowser1.OleObject.document.anchors.Length -1do   

        ListBox1.Items.Add(WebBrowser1.OleObject.document.anchors.item(i).href);

end;


--------------------------------------------------------------------------------

//擷取網頁源代碼
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;
--------------------------------------------------------------------------------

           

繼續閱讀