1.WebBrowser 操作记要
WebBrowser1.GoHome; //到浏览器默认主页 WebBrowser1.Refresh; //刷新WebBrowser1.GoBack; //后退 WebBrowser1.GoForward; //前进WebBrowser1.Navigate('...'); //打开指定页面 WebBrowser1.Navigate('about:blank'); //打开空页面
2.//打开空页面, 并写入...
WebBrowser1.Navigate('about:<head><title>标题</title><body>页面内容</body>');
3.
//判断网页及内部框架网页是否全部下载完毕
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if WebBrowser1.Application = pDisp then
begin
Text := '网页下载完毕!';
end;
end;
4.
//改变背景色或背景图片:
WebBrowser1.OleObject.document.body.bgcolor := '#FF0000';
WebBrowser1.OleObject.document.body.background := '...图片地址';
5.
//获取网页源代码
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;
6.
//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;
7
//新页面写入
begin WebBrowser1.Navigate('about:blank'); WebBrowser1.OleObject.Document.Writeln('ok');end;
8.
//禁止提示脚步错误
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;
9.
从 TWebBrowser 获取 DOM 中的 window 对象
10.获取 window 对象的时机
{当页面打开完毕时获取 window 对象}
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
if pDisp = (ASender as TWebBrowser).Application then
begin window := ((ASender as TWebBrowser).Document as IHTMLDocument2).parentWindow;
end;
end;
10.
使用 TWebBrowser 编辑网页
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(WebBrowser1.OleObject.document.body.innerText);
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin {这里是让整个页面可编辑, 也可以单独编辑某个元素} WebBrowser1.OleObject.document.body.contentEditable := CheckBox1.Checked; {有些浏览器需要这样设置: document.designMode = "On";}
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Panel1.Align := alTop;
CheckBox1.Anchors := [akTop, akRight];
Button1.Anchors := CheckBox1.Anchors;
CheckBox1.Caption := '编辑'; Button1.Caption := '显示网页文本';
WebBrowser1.Align := alClient; WebBrowser1.Navigate('http://www.cnblogs.com/del/archive/2009/04/16/1437417.html');
end;
11.
如何放缩 TWebBrowser 中的页面
const OLECMDID_OPTICAL_ZOOM = 63;
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('http://del.cnblogs.com');
end;
procedure TForm1.Button1Click(Sender: TObject);
var ZoomIn: OleVariant;
begin
ZoomIn := 150;
WebBrowser1.ExecWB(OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DODEFAULT, ZoomIn);
end;