Xml 使用配置文件创建动态GUI
可以使用xml等的配置模式为Delphi应用程序创建GUI。。。文件任何这样的操作框架都存在。使用类似脚本的语言很容易,但是我们可以在Delphi中模拟这种行为吗Xml 使用配置文件创建动态GUI,xml,delphi,forms,user-interface,generics,Xml,Delphi,Forms,User Interface,Generics,可以使用xml等的配置模式为Delphi应用程序创建GUI。。。文件任何这样的操作框架都存在。使用类似脚本的语言很容易,但是我们可以在Delphi中模拟这种行为吗 我需要免费的图书馆。是的,有可能。这个的伪代码是这样的 var AParent:Tpanel; Edit:TControl; for i := 0 to ConfigItems.Count - 1 do begin if (ConfigItems[i].Type = 0) then Edit := TEdit.Creat
我需要免费的图书馆。是的,有可能。这个的伪代码是这样的
var
AParent:Tpanel;
Edit:TControl;
for i := 0 to ConfigItems.Count - 1 do
begin
if (ConfigItems[i].Type = 0) then Edit := TEdit.Create(AParent) as TControl
else Edit := TAnotherEditOrAnotherControlType.Create(APanel) as TControl;
//assume 20 pixels for each control, so thay will be placed one below another
Edit.Top := i * 20;
//Left in this case can be fixed
Edit.Left := 10;
Edit.Parent := AParent;
end;
这将在声明为AParent的TPanel上创建少量TEdit或其他控件(例如,TanotherEditorNotherControlType,但如果您将Edit变量声明为TControl,则可以创建所需的任何控件)。
当然,您可以声明big CASE语句,并创建适当类型的控件,而不是IF子句。
重要的线路是
- 添加父项作为动态控件构造函数的参数(以便可以自动释放动态控件)
- 将“动态控件父对象”设置为Apart面板-此行实际上将控件放置在父面板上
var
i, itemCount: Integer;
AClassName: string;
AnItemClass: TSomeBaseClass;
AnItem: TSomeDrivedBaseClass
ARect: TRect;
begin
// just so we have an initail size
ARect.Left := 100;
ARect.Top := 100;
ARect.Bottom := 200;
ARect.Right := 200;
// Alist is a specialised TStringList
for i := 0 to itemCount - 1 do
begin
AClassName := Alist.ByKey['Class' + IntToStr(i)]; // locate class name
AnItemClass := TSomeBaseClass(GetClass(AClassName)); // ClassName must be registered
AnItem := AnItemClass.Create(OwnerComponent, ARect, AParent);
AnItem.LoadFromFile(IntToStr(i), AList); // specialised loader that reads and sets all necessary properties
AddItemToComponentList(AnItem); // Add to form / frame / panel whatever
end;
end;
当然,您首先需要一个“表单设计器”,它可以在开始时保存设计-保存过程与上述过程正好相反…我将把它作为练习留给读者。只需稍加修改,您就可以使用Delphi读取DFM文件:)您可以从流和文件中保存和加载DFM文件。您可以保存/加载整个表单,也可以仅保存/加载组件及其子级 乙二醇 作为二进制:
AStream.WriteComponent(AComponent);
MyComponent:= Result:= AStream.ReadComponent(AComponent);
全文如下:
procedure WriteComponentAsText(AStream: TStream; AComponent: TComponent);
var
BinStream:TMemoryStream;
begin
BinStream := TMemoryStream.Create;
try
BinStream.WriteComponent(AComponent);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, AStream);
finally
BinStream.Free
end;
end;
function ReadComponentAsText(AStream: TStream; AComponent: TComponent): TComponent;
var
BinStream:TMemoryStream;
begin
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(AStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result:= BinStream.ReadComponent(AComponent);
finally
BinStream.Free
end;
end;
您需要使用RegisterClass注册任何要保存或加载的类:
RegisterClass(TPanel);
席似乎完全符合我的要求,只有一个。这不是免费的。首先,我应该在问题中提到这一点。特别是libglade2.pas