html交互界面怎么写,用WebBrowser实现HTML界面的应用和交互

unit Unit1;

interface

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw,UnitDHtmlEvent,mshtml;

type

TForm1 = class(TForm)

wb1: TWebBrowser;

procedure FormCreate(Sender: TObject);

procedure FormClose(Sender: TObject; var Action: TCloseAction);

procedure wb1DocumentComplete(ASender: TObject; const pDisp: IDispatch;

const URL: OleVariant);

private

procedure DemoEventSink(Sender: TObject);

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

EventSink: TDHTMLEvent;

implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin

EventSink.Free;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

EventSink:= TDHTMLEvent.Create;

wb1.Navigate('about:

标题 ');

end;

procedure TForm1.wb1DocumentComplete(ASender: TObject; const pDisp: IDispatch;

const URL: OleVariant);

var

Doc : IHTMLDocument2;

Element : IHTMLElement;

begin

Doc := IHTMLDocument2(wb1.Document);

if nil <> Doc then

begin

//找到HTML元件

Element := Doc.all.item('B3', 0) as IHTMLElement;

//使HTML元件的click事件和DemoEventSink过程关连

Element.onclick := EventSink.HookEventHandler(DemoEventSink);

end;

end;

procedure TForm1.DemoEventSink(Sender: TObject);

begin

ShowMessage('成功从HTML中调用Delphi的过程。');

end;

end.

//引用的类pas文件

unit UnitDHtmlEvent; interface

uses Windows, Classes;

type

TDHTMLEvent = class (TObject, IUnknown, IDispatch)

private

FRefCount: Integer;

FOldEvent: IDispatch;

FElementEvent: TNotifyEvent;

// IUnknown

function QueryInterface(const IID: TGUID; out Obj): Integer; stdcall;

function _AddRef: Integer; stdcall;

function _Release: Integer; stdcall;

// IDispatch

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

{ Public declarations }

function HookEventHandler(CallerHandler: TNotifyEvent): IDispatch;

property ElementEvent: TNotifyEvent read FElementEvent write FElementEvent;

end;

implementation

{ TDHTMLEvent }

function TDHTMLEvent._AddRef: Integer;

begin

Inc(FRefCount);

Result := FRefCount;

end;

function TDHTMLEvent._Release: Integer;

begin

Dec(FRefCount);

Result := FRefCount;

end;

function TDHTMLEvent.GetIDsOfNames(const IID: TGUID; Names: Pointer;

NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;

begin

if FOldEvent <> nil then

Result := FOldEvent.GetIDsOfNames(IID, Names, NameCount, LocaleID, DispIDs)

else

Result := E_NOTIMPL;

end;

function TDHTMLEvent.GetTypeInfo(Index, LocaleID: Integer;

out TypeInfo): HResult;

begin

if FOldEvent <> nil then

Result := FOldEvent.GetTypeInfo(Index, LocaleID, TypeInfo)

else

begin

Pointer(TypeInfo) := nil;

Result := E_NOTIMPL;

end

end;

function TDHTMLEvent.GetTypeInfoCount(out Count: Integer): HResult;

begin

if FOldEvent <> nil then

Result := FOldEvent.GetTypeInfoCount(Count)

else

begin

Count := 0;

Result := S_OK;

end;

end;

function TDHTMLEvent.QueryInterface(const IID: TGUID; out Obj): Integer;

begin

if GetInterface(IID, Obj) then

Result := S_OK

else

Result := E_NOINTERFACE;

end;

function TDHTMLEvent.Invoke(DispID: Integer; const IID: TGUID;

LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,

ArgErr: Pointer): HResult;

begin

try

if Assigned(FElementEvent) then FElementEvent(Self);

finally

if FOldEvent <> nil then

Result := FOldEvent.Invoke(DispID, IID, LocaleID, Flags, Params,

VarResult, ExcepInfo, ArgErr)

else

Result := E_NOTIMPL;

end;

end;

function TDHTMLEvent.HookEventHandler(CallerHandler: TNotifyEvent): IDispatch;

begin

FOldEvent:=nil;

ElementEvent:=CallerHandler;

Result:=Self;

end;

end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值