Delphi中使用ActiveX的一些心得

使用方法分爲兩種:
1、直接把可視化的ActiveX控件放到程序中;
2、運行時根據須要實時創建。
  若是是直接使用,則應用程序在初始化的過程當中會自動尋找、建立所需的ActiveX控件,若是控件沒有註冊,初始化程序會產生一個異常,捕捉並處理這個異常。
在程序Form中加入一個新的方法:
unit UAutoRegActiveX; 
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComObj; //加入ComObj單元
type
  TAutoRegActiveXFrm = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  protected
    { Public declarations }
    procedure CheckException(Sender: TObject; EAbort: Exception);
  end;
var
  AutoRegActiveXFrm: TAutoRegActiveXFrm;
implementation
{$R *.dfm}
{-------------------------------------------------
標準ActiveX控件中,有兩個函數DLLRegisterServer 、DLLUnRegisterServer可調用,
其中函數用於註冊控件,用於卸載控件。咱們可用LoadLibrary裝載DLL/OCX文件,用
GetProcAddress獲取DLLRegisterServer和DLLUnRegisterServer兩個函數的指針,然
後再直接運行這兩個函數便可實現註冊和卸載ActiveX控件的操做,從而代替Windows
系統的RegSvr32.exe實現ActiveX控件的註冊和卸載。
--------------------------------------------------}
{-------------------------------------------------
參數說明:
sOleFileName 一個DLL或OCX文件名;
OleAction 表示註冊操做類型:1表示註冊,0表示卸載
返回值:True表示操做執行成功,False表示操做執行失敗
--------------------------------------------------}
function OLERegister(sOleFileName: String; OleAction: Byte):Boolean;
const
  RegisterOle = 1; //註冊
  UnRegisterOle = 0; //卸載
type
  TOleRegisterFunction = function: HResult; //註冊或卸載函數原型
var
  hLibraryHandle: THandle; //由LoadLibray返回的DLL或OCX句柄
  hFunctionAddress: TFarProc; //DLL或OCX中的函數句柄,由GetProAddress返回
  RegFunction: TOleRegisterFunction; //註冊或卸載函數指針
begin
  Result := False;
  //打開文件,返回DLL或OCX句柄
  hLibraryhandle := LoadLibrary(PChar(SOleFileName));
  if (hLibraryHandle > 0) then //DLLakg OCX句柄正確
  try
    //返回註冊或卸載函數指針
    if (OleAction = RegisterOle) then  //返回註冊函數指針
      hFunctionAddress := GetProcAddress(hLibraryhandle,PChar('DLLRegisterServer'))
    else //返回卸載函數指針
      hFunctionAddress := GetProcAddress(hLibraryhandle,PChar('DLLUnRegisterServer'));
    if (hFunctionAddress <> nil) then //判斷註冊或卸載函數是否存在
    begin
      RegFunction := TOleRegisterFunction(hFunctionAddress); //獲取操做函數的指針
      if RegFunction >=0 then  //執行註冊或卸載操做,返回值>=0表示執行成功
        Result := True;
    end;
  finally
    FreeLibrary(hLibraryHandle); //關閉已打開的文件
  end;
end;
{ TAutoRegActiveXFrm }
procedure TAutoRegActiveXFrm.CheckException(Sender: TObject;
  EAbort: Exception);
begin
  if EAbort is EOleSysError then
  begin
    if HResult(EOleSysError(EAbort).ErrorCode) = REGDB_E_CLASSNOTREG then
      OleRegister('D:/Flash.ocx',1);
  end
  else
    Application.ShowException(EAbort);
end;
//將CheckException方法賦值給系統Application變量,在主Form的OnCreate事件中。
procedure TAutoRegActiveXFrm.FormCreate(Sender: TObject);
var
  DemoOcx: Variant; //變量聲明
begin
  Application.OnException := CheckException;
  //是否產生類名稱字符串錯誤
  try
    DemoOcx := CreateOleObject('Demo.Demo');
  except
    on EAbort:EOleSysError do
    if HResult(EAbort.ErrorCode) = CO_E_CLASSSTRING then
    begin
      if OleRegister('D:/Flash.ocx',1) then
        DemoOcx := CreateOleObject('Demo.Demo')
      else
      begin
        Application.MessageBox('控件註冊失敗,程序將沒法正常運行',PChar('註冊控件'),MB_OK+MB_ICONERROR);
        Application.Terminate;
      end;
    end;
  end;
end;
第二種方法:有時用到的*.OCX會用到不少DLL因此,這個時候若是,咱們仍是按照上面的方法去作,是比較麻煩的,因此我建議用一下方法:
1. 首先找到*.OCX所須要的DLL文件;
2. 把全部文件拷貝到程序所在目錄下;
3. 在程序初始化段中寫以下代碼:
winexec(pchar('regsvr32.exe -s'+getcurrentdir()+'/vcf132.ocx'),sw_show);
   實際上咱們只是把原來在MS-DOS手動安裝的步驟放到程序裏面作,
注意這裏註冊只要註冊vcf132.ocx便可;函數

 

 

原文:https://blog.csdn.net/chenamo9651/article/details/763546.net

相關文章
相關標籤/搜索