装载BPL中的类,数据模块,窗体控件

{*
单元说明    : 装载BPL中的类,数据模块,窗体控件
作者        :   
笔名        :  易  一    英文名:yeeyee
E-Mail      :  jane1437@163.com
My Blog     :   http://blog.csdn.net/yeeyee/
QQ          :   282624758
创建时间    :  2005年10月18日
及最后修改时间:
修改人修改时间:
修改说明:        
版权声明:      版权所有,转载请注明本人邮箱,笔名,
                         并保证文章的完整性。
调用说明:
*}

unit LoadClass;

interface

uses
  SysUtils, Windows, Classes, Forms;

type
  TLoadClass = class(TComponent)
  private
    { Private declarations }
    FPkgPath:string;
    FPkgName:string;
    FPkgPathName:string;
    FClassName:string;

    procedure SetPkgPath(const AValue:string);
    procedure SetPkgName(const AValue:string);
    procedure SetPkgPathName(const AValue:string);
    procedure SetClassName(const AValue:string);

    function FnIsFormExist(const AFormName:string):boolean;
    function GetExistForm(const AFormName:string):TCustomForm;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;    

    function CreateSingleTonForm(AOwner: TComponent;const APkgName:string;const AClassName:string):TCustomForm;
   
    procedure ShowModalForm(const APkgName:string;const AClassName:string;ATag:integer=0);reintroduce;overload;
    procedure ShowModalForm(AOwner: TComponent;const APkgName:string;const AClassName:string;ATag:integer=0);reintroduce;overload;

    procedure ShowModalLessForm(const APkgName:string;const AClassName:string;ATag:integer=0);reintroduce;overload;
    procedure ShowModalLessForm(AOwner: TComponent;const APkgName:string;const AClassName:string;ATag:integer=0);reintroduce;overload;


    function CreateDataModule(AOwner: TComponent;const APkgName:string;const AClassName:string):TDataModule;

    procedure CreateComponent(const APkgName:string;const AClassName:string);
  published
    { Published declarations }
    property YPkgPath:string read FPkgPath write SetPkgPath;
    property YPkgName:string read FPkgName write SetPkgName;
    property YPkgPathName:string read FPkgName write SetPkgPathName;
    property YClassName:string read FClassName write SetClassName;
  end;

//procedure Register;

implementation

uses TestComp;

{
procedure Register;
begin
  RegisterComponents('Yeeyee', [TLoadClass]);
end;
}

constructor TLoadClass.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FPkgPath:='D:/Projects/TestAutoUpdate/Bin/';
end;

destructor TLoadClass.Destroy;
begin
  inherited Destroy;
end;

procedure TLoadClass.SetPkgPath(const AValue:string);
begin
  if FPkgPath<>AValue then
  begin
    FPkgPath:=AValue;
  end;
end;

procedure TLoadClass.SetPkgName(const AValue:string);
begin
  if FPkgName<>AValue then
  begin
    FPkgName:=AValue;
  end;
end;

procedure TLoadClass.SetPkgPathName(const AValue:string);
begin
  if FPkgName<>AValue then
  begin
    FPkgName:=AValue;
  end;
end;

procedure TLoadClass.SetClassName(const AValue:string);
begin
  if FClassName<>AValue then
  begin
    FClassName:=AValue;
  end;
end;

function TLoadClass.FnIsFormExist(const AFormName:string):boolean;
var
  I:integer;
begin
  Result:=False;
  for I := 0 to (Application.ComponentCount - 1) do
  begin
    if (Application.Components[I] is TCustomForm) then
    begin
      //注意,关键判断这个类型名称是否存在。
      if (Application.Components[I] as TCustomForm).ClassType.ClassName = AFormName then
      begin
        Result:=True;
        exit;
      end
    end;
  end;
end;

function TLoadClass.GetExistForm(const AFormName:string):TCustomForm;
var
  I:integer;
begin
  Result:=nil;
  for I := 0 to (Application.ComponentCount - 1) do
  begin
    if (Application.Components[I] is TCustomForm) then
    begin
      //注意,关键判断这个类型名称是否存在。
      if (Application.Components[I] as TCustomForm).ClassType.ClassName = AFormName then
      begin
        Result:=(Application.Components[I] as TCustomForm);
        exit;
      end
    end;
  end;
end;

procedure TLoadClass.ShowModalForm(const APkgName:string;const AClassName:string;ATag:integer=0);
var
  hPkgModule: HModule;
  AClass: TPersistentClass;
  LCustForm:TCustomForm;
begin
  FClassName:=AClassName;
  FPkgName:=APkgName;
  FPkgPathName:=FPkgPath+FPkgName;
  hPkgModule := LoadPackage(FPkgPathName); 

  try
    if hPkgModule <> 0 then
    begin
      AClass := GetClass(FClassName);
      if AClass <> nil then
      begin
        LCustForm:=(TComponentClass(AClass).Create(nil) as TCustomForm);
        try
          LCustForm.Tag:=ATag;
          LCustForm.ShowModal;
        finally
          LCustForm.Free;
        end;
      end; //End if if AClass <> nil then
    end;   //End if hPkgModule <> 0 then
  finally
    UnloadPackage(hPkgModule);
  end;
end;

procedure TLoadClass.ShowModalForm(AOwner: TComponent;const APkgName:string;const AClassName:string;ATag:integer=0);
var
  hPkgModule: HModule;
  AClass: TPersistentClass;
  LCustForm:TCustomForm;
