用Delphi編寫IE擴(kuò)展
發(fā)表時(shí)間:2023-08-07 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在自己的程序中使用過(guò)WebBrowser控件的朋友都知道,WebBrowser控件定義了諸如BeforeNavigate、DownloadComplete 等事件,我們可以通過(guò)編寫事件處理代碼實(shí)現(xiàn)對(duì)...
在自己的程序中使用過(guò)WebBrowser控件的朋友都知道,WebBrowser控件定義了諸如BeforeNavigate、DownloadComplete 等事件,我們可以通過(guò)編寫事件處理代碼實(shí)現(xiàn)對(duì)WebBrowser控件的操作。那么如何實(shí)現(xiàn)對(duì)IE的事件響應(yīng)和處理呢?同建立IE面板一樣。我們需要建立一個(gè)實(shí)現(xiàn)IObjectWithSite接口的COM組件,不同的是,我們還需要實(shí)現(xiàn)IDispatch接口,在IObjectWithSite接口的SetSite方法中獲得IE的WebBrowser接口并建立自身與WebBrowser的連接,然后如果在IE的Webbrowser對(duì)象中發(fā)生什么事件的話,那么IE就會(huì)回調(diào)連接的IDispatch接口的Invoke方法。我們通過(guò)在Invoke方法中編寫代碼就可以獲得IE事件了。這個(gè)利用的是COM編程的回調(diào)接口原理。
下面我們首先來(lái)實(shí)現(xiàn)代碼。點(diǎn)擊Delphi菜單 File New 。在 ActiveX 頁(yè)面中選擇Active Library ,然后點(diǎn)擊 OK 按鈕。然后用同樣的方法建立一個(gè)COM Object。在COM Object Wizard 窗口中,將復(fù)選框 Included type library 去掉。然后在Class Name中輸入IEHelper,在Implemented Interface 中輸入:IDispatch;IObjectwithSite 。然后點(diǎn)擊 OK 按鈕建立一個(gè)COM組件。
保存工程,將工程保存為IEHelper.dpr,將Unit1保存為IEHelperUnit.pas。下面是IEHelperUnit.pas的具體代碼:
unit iehelperunit;
interface
uses
WIndows, Comobj, ActiveX, SHDOCVW, MSHTML,Dialogs;
type
TIEHelperFactory = class(TComObjectFactory)
private
procedure AddKeys;
procedure RemoveKeys;
public
procedure UpdateRegistry(Register: Boolean); override;
end;
TIEHelper = class(TComObject, IDispatch, IObjectWithSite)
public
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;
function SetSite(const pUnkSite: IUnknown): HResult; stdcall;
function GetSite(const riid: TIID; out site: IUnknown): HResult; stdcall;
private
IE: IWebbrowser2;
Cookie: Integer;
end;
const
Class_IEHelper: TGUID = '{3D898C55-74CC-4B7C-B5F1-45913F368388}';
implementation
uses ComServ, Registry, SysUtils;
procedure DoStatusTextChange(const Text: WideString);
begin
end;
procedure DoProgressChange(Progress: Integer; ProgressMax: Integer);
begin
end;
procedure DoCommandStateChange(Command: Integer; Enable: WordBool);
begin
end;
procedure DoDownloadBegin;
begin
end;
procedure DoDownloadComplete;
begin
end;
procedure DoTitleChange(const Text: WideString);
begin
end;
procedure DoPropertyChange(const szProperty: WideString);
begin
end;
procedure DoBeforeNavigate2(const pDisp: IDispatch; var URL: OleVariant; var Flags: OleVariant; var TargetFrameName: OleVariant; var PostData: OleVariant; var Headers: OleVariant; var Cancel: WordBool);
begin
if URL<>'http://www.applevb.com/'then begin
Showmessage('你不可以瀏覽其它站點(diǎn)');
Cancel:=True;
URL:='http://www.applevb.com';
(pDisp as IWebbrowser2).Navigate2(URL,Flags,TargetFrameName,PostData,Headers);
end;
end;
procedure DoNewWindow2(var ppDisp: IDispatch; var Cancel: WordBool);
begin
end;
procedure DoNavigateComplete2(const pDisp: IDispatch; var URL: OleVariant);
begin
end;
procedure DoDocumentComplete(const pDisp: IDispatch; var URL: OleVariant);
begin
end;
procedure DoOnQuit;
begin
end;
procedure DoOnVisible(Visible: WordBool);
begin
end;
procedure DoOnToolBar(ToolBar: WordBool);
begin
end;
procedure DoOnMenuBar(MenuBar: WordBool);
begin
end;
procedure DoOnStatusBar(StatusBar: WordBool);
begin
end;
procedure DoOnFullScreen(FullScreen: WordBool);
begin
end;
procedure DoOnTheaterMode(TheaterMode: WordBool);
begin
end;
procedure BuildPositionalDispIds(pDispIds: PDispIdList; const dps: TDispParams);
var
i: integer;
begin
Assert(pDispIds <> nil);
for i := 0 to dps.cArgs - 1 do
pDispIds^[i] := dps.cArgs - 1 - i;
if (dps.cNamedArgs <= 0) then Exit;
for i := 0 to dps.cNamedArgs - 1 do
pDispIds^[dps.rgdispidNamedArgs^[i} := i;
end;
function TIEHelper.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
type
POleVariant = ^OleVariant;
var
dps: TDispParams absolute Params;
bHasParams: boolean;
pDispIds: PDispIdList;
iDispIdsSize: integer;
begin
Result := DISP_E_MEMBERNOTFOUND;
pDispIds := nil;
iDispIdsSize := 0;
bHasParams := (dps.cArgs > 0);
if (bHasParams) then
begin
iDispIdsSize := dps.cArgs * SizeOf(TDispId);
GetMem(pDispIds, iDispIdsSize);
end;
try
if (bHasParams) then BuildPositionalDispIds(pDispIds, dps);
case DispId of
102:
begin
DoStatusTextChange(dps.rgvarg^[pDispIds^[0}.bstrval);
Result := S_OK;
end;
108:
begin
DoProgressChange(dps.rgvarg^[pDispIds^[0}.lval, dps.rgvarg^[pDispIds^[1}.lval);
Result := S_OK;
end;
105:
begin
DoCommandStateChange(dps.rgvarg^[pDispIds^[0}.lval, dps.rgvarg^[pDispIds^[1}.vbool);
Result := S_OK;
end;
106:
begin
DoDownloadBegin();
Result := S_OK;
end;
104:
begin
DoDownloadComplete();
Result := S_OK;
end;
113:
begin
DoTitleChange(dps.rgvarg^[pDispIds^[0}.bstrval);
Result := S_OK;
end;
112:
begin
DoPropertyChange(dps.rgvarg^[pDispIds^[0}.bstrval);
Result := S_OK;
end;
250:
begin
DoBeforeNavigate2(IDispatch(dps.rgvarg^[pDispIds^[0}.dispval), POleVariant(dps.rgvarg^[pDispIds^[1}.pvarval)^, POleVariant(dps.rgvarg^[pDispIds^[2}.pvarval)^, POleVariant(dps.rgvarg^[pDispIds^[3}.pvarval)^, POleVariant(dps.rgvarg^[pDispIds^[4}.pvarval)^, POleVariant(dps.rgvarg^[pDispIds^[5}.pvarval)^, dps.rgvarg^[pDispIds^[6}.pbool^);
Result := S_OK;
end;
251:
begin
DoNewWindow2(IDispatch(dps.rgvarg^[pDispIds^[0}.pdispval^), dps.rgvarg^[pDispIds^[1}.pbool^);
Result := S_OK;
end;
252:
begin
DoNavigateComplete2(IDispatch(dps.rgvarg^[pDispIds^[0}.dispval), POleVariant(dps.rgvarg^[pDispIds^[1}.pvarval)^);
Result := S_OK;
end;
259:
begin
DoDocumentComplete(IDispatch(dps.rgvarg^[pDispIds^[0}.dispval), POleVariant(dps.rgvarg^[pDispIds^[1}.pvarval)^);
Result := S_OK;
end;
253:
begin
DoOnQuit();
Result := S_OK;
end;
254:
begin
DoOnVisible(dps.rgvarg^[pDispIds^[0}.vbool);
Result := S_OK;
end;
255:
begin
DoOnToolBar(dps.rgvarg^[pDispIds^[0}.vbool);
Result := S_OK;
end;
256:
begin
DoOnMenuBar(dps.rgvarg^[pDispIds^[0}.vbool);
Result := S_OK;
end;
257:
begin
DoOnStatusBar(dps.rgvarg^[pDispIds^[0}.vbool);
Result := S_OK;
end;
258:
begin
DoOnFullScreen(dps.rgvarg^[pDispIds^[0}.vbool);
Result := S_OK;
end;
260:
begin
DoOnTheaterMode(dps.rgvarg^[pDispIds^[0}.vbool);
Result := S_OK;
end;
end;
finally
if (bHasParams) then FreeMem(pDispIds, iDispIdsSize);
end;
end;
function TIEHelper.GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
begin
Result := E_NOTIMPL;
end;
function TIEHelper.GetTypeInfo(Index, LocaleID: Integer;
out TypeInfo): HResult;
begin
Result := E_NOTIMPL;
pointer(TypeInfo) := nil;
end;
function TIEHelper.GetTypeInfoCount(out Count: Integer): HResult;
begin
Result := E_NOTIMPL;
Count := 0;
end;
function TIEHelper.GetSite(const riid: TIID; out site: IUnknown): HResult;
begin
// Result := S_OK;
if Assigned(IE) then result:=IE.QueryInterface(riid, site)
else
Result:= E_FAIL;
end;
function TIEHelper.SetSite(const pUnkSite: IUnknown): HResult;
var
cmdTarget: IOleCommandTarget;
Sp: IServiceProvider;
CPC: IConnectionPointContainer;
CP: ICOnnectionPoint;
begin
if Assigned(pUnkSite) then begin
cmdTarget := pUnkSite as IOleCommandTarget;
Sp := CmdTarget as IServiceProvider;
if Assigned(Sp)then
Sp.QueryService(IWebbrowserApp, IWebbrowser2, IE);
if Assigned(IE) then begin
IE.QueryInterface(IConnectionPointContainer, CPC);
CPC.FindConnectionPoint(DWEBbrowserEvents2, CP);
CP.Advise(Self, Cookie)
end;
end;
Result := S_OK;
end;
procedure TIEHelperFactory.AddKeys;
var S: string;
begin
S := GUIDToString(CLASS_IEHelper);
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('Software\Microsoft\Windows\CurrentVersion\explorer\Browser Helper Objects\' + S, TRUE)
then CloseKey;
finally
free;
end;
end;
procedure TIEHelperFactory.RemoveKeys;
var S: string;
begin
S := GUIDToString(CLASS_IEHelper);
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
DeleteKey('Software\Microsoft\Windows\CurrentVersion\explorer\Browser Helper Objects\' + S);
finally
free;
end;
end;
procedure TIEHelperFactory.UpdateRegistry(Register: Boolean);
begin
inherited UpdateRegistry(Register);
if Register then AddKeys else RemoveKeys;
end;
initialization
TIEHelperFactory.Create(ComServer, TIEHelper, Class_IEHelper,
'IEHelper', '', ciMultiInstance, tmApartment);
end.
代碼很長(zhǎng),但是關(guān)鍵的是TIEHelper.SetSite方法以及TIEHelper.Invoke方法。在TIEHelper.SetSite方法中注意以下語(yǔ)句:
if Assigned(Sp)then
Sp.QueryService(IWebbrowserApp, IWebbrowser2, IE);
if Assigned(IE) then begin
IE.QueryInterface(IConnectionPointContainer, CPC);
CPC.FindConnectionPoint(DWEBbrowserEvents2, CP);
CP.Advise(Self, Cookie)
上面的語(yǔ)句作用是,首先獲得IE的Webbrowser接口,然后尋找到連接點(diǎn)。并通過(guò)Advise方法建立COM自身與連接點(diǎn)的連接。
當(dāng)連接建立成功后,IE在有事件引發(fā)后,會(huì)調(diào)用連接到自身的IDispatch接口對(duì)象的Invoke方法。不同的事件對(duì)應(yīng)不同的DispID編碼,我們可以在程序中判斷DispID并做相應(yīng)的處理。在上面的程序中,我們只處理了BeforeNavigate2 事件,處理函數(shù)是DoBeforeNavigate2,在該函數(shù)中,如果瀏覽的站點(diǎn)不是'http://www.applevb.com/'的話,程序會(huì)提示:'你不可以瀏覽其它站點(diǎn)'并強(qiáng)行轉(zhuǎn)到http://www.applevb.com。
很多的軟件,象“護(hù)花使者”以及“3721”一類的中文網(wǎng)址”都是利用上面的原理來(lái)實(shí)現(xiàn)對(duì)IE瀏覽器事件響應(yīng)的,例如3721,當(dāng)用戶輸入一個(gè)中文詞并瀏覽時(shí),COM組件可以在BeforeNavigate2 事件中編寫代碼訪問(wèn)服務(wù)器并轉(zhuǎn)到正確的站點(diǎn)上去。
以上程序在Win2K、Delphi 5下編寫 Win98、Win2K下編輯通過(guò),如果大家需要源程序或者對(duì)于COM編程需要有什么的指教的話,歡迎到我的主頁(yè) http://www.applevb.com 訪問(wèn),我愿意同大家一起探討。