(急)请问对几个不同的窗体任意创建一个,可不可以新将创建的窗体赋给一个公共变量,通过...(200分)

J

jiefeij

Unregistered / Unconfirmed
GUEST, unregistred user!
请问可不可以对几个不同的窗体任意创建一个,将新创建的窗体赋给一个公共变量,
通过这个窗体变量来访问该窗体中的控件和事件。
 
你可以详细看看类方法的概念

或可看看以下的文章:

在C++中,我们可以用static来声明一个属于类而不是类的某个具体实例的函数。许多人以为Object Pascal没有类似的功能,其实是有的,只要将函数声明为类方法(class procedure或者class function)就可以了。
例如:
TForm1=class(TForm)
public
class procedure T;
end;
implementation
class procedure TFomr1.T;
begin
end;
你可以在项目文件中验证它是不是真的static:
begin
TForm1.T;
Application.Initialzie;
Application.CreateForm(TForm1,Form1);
Application.Run;
end;
class procedure/class function在VCL中也是相当重要的一类特殊方法。TObject的ClassName,ClassNameIs,ClassPoint,ClassInfo这四个方法全都是class function。虽然你在实际的编程中99.99999999999%的时间都不会用到它们,不过这四个函数堪称构筑整个VCL的基石。
  值得一提的是,与C++中的static member function不同,Object Pascal中的class method能够具有多态的性质。例如,ClassName是TObject的一个class method,但是你调用TForm.ClassName得到的就是'TForm',调用TButton.ClassName得到的就是'TButton',等等。静态方法能够做到多态,岂不是非常神奇?这一切都是通过RTTI才做到的。你可以想象,如果ClassName用一般的virtual method来声明的话,就必须在每个派生类中都必须重载它才能实现类名称的正确映射,那将是多么痛苦的一件事情。
Class Reference是Object Pascal中不太为人所知晓的概念。它的语法很简单,比如,
TClass=class of TObject;
Class Reference有什么用呢?用一个小例子来说明。在Form上面放一个RadioGroup,其中包括Radio Button, Check Box, Button三项。代码如下:
type
TForm1=class(TForm)
...
private
ControlRef:TControlClass;
Counter : integer;
...
end;
implementation
procedure TForm1.FormCreate(Sender:TObject);
begin
Counter := 0;
end;
procedure TForm1.RadioGroup1Click(Sender:TObject);
begin
case RadioGroup1.ItemIndex of
0: ControlRef:=TRadioButton;
1: ControlRef:=TCheckBox;
2: ControlRef:=TButton;
end;
end;
procedure TForm1.FormMouseDown(Sender:TObject;Button:TMouseButton;Shift:TShiftState;X,Y:integer);
var
Control : TControl;
ControlName : string;
begin
if Button=mbLeft then begin
Control := TControlRef.Create(Self);
with Control do begin
Visible := False;
Parent := Self;
Left := X;
Top := Y;
ControlName := Control.ClassName + IntToStr(Counter);
Delete(ControlName,1,1);
Name := ControlName;
visible := True;
end;
end;
end;
  Class Reference揭示了Object Pascal的一个令人惊讶的能力。在OO语言的一般概念中,Constructor不可能是virtual,我们调用TButton.Create建立的就是Button,TForm.Create建立的就是TForm,依此类推。但是,籍由Class Reference,VCL就拥有了virtual Construction的能力,在上面的代码中,通过单独的一个ControlRef.Create,我们建立起来的可以是任何TControl的派生对象。正是由于这种能力,Form Designer和Object Inspector才有办法操作各种各样不同类型的构件,而不管这些构件是Delphi内置的,还是我们从其他地方得到的,或者是我们自己编写的。Class Reference的概念在其他地方也被称作meta-class,是用来描述类信息的特殊类型。
   实际上,真正的幕后英雄是RTTI,通过RTTI,不仅使得类拥有了virtual construction的功能,而且类中的class method也有了virtual的性质。例如,TObject中的ClassName方法声明为class function ClassName:string,按道理说static method是不可能做到多态的,但实际上,我们调用TButton.ClassName得到的就是TButton,调用TForm.ClassName得到的就是TForm,通过RTTI,不可能的事情变成了可能,而且而避免了在每个派生类都要重载ClassName的麻烦。
  在MFC中,如果一个对象满足下列条件:1。它是从CObject派生的;2。它的声明中包含DECLARE_DYNAMIC,并且在某个地方实现了IMPLEMENT_DYNAMIC,那么它也可以通过GetRuntimeClass()->CreateObject来实现类似的功能。
   所不同的是,MFC的实现方法是通过宏来在内存中建立起一个完整的CRuntimeClass表格,而Object Pascal中,由于单根继承的特性,所有对象天生就具备了这些功能。MFC的内存表格是通过程序员的声明而建立的,而Object Pascal中的RTTI表格是编译器自动生成的,彼此实现方法不同,但背后的思想是基本一致的。
  通过RTTI,不仅对象可以动态生成,而且对象的属性也可以动态设置。下面这段代码不是来自《Mastering Delphi 6》,而是来自陈宽达先生的《Delphi深度历险》,它同样揭示了Object Pascal通过RTTI而具备的强大功能和灵活性:
