程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 利用Delphi編寫IE擴展

利用Delphi編寫IE擴展

編輯:Delphi

就是如何使IE擴展組件可以響應事件。
    在自己的程序中使用過WebBrowser控件的朋友都知道,WebBrowser控件定義了諸如BeforeNavigate、DownloadComplete 等事件,我們可以通過編寫事件處理代碼實現對WebBrowser控件的操作。那麼如何實現對IE的事件響應和處理呢?同建立IE面板一樣。我們需要建立一個實現IObjectWithSite接口的COM組件,不同的是,我們還需要實現IDispatch接口,在IObjectWithSite接口的SetSite方法中獲得IE的WebBrowser接口並建立自身與WebBrowser的連接,然後如果在IE的Webbrowser對象中發生什麼事件的話,那麼IE就會回調連接的IDispatch接口的Invoke方法。我們通過在Invoke方法中編寫代碼就可以獲得IE事件了。這個利用的是COM編程的回調接口原理。
    下面我們首先來實現代碼。點擊Delphi菜單 File | New 。在 ActiveX 頁面中選擇Active Library ,然後點擊 OK 按鈕。然後用同樣的方法建立一個COM Object。在COM Object Wizard 窗口中,將復選框 Included type library 去掉。然後在Class Name中輸入IEHelper,在Implemented Interface 中輸入:IDispatch;IObjectwithSite 。然後點擊 OK 按鈕建立一個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(你不可以浏覽其它站點);
    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
          DoPr

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved