TWebBrowser 与 MSHTML(1): 从 TWebBrowser 获取 DOM 中的 window 对象 借助 TWebBrowser 可以把软件做的更漂亮、更灵活, 很多软件已经或者早就这样做了.尽管 HTML DOM 内容繁杂(涉及到 HTML、JavaScript、CSS), 但...
TWebBrowser 与 MSHTML(1): 从 TWebBrowser 获取 DOM 中的 window 对象
借助 TWebBrowser 可以把软件做的更漂亮、更灵活, 很多软件已经或者早就这样做了.
尽管 HTML DOM 内容繁杂(涉及到 HTML、JavaScript、CSS), 但也都属于 TWebBrowser 的功能范围.
使用 TWebBrowser 时, 如果配合上 MSHTML, 将会有很好的代码提示; 不然也可以, 就是写代码困难.
HTML DOM 中的一切都是源于一个叫 window 对象, 为了和 JS 中的 DOM 一致起来, 本次先获取这个对象.
TWebBrowser 是调用 SHDocVw.dll 的功能并继承与 TOleControl 类, 会用到 SHDocVw 和 OleCtrls 单元.
另外 window 是 IHTMLWindow2 接口类型的, 此接口定义在 MSHTML 单元.
本例效果图:

代码文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses MSHTML;
var window: IHTMLWindow2;
{打开一个页面}
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('http://del.cnblogs.com');
end;
{获取并试用 window 对象}
procedure TForm1.Button2Click(Sender: TObject);
begin
window := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
window.document.body.innerHTML := '<hr>万一的 Delphi 博客<hr>';
window.alert('JavaScript 的提示: 修改成功!');
end;
{初始化}
procedure TForm1.FormCreate(Sender: TObject);
begin
Position := poScreenCenter;
WebBrowser1.Align := alTop;
Button1.Caption := '打开';
Button2.Caption := '改写';
end;
end.
窗体文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 167
ClientWidth = 318
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object WebBrowser1: TWebBrowser
Left = 8
Top = 8
Width = 300
Height = 121
TabOrder = 0
ControlData = {
4C000000021F0000810C00000000000000000000000000000000000000000000
000000004C000000000000000000000001000000E0D057007335CF11AE690800
2B2E126208000000000000004C0000000114020000000000C000000000000046
8000000000000000000000000000000000000000000000000000000000000000
00000000000000000100000000000000000000000000000000000000}
end
object Button1: TButton
Left = 154
Top = 135
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 235
Top = 135
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
end
从一个外部的 IE 窗口获取 window 对象也可以:
uses MSHTML, ComObj;
var
IE: Variant;
window: IHTMLWindow2;
procedure TForm1.Button1Click(Sender: TObject);
begin
IE := CreateOleObject('InternetExplorer.Application');
IE.Visible := True;
IE.Navigate('http://del.cnblogs.com');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
window := (IDispatch(IE.document) as IHTMLDocument2).parentWindow;
window.document.body.innerHTML := '<hr>万一的 Delphi 博客<hr>';
end;
posted on
2009-03-12 00:20?
万一?
阅读(7759)?
评论(5)?
编辑?
收藏
沃梦达教程
本文标题为:TWebBrowser 与 MSHTML(1): 从 TWebBrowser 获取 DOM 中的 window 对象
猜你喜欢
- 如何通过双击Windows XP中的html文件之类的任何文件夹来运行php文件? 2023-10-25
- 微信小程序实现发动态功能的示例代码 2022-10-21
- 使用React.forwardRef传递泛型参数 2023-07-09
- linux – 如何使用没有html的wget获取页面文本? 2023-10-25
- JavaScript中的预解析你了解吗 2023-07-09
- Ajax引擎 ajax请求步骤详细代码 2023-02-23
- 使用Ajax方法实现Form表单的提交及注意事项 2023-02-14
- layui获取select下面的选项值和value值 2023-10-08
- 微信小程序实现文章关注功能详细流程 2022-08-30
- 解决HTML5手机端页面缩放的问题 2022-09-16