begin
  FClassName:=AClassName;
  FPkgName:=APkgName;
  FPkgPathName:=FPkgPath+FPkgName;
  hPkgModule := LoadPackage(FPkgPathName); 

  try
    if hPkgModule <> 0 then
    begin
      AClass := GetClass(FClassName);
      if AClass <> nil then
      begin
        LCustForm:=(TComponentClass(AClass).Create(AOwner) as TCustomForm);
        try
          LCustForm.Tag:=ATag;
          LCustForm.ShowModal;
        finally
          LCustForm.Free;
        end;
      end; //End if if AClass <> nil then
    end;   //End if hPkgModule <> 0 then
  finally
    UnloadPackage(hPkgModule);
  end;
end;

function TLoadClass.CreateSingleTonForm(AOwner: TComponent;const APkgName:string;const AClassName:string):TCustomForm;
var
  hPkgModule: HModule;
  AClass: TPersistentClass;
  //LCustForm:TCustomForm;
  LCustForm:TCustomForm;
begin
  FClassName:=AClassName;
  FPkgName:=APkgName;
  FPkgPathName:=FPkgPath+FPkgName;
  hPkgModule := LoadPackage(FPkgPathName);
  try
    if hPkgModule <> 0 then
    begin
      AClass := GetClass(FClassName);
      if AClass <> nil then
      begin
        //窗体不存在,则创建。
        if not FnIsFormExist(FClassName) then
        begin
          LCustForm:=(TComponentClass(AClass).Create(Application) as TCustomForm);
        end
        else
        begin
          //存在,则得到窗体。带到最前头。
          LCustForm:=GetExistForm(FClassName);
          LCustForm.BringToFront;
        end;
        Result:=LCustForm;
      end;  //End if if AClass <> nil then
    end;   //End if hPkgModule <> 0 then
  finally
    UnloadPackage(hPkgModule);
  end;
end;

procedure TLoadClass.ShowModalLessForm(AOwner: TComponent;const APkgName:string;const AClassName:string;ATag:integer=0);
var
  hPkgModule: HModule;
  AClass: TPersistentClass;
  LCustForm:TCustomForm;
begin
  FClassName:=AClassName;
  FPkgName:=APkgName;
  FPkgPathName:=FPkgPath+FPkgName;
  hPkgModule := LoadPackage(FPkgPathName);
  try
    if hPkgModule <> 0 then
    begin
      AClass := GetClass(FClassName);
      if AClass <> nil then
      begin
        //窗体不存在,则创建。
        if not FnIsFormExist(FClassName) then
        begin
          LCustForm:=(TComponentClass(AClass).Create(AOwner) as TCustomForm);
          LCustForm.Tag:=ATag;
          LCustForm.Show;
        end
        else
        begin
          //存在,则得到窗体。带到最前头。
          LCustForm:=GetExistForm(FClassName);
          LCustForm.BringToFront;
        end;
      end; //End if if AClass <> nil then
    end;   //End if hPkgModule <> 0 then
  finally
    UnloadPackage(hPkgModule);
  end;
end;

procedure TLoadClass.ShowModalLessForm(const APkgName:string;const AClassName:string;ATag:integer=0);
var
  hPkgModule: HModule;
  AClass: TPersistentClass;
  LCustForm:TCustomForm;
begin
  FClassName:=AClassName;
  FPkgName:=APkgName;
  FPkgPathName:=FPkgPath+FPkgName;
  hPkgModule := LoadPackage(FPkgPathName);
  try
    if hPkgModule <> 0 then
    begin
      AClass := GetClass(FClassName);
      if AClass <> nil then
      begin
        //窗体不存在,则创建。
        if not FnIsFormExist(FClassName) then
        begin
          LCustForm:=(TComponentClass(AClass).Create(Application) as TCustomForm);
          LCustForm.Tag:=ATag;
          LCustForm.Show;
        end
        else
        begin
          //存在,则得到窗体。带到最前头。
          LCustForm:=GetExistForm(FClassName);
          LCustForm.BringToFront;
        end;
      end; //End if if AClass <> nil then
    end;   //End if hPkgModule <> 0 then
  finally
    UnloadPackage(hPkgModule);
  end;
end;

function TLoadClass.CreateDataModule(AOwner: TComponent;const APkgName:string;const AClassName:string):TDataModule;
var
  hPkgModule: HModule;
  AClass: TPersistentClass;
  LDataModule:TDataModule;
begin
  FClassName:=AClassName;
  FPkgName:=APkgName;
  FPkgPathName:=FPkgPath+FPkgName;
  hPkgModule := LoadPackage(FPkgPathName);
  try
    if hPkgModule <> 0 then
    begin
      AClass := GetClass(FClassName);
      if AClass <> nil then
      begin
        Result:=(TComponentClass(AClass).Create(AOwner) as TDataModule);
      end;
    end;
  finally
    UnloadPackage(hPkgModule);
  end;    
end;

procedure TLoadClass.CreateComponent(const APkgName:string;const AClassName:string);
var
  hPkgModule: HModule;
  AClass: TPersistentClass;
  LComponent:TComponent;
begin
  FClassName:=AClassName;
  FPkgName:=APkgName;
  FPkgPathName:=FPkgPath+FPkgName;
  hPkgModule := LoadPackage(FPkgPathName);
  try
    if hPkgModule <> 0 then
    begin
      AClass := GetClass(FClassName);
      if AClass <> nil then
      begin
        LComponent:=(TComponentClass(AClass).Create(Application) as TComponent);
      end;
    end;
  finally
    UnloadPackage(hPkgModule);
  end;
end;

end.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值