p; 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.
代码很长,但是关键的是TIEHelper.SetSite方法以及TIEHelper.Invoke方法。在TIEHelper.SetSite方法中注意以下语句: 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)
上面的语句作用是,首先获得IE的Webbrowser接口,然后寻找到连接点。并通过Advise方法建立COM自身与连接点的连接。 当连接建立成功后,IE在有事件引发后,会调用连接到自身的IDispatch接口对象的Invoke方法。不同的事件对应不同的DispID编码,我们可以在程序中判断DispID并做相应的处理。在上面的程序中,我们只处理了BeforeNavigate2 事件,处理函数是DoBeforeNavigate2,在该函数中,如果浏览的站点不是'http://www.applevb.com/'的话,程序会提示:'你不可以浏览其它站点'并强行转到http://www.applevb.com。 很多的软件,象“护花使者”以及“3721”一类的中文网址”都是利用上面的原理来实现对IE浏览器事件响应的,例如3721,当用户输入一个中文词并浏览时,COM组件可以在BeforeNavigate2 事件中编写代码访问服务器并转到正确的站点上去。 以上程序在Win2K、Delphi 5下编写 Win98、Win2K下编辑通过.
上一页 [1] [2] |