首页 > 其他分享 >给 TWebBrowser.Document 定义事件

给 TWebBrowser.Document 定义事件

时间:2022-10-03 23:00:34浏览次数:45  
标签:begin end 定义 TWebBrowser Integer var const Document Memo1

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



代码:
unit Unit1;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, OleCtrls, SHDocVw, MSHTML, ActiveX, StdCtrls;

 type
   TObjectProcedure = procedure of object;

   TEventObject = class(TInterfacedObject, IDispatch)
   private
     FOnEvent: TObjectProcedure;
   protected
     function 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;
   public
     constructor 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);
   private
     procedure Document_OnMouseOver;
   public
     { Public declarations }
   end;

 var
   Form1: TForm1;
   htmlDoc: IHTMLDocument2;

 implementation

{$R *.dfm}

 procedure TForm1.Document_OnMouseOver;
 var
   element: IHTMLElement;
 begin
   if htmlDoc = nil then
     Exit;
   element := htmlDoc.parentWindow.event.srcElement;
   Memo1.Clear;
   if LowerCase(element.tagName) = 'a' then
   begin
     Memo1.Lines.Add('LINK info...');
     Memo1.Lines.Add(Format('HREF : %s', [element.getAttribute('href', 0)]));
   end
   else if LowerCase(element.tagName) = 'img' then
   begin
     Memo1.Lines.Add('IMAGE info...');
     Memo1.Lines.Add(Format('SRC : %s', [element.getAttribute('src', 0)]));
   end
   else
   begin
     Memo1.Lines.Add(Format('TAG : %s', [element.tagName]));
   end;
 end; (* Document_OnMouseOver *)

 procedure TForm1.FormCreate(Sender: TObject);
 begin
   WebBrowser1.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);
 begin
   htmlDoc := nil;
 end; (* WebBrowser1BeforeNavigate2 *)

 procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;
   var URL: OleVariant);
 begin
   if Assigned(WebBrowser1.Document) then
   begin
     htmlDoc := WebBrowser1.Document as IHTMLDocument2;
     htmlDoc.onmouseover := (TEventObject.Create(Document_OnMouseOver) as IDispatch);
   end;
 end; (* WebBrowser1DocumentComplete *)
 { TEventObject }

 constructor TEventObject.Create(const OnEvent: TObjectProcedure);
 begin
   inherited Create;
   FOnEvent := OnEvent;
 end;

 function TEventObject.GetIDsOfNames(const IID: TGUID; Names: Pointer;
   NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
 begin
   Result := E_NOTIMPL;
 end;

 function TEventObject.GetTypeInfo(index, LocaleID: Integer; out TypeInfo): HResult;
 begin
   Result := E_NOTIMPL;
 end;

 function TEventObject.GetTypeInfoCount(out Count: Integer): HResult;
 begin
   Result := E_NOTIMPL;
 end;

 function TEventObject.Invoke(dispid: Integer; const IID: TGUID; LocaleID: Integer;
   Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
 begin
   if (dispid = DISPID_VALUE) then
   begin
     if Assigned(FOnEvent) then
       FOnEvent;
     Result := S_OK;
   end
   else
     Result := E_NOTIMPL;
 end;

 end.


窗体:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 375
  ClientWidth = 643
  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 = 0
    Top = 73
    Width = 643
    Height = 302
    Align = alClient
    TabOrder = 0
    OnBeforeNavigate2 = WebBrowser1BeforeNavigate2
    OnDocumentComplete = WebBrowser1DocumentComplete
    ExplicitLeft = 264
    ExplicitTop = 200
    ExplicitWidth = 300
    ExplicitHeight = 150
    ControlData = {
      4C00000075420000361F00000000000000000000000000000000000000000000
      000000004C000000000000000000000001000000E0D057007335CF11AE690800
      2B2E126208000000000000004C0000000114020000000000C000000000000046
      8000000000000000000000000000000000000000000000000000000000000000
      00000000000000000100000000000000000000000000000000000000}
  end
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 643
    Height = 73
    Align = alTop
    Lines.Strings = (
      'Memo1')
    TabOrder = 1
  end
end


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

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, OleCtrls, SHDocVw, MSHTML, ActiveX, StdCtrls;

 type
   TObjectProcedure = procedure of object;

   TEventObject = class(TInterfacedObject, IDispatch)
   private
     FOnEvent: TObjectProcedure;

   protected
     function 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;

   public
     constructor 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);

   private
     procedure Document_OnMouseOver;

   public
     { Public declarations }
   end;

 var
   Form1: TForm1;
   htmlDoc: IHTMLDocument2;

 implementation

{$R *.dfm}

 procedure TForm1.Document_OnMouseOver;
 var
   element: IHTMLElement;
 begin
   if htmlDoc = nil then
     Exit;
   element := htmlDoc.parentWindow.event.srcElement;
   Memo1.Clear;
   if LowerCase(element.tagName) = 'a' then
   begin
     Memo1.Lines.Add('LINK info...');
     Memo1.Lines.Add(Format('HREF : %s', [element.getAttribute('href', 0)]));
   end
   else if LowerCase(element.tagName) = 'img' then
   begin
     Memo1.Lines.Add('IMAGE info...');
     Memo1.Lines.Add(Format('SRC : %s', [element.getAttribute('src', 0)]));
   end
   else
   begin
     Memo1.Lines.Add(Format('TAG : %s', [element.tagName]));
   end;
 end; (* Document_OnMouseOver *)

 procedure TForm1.FormCreate(Sender: TObject);
 begin
   WebBrowser1.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);
 begin
   htmlDoc := nil;
 end; (* WebBrowser1BeforeNavigate2 *)

 procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;
   var URL: OleVariant);
 begin
   if Assigned(WebBrowser1.Document) then
   begin
     htmlDoc := WebBrowser1.Document as IHTMLDocument2;
     if htmlDoc.frames.length > 0 then
     begin
       htmlDoc := (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);
 begin
   inherited Create;
   FOnEvent := OnEvent;
 end;

 function TEventObject.GetIDsOfNames(const IID: TGUID; Names: Pointer;
   NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
 begin
   Result := E_NOTIMPL;
 end;

 function TEventObject.GetTypeInfo(index, LocaleID: Integer; out TypeInfo): HResult;
 begin
   Result := E_NOTIMPL;
 end;

 function TEventObject.GetTypeInfoCount(out Count: Integer): HResult;
 begin
   Result := E_NOTIMPL;
 end;

 function TEventObject.Invoke(dispid: Integer; const IID: TGUID; LocaleID: Integer;
   Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
 begin
   if (dispid = DISPID_VALUE) then
   begin
     if Assigned(FOnEvent) then
       FOnEvent;
     Result := S_OK;
   end
   else
     Result := E_NOTIMPL;
 end;

end.
 转自:https://www.cnblogs.com/del/archive/2010/03/04/1678550.html

标签:begin,end,定义,TWebBrowser,Integer,var,const,Document,Memo1
From: https://www.cnblogs.com/tang-delphi/p/16751522.html

相关文章

  • 给TWebBrowser添加鼠标事件
    学习用IsChild(WebBrowser1.Handle,  Msg.Hwnd)判断鼠标事件是否来自TWebBrowserunit  Unit1;interfaceuses   Windows,  Messages,  SysUtils,  Vari......
  • (4)SparkSQL中如何定义UDF和使用UDF
    SparkSQL中用户自定义函数,用法和SparkSQL中的内置函数类似;是saprkSQL中内置函数无法满足要求,用户根据业务需求自定义的函数。首先定义一个UDF函数:packagecom.udf;import......
  • 源码角度了解Skywalking之AbstractClassEnhancePluginDefine插件增强定义
    源码角度了解Skywalking之AbstractClassEnhancePluginDefine插件增强定义AbstractClassEnhancePluginDefine是所有插件的抽象类,我们在分析Skywalking初始化流程的时候见到......
  • java---回顾方法的定义和调用
    方法的回顾和调用packagecom.oop.demo;​importjava.io.IOError;importjava.io.IOException;​//return代表方法结束,返回一个结果//下方就是一个类publicclassDemo01......
  • 自定义异常
    自定义异常使用Java内置的异常类可以描述在编程时出现的大部分异常情况。除此之外,用户还可以自定义异常。用户自定义异常类,只需要继承Exception类即可。在程序中使用自......
  • Vue2 自定义属性
    概述vue中不不仅仅有官方提供的指令,用户还可以根据自己的需要进行自定义指令。比如当我们需要一个常用的操作将文字改为蓝色,如果我们需要修改大量标签时,就可以使用自定......
  • #yyds干货盘点# 软件测试的定义
    1973年,BillHetzel给出了软件测试的第一个定义:“软件测试就是为了程序能够按预期设想运行而建立足够的信心”。这个定义强调的是证实程序按预期运行,当软件测试这种技术手段......
  • spring boot 自定义线程池与使用
    一、进行线程池创建importcn.hutool.core.thread.ThreadFactoryBuilder;importlombok.extern.slf4j.Slf4j;importorg.springframework.aop.interceptor.AsyncUncaug......
  • Jmeter组件:参数化之用户定义的变量
    1、UserDefinedVariables:用户定义的变量,可以将请求路径设置为变量或者将参数值设置为变量等2、添加一个变量存储http请求的路径3、通过${变量名}取值......
  • 关于 js 函数定义方式
    函数声明式function(a,b){returna+b}特点:此种方式可定义命名的函数变量,而无需给变量赋值,这是一种独立的结构,不能嵌套在非功能模块中。函数名在自身作用域和父作用域内是......