通过字符串,类的引用,创建窗体。[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 yeeyee 的 blog

//控件单元。

{*
单元说明:     创建模式窗口,和非模式窗口的类,保证非模式窗口只创建一次。
作者        :     笔名:易  一    英文名:yeeyee
e-mail     :    jane1437@163.com
创建时间:          2005年5月20日
及最后修改时间:
修改人修改时间及:
修改说明:
版权声明:      版权所有,转载请注明本人邮箱和笔名。
*}
unit showformclass;

interface

uses
  sysutils, classes,dialogs, forms;

type          
  tshowformclass = class(tcomponent)
  private
    { private declarations }
    //保存要创建的窗体的类名
    ffrmname:string;   
    //判断窗体是否存在。
    function isformexist:boolean;
    //得到窗体。
    function getexistform:tform;
    //创建一个类
    function createaclass(const aclassname: string): tform;
  protected
    { protected declarations }
  public
    { public declarations }
    constructor create(aowner: tcomponent); virtual;
    destructor destroy; override;
   
    //创建并显示窗体。模式窗体。
    procedure showmodalform(const astrform:string);overload;
    procedure showmodalform(aformclass:tformclass);overload;
    //创建并显示窗体。非模式窗体。
    procedure showmodallessform(const astrform:string);overload;
    procedure showmodallessform(aformclass:tformclass);overload;

  published
    { published declarations }
  end;

procedure register;

implementation


procedure register;
begin
  registercomponents('yeeyee', [tshowformclass]);
end;

constructor tshowformclass.create(aowner: tcomponent);
begin
  inherited create(aowner);
end;

destructor tshowformclass.destroy;
begin
  inherited destroy;
end;

function tshowformclass.getexistform:tform;
var
  i:integer;
begin
  for i := 0 to (application.componentcount - 1) do
  begin
    if (application.components[i] is tform) then
    begin
      //注意,关键判断这个类型名称是否存在。
      if (application.components[i] as tform).classtype.classname = ffrmname then
      begin
        result:=(application.components[i] as tform);
        exit;
      end
    end;
  end;
end;

function tshowformclass.isformexist:boolean;
var
  i:integer;
begin
  result:=false;
  for i := 0 to (application.componentcount - 1) do
  begin
    if (application.components[i] is tform) then
    begin
      //注意,关键判断这个类型名称是否存在。
      if (application.components[i] as tform).classtype.classname = ffrmname then
      begin
        result:=true;
        exit;
      end
    end;
  end;
end;

//创建一个类
function tshowformclass.createaclass(const aclassname: string): tform;
var
  lformclass : tformclass;
  lform: tform;
begin
  lformclass := tformclass(findclass(aclassname));
  lform := lformclass.create(application);
  result := lform;
end;

//创建并显示窗体。模式窗体。传入字符串。
procedure tshowformclass.showmodalform(const astrform:string);
var
  lform: tform;
begin
  ffrmname:=astrform;
  lform := createaclass(ffrmname);
  try
    lform.showmodal;
  finally
    lform.free;
  end;
end;

//创建并显示窗体。模式窗体。传入类的引用。
procedure tshowformclass.showmodalform(aformclass:tformclass);
begin
  with aformclass.create(application) do
  begin
    try
      showmodal;
    finally
      free;
    end;
  end;
end;

本文关键:通过字符串,类的引用,创建窗体。
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top