前言
WebBrowser 作为Delphi 常见的网络浏览控件,我这里整理了一些它的基本操作,遇到了一些问题,我梳理了一下并给出解决方案
基本操作
WebBrowser1.GoHome; //到浏览器默认主页
WebBrowser1.Refresh; //刷新
WebBrowser1.GoBack; //后退
WebBrowser1.GoForward; //前进
WebBrowser1.Navigate(‘…’); //打开指定页面
WebBrowser1.Navigate(‘about:blank’); //打开空页面WebBrowser1.Navigate(‘about:
标题> 页面内容’);//打开空页面, 并写入…
常见问题与解决方案
读取网页脚本中的变量
假如网页中有这样的语句:
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;?
调用网页脚本中的函数
假如有这样的脚本:
procedure TForm1.Button1Click(Sender: TObject);?
begin?
WebBrowser1.OleObject.document.parentWindow.MB(); //HTML-Js?//如需指定脚本语言, 需要:?
WebBrowser1.OleObject.document.parentWindow.execScript('MB()','JavaScript'); //HTML-Js?
end;?
判断网页及内部框架网页是否全部下载完毕
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 标签的对象
假如网页中有这样的内容:
这是 span1 标签中的内容
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;
获取网页源代码
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;
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
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
beginwebbrowser1.Navigate(edit1.Text);end;procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
i:integer;
begin
for i := 0 to Webbrowser1.OleObject.Document.links.Length - 1 do
Listbox1.Items.Add(Webbrowser1.OleObject.Document.Links.Item(i));
end;end.
获取网页的验证码
方式1:
procedure TForm1.Button1Click(Sender: TObject);
var
ms:TMemoryStream;
jpg:TJpegImage;
vp:string;
begin
ms:=TMemoryStream.Create;
jpg:=TJpegimage.Create;
vp:='http://ptlogin2.qq.com/getimage?aid=22';
//vp:='http://www.0zer.cn/function/c_validcode.asp?name=loginvalid';
IdHTTP1.Get(vp,ms);
ms.Position:=0;
jpg.LoadFromStream(ms);
image1.Picture.Assign(jpg);ms.Free;
jpg.free;END;
方式2:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls, jpeg, GIFImage, WinInet; type TForm1 = class(TForm) IdHTTP1: TIdHTTP; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
const // url = 'http://ptlogin2.qq.com/getimage?aid=15000701&0.9129723031485226'; url='http://www.0zer.cn/function/c_validcode.asp?name=loginvalid';
var ms: TMemoryStream; //jpg: TJPEGImage; te: TGIFImage;
begin ms := TMemoryStream.Create; //jpg := TJPEGImage.Create; te:=TGIFImage.Create; if InternetGetConnectedState(nil,0) then //检测计算机是否联网需要引用WinInet begin IdHTTP1.Get(url, ms); ms.Position := 0; //jpg.LoadFromStream(ms); te.LoadFromStream(ms); Canvas.Draw(10, 10, te); end else begin ShowMessage('错误'); //jpg.Free; te.Free; ms.Free; end;
end;
end.