我想在ListBox中动态列出我的项目中存在的所有表单的名称,然后通过单击它们中的每一个,列出该表单上存在的另一个ListBox中的所有按钮.
但我不知道它是否可以实施以及如何实施.
请帮我.
谢谢
如果您使用的是Delphi 2010,则可以使用RTTI列出所有已注册的(在应用程序中以某种方式使用)表单类:
uses
TypInfo, RTTI;
procedure ListAllFormClasses(Target: TStrings);
var
aClass: TClass;
context: TRttiContext;
types: TArray<TRttiType>;
aType: TRttiType;
begin
context := TRttiContext.Create;
types := context.GetTypes;
for aType in types do begin
if aType.TypeKind = tkClass then begin
aClass := aType.AsInstance.MetaclassType;
if (aClass <> TForm) and aClass.InheritsFrom(TForm) then begin
Target.Add(aClass.ClassName);
end;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
你必须以某种方式注意链接器没有完全删除类(因此上面的注册提示).否则你无法用所描述的方法获得该类.
表单通常使用Screen.Forms属性列出,例如:
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
Memo1.Lines.Clear;
for I:= 0 to Screen.CustomFormCount - 1 do
Memo1.Lines.Add(Screen.Forms[I].Caption);
end;
Run Code Online (Sandbox Code Playgroud)