注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

鑫淼梦园的博客

圆你的梦想 从这里开始

 
 
 

日志

 
 

通过类名动态创建Form  

2013-10-20 18:38:04|  分类: delphi xe5 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

通过类名动态创建Form  

主窗体:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ImgList, ActnList, ToolWin;

type
  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    ActionList1: TActionList;
    ImageList1: TImageList;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    StatusBar1: TStatusBar;
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
  public

    vform: TForm;
    vFormClass: TFormClass;
    function ExistFormClass(Name: string): boolean;
    function IsInclude(FormClass: TFormClass): boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.ExistFormClass(Name: string): boolean;
var
  i: integer;
begin
  result := false;
  if self.MDIChildCount = 0 then exit;
  for i := 0 to self.MDIChildCount - 1 do
    if self.MDIChildren[i].ClassName = Name then
    begin
      result := True;
      break;
    end;
end;

function TForm1.IsInclude(FormClass: TFormClass): boolean;
var
  i: Integer;
  form: TObject;
begin
  result := false;
  for i := 0 to MDIChildCount do
  begin
    form := MDIChildren[i];
    if form is formclass then
      result := true;
  end;
end;

procedure TForm1.ToolButton1Click(Sender: TObject);
begin
  vFormClass := TFormClass(FindClass('TForm3'));
  if (vformClass <> nil) and (not ExistFormClass('TForm3')) then
  begin
    vForm := vformClass.Create(application);
    try
      vform.DisableAutoRange;
      vform.DisableAlign;
//      vform.FormStyle := fsMDIChild;
    finally
      vform.EnableAutoRange;
      vform.EnableAlign;
    end;
  end;

end;

procedure TForm1.ToolButton2Click(Sender: TObject);
begin
  showmessage(IntToStr(self.MDIChildCount));
  showmessage(IntToStr(Application.ComponentCount));  
end;

procedure TForm1.ToolButton3Click(Sender: TObject);
begin
  if vform = nil then
    showmessage('nil')
  else
    showmessage('not nil');
end;

procedure TForm1.ToolButton4Click(Sender: TObject);
begin
  vFormClass := TFormClass(FindClass('TForm3'));
  if (vformClass <> nil) and not IsInclude(vFormClass) then
  begin
    vForm := vformClass.Create(application);
    try
      vform.Show;
    finally
    end;
  end;
end;

end.

 

调用窗体:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ImgList, ActnList, ToolWin;

type
  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    ActionList1: TActionList;
    ImageList1: TImageList;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    StatusBar1: TStatusBar;
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
  public

    vform: TForm;
    vFormClass: TFormClass;
    function ExistFormClass(Name: string): boolean;
    function IsInclude(FormClass: TFormClass): boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.ExistFormClass(Name: string): boolean;
var
  i: integer;
begin
  result := false;
  if self.MDIChildCount = 0 then exit;
  for i := 0 to self.MDIChildCount - 1 do
    if self.MDIChildren[i].ClassName = Name then
    begin
      result := True;
      break;
    end;
end;

function TForm1.IsInclude(FormClass: TFormClass): boolean;
var
  i: Integer;
  form: TObject;
begin
  result := false;
  for i := 0 to MDIChildCount do
  begin
    form := MDIChildren[i];
    if form is formclass then
      result := true;
  end;
end;

procedure TForm1.ToolButton1Click(Sender: TObject);
begin
  vFormClass := TFormClass(FindClass('TForm3'));
  if (vformClass <> nil) and (not ExistFormClass('TForm3')) then
  begin
    vForm := vformClass.Create(application);
    try
      vform.DisableAutoRange;
      vform.DisableAlign;
//      vform.FormStyle := fsMDIChild;
    finally
      vform.EnableAutoRange;
      vform.EnableAlign;
    end;
  end;

end;

procedure TForm1.ToolButton2Click(Sender: TObject);
begin
  showmessage(IntToStr(self.MDIChildCount));
  showmessage(IntToStr(Application.ComponentCount));  
end;

procedure TForm1.ToolButton3Click(Sender: TObject);
begin
  if vform = nil then
    showmessage('nil')
  else
    showmessage('not nil');
end;

procedure TForm1.ToolButton4Click(Sender: TObject);
begin
  vFormClass := TFormClass(FindClass('TForm3'));
  if (vformClass <> nil) and not IsInclude(vFormClass) then
  begin
    vForm := vformClass.Create(application);
    try
      vform.Show;
    finally
    end;
  end;
end;

end.

  评论这张
 
阅读(217)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017