• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphiwebbrowser与delphi互动

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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;amp;lt;br&amp;amp;amp;gt;假如网页中有这样的语句:&amp;amp;amp;lt;br&amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;script&amp;amp;amp;amp;amp;gt;&amp;amp;amp;lt;br&amp;amp;amp;gt;var&amp;amp;amp;lt;br&amp;amp;amp;gt; str = &amp;amp;amp;quot;Hello&amp;amp;amp;quot;;&amp;amp;amp;lt;br&amp;amp;amp;gt; i = 99;&amp;amp;amp;lt;br&amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;/script&amp;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;amp;lt;br&amp;amp;amp;gt;假如有这样的脚本:&amp;amp;amp;lt;br&amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;script&amp;amp;amp;amp;amp;gt;&amp;amp;amp;lt;br&amp;amp;amp;gt;function MB(){ &amp;amp;amp;lt;br&amp;amp;amp;gt; alert('HTML-Js');&amp;amp;amp;lt;br&amp;amp;amp;gt;}&amp;amp;amp;lt;br&amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;/script&amp;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.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;
&amp;amp;amp;lt;br&amp;amp;amp;gt;假如网页中有这样的内容:&amp;amp;amp;lt;br&amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;span id=span1&amp;amp;amp;amp;amp;gt;这是 span1 标签中的内容&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;&amp;amp;amp;lt;br&amp;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;

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi动态事件深入分析(转)发布时间:2022-07-18
下一篇:
Delphi将输入汉字自动产生拼音简码发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap