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

delphi中EmbeddedWB网页html相互调用(二)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

我们可以通过控件 EmbeddedWB_D5-D2010_Version_14.69.1 来响应html事件,还可以自定义html响应哪些html元素。

控件下载 点击下载

里面有demos文件夹大家可以看看delphi代码实例。

EmbeddedWB_D5-D2010_Version_14.69.1\Demos\EmbeddedWB Demos\15 - THTMLListener_Demo

关键方法:HtmlListener1HandlersOnClickHandle

这里贴出这个demo的代码

unit uMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Forms,
  Dialogs, EwbBehaviorsComp, EwbEventsComp, EwbEvents, OleCtrls, SHDocVw_EWB, MSHTML_EWB, EwbCore,
  EmbeddedWB, StdCtrls, ComObj, ActiveX, ExtCtrls;



type
  TForm1 = class(TForm)
    EmbeddedWB1: TEmbeddedWB;
    Panel1: TPanel;
    btnEnableDisable: TButton;
    lblClickedOnElement: TLabel;
    lblElementUndertheMouse: TLabel;
    HtmlListener1: THtmlListener;
    procedure HtmlListener1HandlersOnClickHandle(Sender: TObject;
      Event: IHTMLEventObj);
    procedure FormCreate(Sender: TObject);
    procedure HtmlListener1HandlersOnMouseMoveHandle(Sender: TObject;
      Event: IHTMLEventObj);
    procedure EmbeddedWB1DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure btnEnableDisableClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure EmbeddedWB1BeforeNavigate2(ASender: TObject;
      const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);
  private
    { Private declarations }
    HubLinksList: TInterfaceList;
    FEventsEnabled: Boolean;
    HubLink: IHubLink;
    procedure DisconnectHtmlListeners;
    procedure ConnectHtmlListeners;
    procedure ConnectHtmlListener(ASender: TObject; Doc: IHTMLDocument2);
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FEventsEnabled := True;
  HubLinksList := TInterfaceList.Create;
  EmbeddedWB1.Navigate('www.google.com');
end;

procedure TForm1.HtmlListener1HandlersOnClickHandle(Sender: TObject;
  Event: IHTMLEventObj);
begin
  lblClickedOnElement.Caption := Format('Clicked on Element with Tag: [%s]', [Event.srcElement.tagName])
end;

procedure TForm1.HtmlListener1HandlersOnMouseMoveHandle(Sender: TObject;
  Event: IHTMLEventObj);
begin
  lblElementUndertheMouse.Caption := Format('Element under the Mouse [%s]', [Event.srcElement.tagName])
end;

procedure TForm1.DisconnectHtmlListeners;
var
  i: Integer;
begin
  for i := HubLinksList.Count - 1 downto 0 do
  begin
    (HubLinksList[i] as IHubLink).Disconnect;
    HubLinksList.Remove(HubLinksList[i]);
  end;
end;

procedure TForm1.ConnectHtmlListener(ASender: TObject; Doc: IHTMLDocument2);
var
  CPC: IConnectionPointContainer;
begin
  if Assigned(Doc) then
  begin
    Doc.QueryInterface(IConnectionPointContainer, CPC);
    if Assigned(CPC) then
    begin
      HubLink := HtmlListener1.Connect2(CPC);
      HubLinksList.Add(HubLink);
    end;
  end;
end;

procedure TForm1.ConnectHtmlListeners;
var
  i: Integer;
  WB: IWebbrowser2;
  HTMLDoc: IHTMLDocument2;
begin
  for i := 0 to EmbeddedWB1.FrameCount - 1 do
  begin
    WB := EmbeddedWB1.GetFrame(i);
    if Assigned(WB) then
    begin
      if Succeeded(WB.Document.QueryInterface(IHTMLDocument2, HTMLDoc)) then
        ConnectHtmlListener(nil, HTMLDoc);
    end;
  end;
end;

procedure TForm1.EmbeddedWB1BeforeNavigate2(ASender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
  with ASender as TEmbeddedWB do
    if pDisp = DefaultInterface then
    begin
      DisconnectHtmlListeners;
    end;
end;

procedure TForm1.EmbeddedWB1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  WB: IWebbrowser;
  HTMLDoc: IHTMLDocument2;
begin
  if FEventsEnabled then
  begin
    WB := (pDisp as IWebBrowser);
    if Assigned(WB) then
      if Succeeded(WB.Document.QueryInterface(IHTMLDocument2, HTMLDoc)) then
        ConnectHtmlListener(ASender, HTMLDoc);
  end;
end;

procedure TForm1.btnEnableDisableClick(Sender: TObject);
begin
  if FEventsEnabled then
  begin
    DisconnectHtmlListeners;
    btnEnableDisable.Caption := 'Enable Events';
    FEventsEnabled := False;
  end else
  begin
    ConnectHtmlListeners;
    btnEnableDisable.Caption := 'Disable Events';
    FEventsEnabled := True;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DisconnectHtmlListeners;
  HubLinksList.Free;
end;


end.

完整代码下载:点击下载


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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