⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 在Delphi语方的应用程序中响应HTML页面元素的事件
💻 PAS
字号:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Panel1: TPanel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    ButtonClicked: TDHTMLEvent;
    procedure DHTMLElementEvent(Sender: TObject);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//procedure SetOnChangeInputElement(Browser:TWebBrowser; ElementName: String;
//                                  EventObject: TDHTMLEvent; EventHandler:TNotifyEvent);
//var   WebDoc : IHTMLDocument2;
//   pDispatch : IDISPATCH;
//    elements : IHTMLElementCollection;
//       Input : IHTMLInputElement;
//begin
//    OleCheck(Browser.Document.QueryInterface(IID_IHTMLDocument2, WebDoc));
//    // grab all elements:
//    elements := WebDoc.Get_all;
//    // find first with the name ElementName:
//    pDispatch := elements.item(ElementName, 0);
//    // get it:
//    OleCheck(pDispatch.QueryInterface(IID_IHTMLInputElement, Input));
//    // now you can hook event handler to our object:
//    OleVariant(Input).OnKeyDown:=EventObject.HookEventHandler(EventHandler);
//end;

procedure SetButtonOnClickEvent(Browser:TWebBrowser; ElementName: String;
                                  EventObject: TDHTMLEvent; EventHandler:TNotifyEvent);
var   WebDoc : IHTMLDocument2;
   pDispatch : IDISPATCH;
    elements : IHTMLElementCollection;
       Button : IHTMLButtonElement;
begin
    OleCheck(Browser.Document.QueryInterface(IID_IHTMLDocument2, WebDoc));
    // grab all elements:
    elements := WebDoc.Get_all;
    // find first with the name ElementName:
    pDispatch := elements.item(ElementName, 0);
    // get it:
    OleCheck(pDispatch.QueryInterface(IID_IHTMLButtonElement, Button));
    // now you can hook event handler to our object:
    OleVariant(Button).OnClick := EventObject.HookEventHandler(EventHandler);
end;

procedure TForm1.DHTMLElementEvent(Sender: TObject);
begin // This is triggered with each KeyDown event
  Panel1.Color:=RGB(Random(254), Random(254), Random(254));
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  ButtonClicked:=TDHTMLEvent.Create;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  site: string;
begin
  // Surf to EE:
  site:= ExtractFilePath(Application.ExeName) + 'test.html';
  WebBrowser1.Navigate(site);
  // Wait for page to fully load:
  while WebBrowser1.ReadyState<>READYSTATE_COMPLETE do
  begin
        Sleep(1);
        Application.ProcessMessages;
  end;
  // Hook it:
  SetButtonOnClickEvent(WebBrowser1,
                           'btnTest',        // We are hooking event in BUTTON element named "btnTest"
                           ButtonClicked,    // TDHTMLEvent object dedicated to this event
                           DHTMLElementEvent // Our own event handler that will get hooked by ButtonClicked
                          );

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -