(该代码来自国外网站, 给 "神奇的科比" 参考)



代码:


unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, OleCtrls, SHDocVw, MSHTML, ActiveX, StdCtrls;typeTObjectProcedure = procedure of object;TEventObject = class(TInterfacedObject, IDispatch)privateFOnEvent: TObjectProcedure;protectedfunction GetTypeInfoCount(out Count: Integer): HResult; stdcall;function GetTypeInfo(index, LocaleID: Integer; out TypeInfo): HResult; stdcall;function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer;DispIDs: Pointer): HResult; stdcall;function Invoke(dispid: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word;var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;publicconstructor Create(const OnEvent: TObjectProcedure);property OnEvent: TObjectProcedure read FOnEvent write FOnEvent;end;TForm1 = class(TForm)WebBrowser1: TWebBrowser;Memo1: TMemo;procedure WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;var Cancel: WordBool);procedure WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;var URL: OleVariant);procedure FormCreate(Sender: TObject);privateprocedure Document_OnMouseOver;public{ Public declarations }end;varForm1: TForm1;htmlDoc: IHTMLDocument2;implementation{$R *.dfm}procedure TForm1.Document_OnMouseOver;varelement: IHTMLElement;beginif htmlDoc = nil thenExit;element := htmlDoc.parentWindow.event.srcElement;Memo1.Clear;if LowerCase(element.tagName) = 'a' thenbeginMemo1.Lines.Add('LINK info...');Memo1.Lines.Add(Format('HREF : %s', [element.getAttribute('href', 0)]));endelse if LowerCase(element.tagName) = 'img' thenbeginMemo1.Lines.Add('IMAGE info...');Memo1.Lines.Add(Format('SRC : %s', [element.getAttribute('src', 0)]));endelsebeginMemo1.Lines.Add(Format('TAG : %s', [element.tagName]));end;end; (* Document_OnMouseOver *)procedure TForm1.FormCreate(Sender: TObject);beginWebBrowser1.Navigate('http://del.cnblogs.com');Memo1.Clear;Memo1.Lines.Add('Move your mouse over the document...');end; (* FormCreate *)procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);beginhtmlDoc := nil;end; (* WebBrowser1BeforeNavigate2 *)procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;var URL: OleVariant);beginif Assigned(WebBrowser1.Document) thenbeginhtmlDoc := WebBrowser1.Document as IHTMLDocument2;htmlDoc.onmouseover := (TEventObject.Create(Document_OnMouseOver) as IDispatch);end;end; (* WebBrowser1DocumentComplete *){ TEventObject }constructor TEventObject.Create(const OnEvent: TObjectProcedure);begininherited Create;FOnEvent := OnEvent;end;function TEventObject.GetIDsOfNames(const IID: TGUID; Names: Pointer;NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;beginResult := E_NOTIMPL;end;function TEventObject.GetTypeInfo(index, LocaleID: Integer; out TypeInfo): HResult;beginResult := E_NOTIMPL;end;function TEventObject.GetTypeInfoCount(out Count: Integer): HResult;beginResult := E_NOTIMPL;end;function TEventObject.Invoke(dispid: Integer; const IID: TGUID; LocaleID: Integer;Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;beginif (dispid = DISPID_VALUE) thenbeginif Assigned(FOnEvent) thenFOnEvent;Result := S_OK;endelseResult := E_NOTIMPL;end;end.

窗体:


object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 375ClientWidth = 643Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalseOnCreate = FormCreatePixelsPerInch = 96TextHeight = 13object WebBrowser1: TWebBrowserLeft = 0Top = 73Width = 643Height = 302Align = alClientTabOrder = 0OnBeforeNavigate2 = WebBrowser1BeforeNavigate2OnDocumentComplete = WebBrowser1DocumentCompleteExplicitLeft = 264ExplicitTop = 200ExplicitWidth = 300ExplicitHeight = 150ControlData = {4C00000075420000361F00000000000000000000000000000000000000000000000000004C000000000000000000000001000000E0D057007335CF11AE6908002B2E126208000000000000004C0000000114020000000000C000000000000046800000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000}endobject Memo1: TMemoLeft = 0Top = 0Width = 643Height = 73Align = alTopLines.Strings = ('Memo1')TabOrder = 1end
end

给 "神奇的科比" 改的识别第一个框架的代码:


unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, OleCtrls, SHDocVw, MSHTML, ActiveX, StdCtrls;typeTObjectProcedure = procedure of object;TEventObject = class(TInterfacedObject, IDispatch)privateFOnEvent: TObjectProcedure;protectedfunction GetTypeInfoCount(out Count: Integer): HResult; stdcall;function GetTypeInfo(index, LocaleID: Integer; out TypeInfo): HResult; stdcall;function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer;DispIDs: Pointer): HResult; stdcall;function Invoke(dispid: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word;var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;publicconstructor Create(const OnEvent: TObjectProcedure);property OnEvent: TObjectProcedure read FOnEvent write FOnEvent;end;TForm1 = class(TForm)WebBrowser1: TWebBrowser;Memo1: TMemo;procedure WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;var Cancel: WordBool);procedure WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;var URL: OleVariant);procedure FormCreate(Sender: TObject);privateprocedure Document_OnMouseOver;public{ Public declarations }end;varForm1: TForm1;htmlDoc: IHTMLDocument2;implementation{$R *.dfm}procedure TForm1.Document_OnMouseOver;varelement: IHTMLElement;beginif htmlDoc = nil thenExit;element := htmlDoc.parentWindow.event.srcElement;Memo1.Clear;if LowerCase(element.tagName) = 'a' thenbeginMemo1.Lines.Add('LINK info...');Memo1.Lines.Add(Format('HREF : %s', [element.getAttribute('href', 0)]));endelse if LowerCase(element.tagName) = 'img' thenbeginMemo1.Lines.Add('IMAGE info...');Memo1.Lines.Add(Format('SRC : %s', [element.getAttribute('src', 0)]));endelsebeginMemo1.Lines.Add(Format('TAG : %s', [element.tagName]));end;end; (* Document_OnMouseOver *)procedure TForm1.FormCreate(Sender: TObject);beginWebBrowser1.Navigate('http://passport.csdn.net/UserLogin.aspx');Memo1.Clear;Memo1.Lines.Add('Move your mouse over the document...');end; (* FormCreate *)procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);beginhtmlDoc := nil;end; (* WebBrowser1BeforeNavigate2 *)procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;var URL: OleVariant);beginif Assigned(WebBrowser1.Document) thenbeginhtmlDoc := WebBrowser1.Document as IHTMLDocument2;if htmlDoc.frames.length > 0 thenbeginhtmlDoc := (IDispatch(htmlDoc.frames.item(0)) as IHTMLWindow2).Document;end;htmlDoc.onmouseover := (TEventObject.Create(Document_OnMouseOver) as IDispatch);end;end; (* WebBrowser1DocumentComplete *){ TEventObject }constructor TEventObject.Create(const OnEvent: TObjectProcedure);begininherited Create;FOnEvent := OnEvent;end;function TEventObject.GetIDsOfNames(const IID: TGUID; Names: Pointer;NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;beginResult := E_NOTIMPL;end;function TEventObject.GetTypeInfo(index, LocaleID: Integer; out TypeInfo): HResult;beginResult := E_NOTIMPL;end;function TEventObject.GetTypeInfoCount(out Count: Integer): HResult;beginResult := E_NOTIMPL;end;function TEventObject.Invoke(dispid: Integer; const IID: TGUID; LocaleID: Integer;Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;beginif (dispid = DISPID_VALUE) thenbeginif Assigned(FOnEvent) thenFOnEvent;Result := S_OK;endelseResult := E_NOTIMPL;end;end.

转载于:https://www.cnblogs.com/del/archive/2010/03/04/1678550.html

给 TWebBrowser.Document 定义事件相关推荐

  1. Delphi处理TWebBrowser的Close事件

    当TWebBrowser控件访问的 Web 页上的脚本调用window.close方法时,TWebBrowser控件可能会在窗体中消失.我们的程序应该对此作出反应,否则再次访问TWebBrowser控 ...

  2. dw上按钮事件 pb_「React TS3 专题」使用 TS 的方式在类组件里定义事件

    在「React TS3 专题」亲自动手创建一个类组件,我们一起学习了如何用 TS 的方式在React 里定义类组件(class component)以及了解了什么是 JSX.本篇文章,笔者将带着大家一 ...

  3. C#中在定义事件委托时怎样跨窗体传递参数

    场景 C#中委托与事件的使用-以Winform中跨窗体传值为例: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100150700 ...

  4. Emit学习-进阶篇-定义事件

    Emit学习-进阶篇-定义事件 之前在研究如何用Emit为动态类添加事件,本来以为会非常简单,但是却碰到了许多的问题,有些问题在之前的答疑篇中已经提到了,并予以了解决,虽然有些地方自己也不是很明白,但 ...

  5. 为WebBrowser的WEB页的Document注册事件的问题

    当使用WebBrowser,并对其装载的Web页建立Document的事件后,WebBrowser里的页面元素都变得难于操作了,只响应建立的连接事件.有兴趣的朋友可以在您的代码中使用以下的代码来建立事 ...

  6. winform 代码定义事件

    /// <summary> /// 定义lable超过第一页是否隐藏 /// </summary> /// <param name="sender"& ...

  7. javascript中定义事件的三种方式

    在javascript中,可以为某个元素指定事件,指定的方式有以下三种: 1.在html中,使用onclick属性 2.在javascript中,使用onclick属性 3.在javascipt中,使 ...

  8. 关于jquery的$(document).on()事件多次执行的问题

    我们在使用$(document).on()来绑定事件的时候,有时候会出现同一个事件会执行多次的问题.原因是这段代码在执行的过程中进行了多次绑定, $(document).on("click& ...

  9. vue-自定义事件之—— 子组件修改父组件的值

    如何利用自定义的事件,在子组件中修改父组件里边的值? 关键点记住:三个事件名字 步骤如下: 这里,相对本案例,父组件定义为Second-module,对应的子组件是Three-module 第一步:你 ...

最新文章

  1. oracle数据库 pc6,使用logdump 查看ogg的CSN
  2. 图解利用Auto Deploy 部署ESXi 5.x参考v1.0
  3. 一步步写一个符合Promise/A+规范的库
  4. 【项目实战】基于python行为评分卡模型
  5. 迁移到其他机器_有赞大数据离线集群迁移实战
  6. 清洁责任–摆脱均等,compareTo和toString
  7. 玩转短视频?守护视频安全?AI智能提速?一分钱体验? 阿里云视频点播大招盘点...
  8. 信息技术计算机网络课堂实录,信息技术教案:计算机网络简介
  9. Android开发笔记(八十三)多语言支持
  10. 直接拿来用!超实用的Java数组技巧攻略
  11. select函数的并发限制和 poll 函数应用举例
  12. NYOJ113 - 字符串替换
  13. LA 3027 并查集
  14. 收藏| 一些可供参考的游戏出海技术架构与经验总结
  15. 最新柒上网络小说漫画系统双模板源码V4.0+TP内核
  16. JADE(Java Agent Development Framework)笔记
  17. 化工原理计算机辅助设计,化工原理课程设计心得三篇
  18. mpvue 搭建整理
  19. 就业信息管理系统的设计与实现
  20. 将数字划分为素数的乘积

热门文章

  1. java弹出虚拟键盘_JS实现电脑虚拟键盘的操作
  2. java方法执行的时间_计算Java中任意一个方法的执行时间的工具类
  3. 黑色背景下,计算照片白色的区域面积和周长
  4. ++i与i++的根本性区别(两个代码对比搞定)
  5. 使用ffmpeg的filter处理yuv数据包括split filter(分流)、crop filter(裁剪)、vflip filter(垂直向上的翻转)、overlay filter(合成)
  6. Codeforces Round #381 (Div. 2) Codeforce_740
  7. 15. 三数之和 golang
  8. 88. 合并两个有序数组 golang
  9. int * p =NULL;和*p =NULL的区别a和a的区别
  10. 【剑指offer】_17正则表达式的匹配