Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用配置文件创建动态GUI_Xml_Delphi_Forms_User Interface_Generics - Fatal编程技术网

Xml 使用配置文件创建动态GUI

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

可以使用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.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面板-此行实际上将控件放置在父面板上

您可以在Torry's using RTTI上找到一些示例:

是的,请看一下

增加终极灵活性和力量 使用本机将其导入到应用程序中 Pascal或基本脚本和完整IDE (综合发展环境) 使用可视化窗体设计器,对象 督察,等等

查看或。

还使用XML文件描述GUI,然后在运行时创建GUI。不过,我不知道它是否可以与Delphi一起使用。

是的,我们可以:)我已经为一个只使用文本框、规则(行)和图形的页面设计器完成了这项工作,但它应该适用于所有已注册的控件

[即时代码近似值]

    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