var
i: integer;
propInfo: PPropInfo;
begin
for i:=0 to ComponentCount-1 do begin
PropInfo := GetPropInfo(Components.ClassInfo,’Color’);
if PropInfo<>nil then
SetOrProp(Components, PropInfo, clRed);
end;
end;

 
上面的老兄说的这么周到,我就不补充了。
 
可以这样:
obj:TForm;
obj:=createForm(myForm)//myform是其中的一个
if obj.classname="form1" then
......
if obj.classname="form2" then
......
if ...

用ClassName、componentName、等来区分后,再分别进行处理
 
您好,vine:
我现在是有一个主窗体和一些子窗体,运行时,在主窗体上有许多选项,
选择不同的选项将会打开不同的窗体,并给子窗体的一些控件(如Edit赋值),执行子
窗体的按钮单击事件。将结果显示(DBGRID)。如果一个一个窗体创建将会很麻烦,很多又
是重复的代码。(子窗体的查询条件相同)
 
您好,wwolf:
你那样的话,obj好象不能获得FORM1(如得不到obj.combobox1)上的控件。
我还是有很多重复代码。
 
您好,wwolf:
那怎么执行事件呢(假设Speedbtnclick事件)。
 
procedure aaa(可能有参数);

aaa:= myform.某事件

事件一定是那个窗体里激活的。
如果要主动执行可以用上面的方法
 
假设你的几个子窗口上都有一个 Edit1 和一个 Button1。那么可以这样:

先看第一个子窗口:
...
var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
ShowMessage('I am Form2');
end;

initialization
RegisterClass(TForm2); // 初始化的时候注册你的窗口类

end.

第二个子窗口:
...
var
Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
begin
ShowMessage('I am Form3');
end;

initialization
RegisterClass(TForm3); // 初始化的时候注册你的窗口类

end.

然后看主窗口如何使用:
...
type
TFormClass = class of TForm;

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
ChildFormClass: TFormClass;
ChildForm: TForm;

implementation

uses Unit2, Unit3;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
// 取得需要的窗口类,如 'TForm2'
ChildFormClass := TFormClass(GetClass(Edit1.Text));
if Assigned(ChildFormClass) then
begin
ChildForm := ChildFormClass.Create(Self); // 创建该类的实例
ChildForm.Show; // 显示
// 先找到该窗口上的 Edit1 编辑框,然后对其赋值:
TEdit(ChildForm.FindComponent('Edit1')).Text := 'Hello World';
// 先找到该窗口上的 Button1 按钮,然后调用其单击方法:
TButton(ChildForm.FindComponent('Button1')).Click;
ChildForm.Free; // 何时释放,自己看着办:)
end
else
ShowMessage('找不到对应类!');
end;

end.

当然还记得在 Project|Options 选项里面的 Forms 选项页里面把 "Auto-created forms"
里面的 Form2 和 Form3 移到右边的 "Available forms" 里面,以使系统不要自动创建
那两个窗口,我们自己根据需要创建。

 
谢谢各位
 
老兄,vine说的这么好,唉!
 
顶部