1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| library test_WB;
uses
SysUtils,
Dialogs,
Windows,
Classes,
ActiveX,
SHDocVw_TLB,
mshtml,
Forms;
type
TDummy = class
class procedure WebBrowser_OnNavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant); // pour WB_Create
class procedure DocumentOnClick();
end;
type
TEventObject = class(TInterfacedObject, IDispatch)
private
FOnEvent: TProcedure; // si ça existe c'est dans System.SysUtils !
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: TProcedure);
property OnEvent: TProcedure read FOnEvent write FOnEvent;
end;
{ TEventObject }
constructor TEventObject.Create(const OnEvent: TProcedure);
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;
var
frm3DllForm: TForm;
WB: TWebBrowser;
{$R *.res}
// procédure pour une attente non bloquante de MSecs milli-secondes
procedure Delay(MSecs: Cardinal);
var
iStop : cardinal;
begin
iStop := GetTickCount + Msecs;
while GetTickCount < iStop do
begin
Application.ProcessMessages;
sleep(1);
end
end;
// procédure évènement OnClick d'un élément HTML quelconque
class procedure TDummy.DocumentOnClick();
begin
showmessage('Clic sur élément HTML');
end;
// procédure évènement de fin de chargement d'une form
class procedure TDummy.WebBrowser_OnNavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
try
if ( (pDisp as IWebBrowser) = (Sender as TWebBrowser).DefaultInterface ) then (Sender as TWebBrowser).Tag := 0;
finally
end;
end;
// création dynamique d'un WB
function WBCreate(xhand: integer):integer; stdcall; export;
begin
try
// if not assigned(GetInfoParams) then GetInfoParams := TStringList.Create
// else GetInfoParams.Clear;
WB := TWebBrowser.Create(Frm3DllForm);
WB.ParentWindow := xhand;
WB.Resizable := true;
WB.Silent := true;
WB.OnNavigateComplete2 := TDummy.WebBrowser_OnNavigateComplete2;
WB.Tag := 0;
finally
end;
result := 0;
end;
// géométrie d'un WB
function WBLocate(xleft,xtop,xwidth,xheight: integer): integer; stdcall; export;
begin
try
WB.Top := xtop;
WB.Left := xleft;
WB.Width := xwidth;
WB.Height := xheight;
finally
end;
result := 0;
end;
// chargement d'une URL dans un WB
function WBUrl(xurl: pstring): integer; stdcall; export;
var
zurl: WideString;
begin
try
zurl := xurl^;
WB.Tag := 1;
WB.Navigate(zurl);
while WB.tag=1 do delay(100);
finally
end;
result := 0;
end;
// supprimer le WB
function WBDelete: integer; stdcall; export;
begin
try
WB.Free;
finally
end;
result := 0;
end;
// installation de l'évènement OnClick sur un élément HTML quelconque
function WBOnClick():integer; stdcall; export;
var
myDoc: IHTMLDocument2;
begin
myDoc := WB.Document as IHTMLDocument2;
myDoc.attachEvent('onclick', TEventObject.Create(DocumentOnClick) as IDispatch);
result := 0;
end;
exports
WBCreate,
WBLocate,
WBUrl,
WBOnClick,
WBDelete;
begin
end. |
Partager