2012-06-28 80 views
1

我与德尔福application.I工作按如下方式创建一个表格:德尔福创建组件模板

enter image description here

我想通过代码使组件出这个控制。但不通过组件 - >创建组件模板 - >依此类推。

我如何制作组件模板不合格控件通过delphi代码。 Thanx提前。

+1

这个问题目前还不清楚。您是否希望在运行时通过代码*在屏幕截图中创建组件?或者你是否想创建一个*组件组,你可以将它作为一个组件添加到你的表单中?或者您是否想要保存这组组件(作为框架)并在项目中多次重复使用该组? – TLama

回答

3

如果您右键单击表格并选择查看为文本,那么您已经很长的路了。简单地用:=替换所有=,并通过添加.Create(Self)来创建所有组件。

所以这样的文字:

object Form1: TForm1 
    Left = 300 
    Top = 281 
    Width = 630 
    Height = 372 
    Caption = 'Form1' 
    Color = clBtnFace 
    Font.Charset = DEFAULT_CHARSET 
    Font.Color = clWindowText 
    Font.Height = -11 
    Font.Name = 'MS Sans Serif' 
    Font.Style = [] 
    OldCreateOrder = False 
    PixelsPerInch = 96 
    TextHeight = 13 
    object Label1: TLabel 
    Left = 14 
    Top = 28 
    Width = 32 
    Height = 13 
    Caption = 'Label1' 
    end 
    object Edit1: TEdit 
    Left = 63 
    Top = 24 
    Width = 121 
    Height = 21 
    TabOrder = 0 
    Text = 'Edit1' 
    end 
end 

应转换成类似:

type 
    TMyForm1 = class(TForm) 
    private 
    Label1: TLabel; 
    Edit1: TEdit; 
    public 
    constructor Create(AOwner: TComponent); override; 
    end; 

constructor TMyForm1.Create(AOwner: TComponent); 
begin 
    inherited Create(AOwner); 
    Width := 630; 
    Height := 372; 
    Caption := 'Form1'; 
    Color := clBtnFace; 
    ... 
    Label1 := TLabel.Create(Self); 
    with Label1 do 
    begin 
    Left := 14; 
    Top := 28; 
    Width := 32; 
    Height := 13; 
    Caption := 'Label1'; 
    end; 
    Edit1 := TEdit.Create(Self); 
    with Edit1 do 
    ... 
end; 

但也有工具,这个特殊的任务,请Are there any Delphi DFM to Delphi source code convertion tools?

4

或者,如果你想有一个控制组作为一个单独的组件,您可以像这样安装单位到一些包:

unit EditGroup; 

interface 

uses 
    SysUtils, Classes, Graphics, Controls, StdCtrls; 

type 
    TEditGroup = class(TCustomControl) 
    private 
    FButton: TButton; 
    FFirstEdit: TEdit; 
    FFirstLabel: TLabel; 
    FSecondEdit: TEdit; 
    FSecondLabel: TLabel; 
    protected 
    procedure Paint; override; 
    public 
    constructor Create(AOwner: TComponent); override; 
    destructor Destroy; override; 
    published 
    property Button: TButton read FButton; 
    property FirstEdit: TEdit read FFirstEdit; 
    property FirstLabel: TLabel read FFirstLabel; 
    property SecondEdit: TEdit read FSecondEdit; 
    property SecondLabel: TLabel read FSecondLabel; 
    end; 

procedure Register; 

implementation 

{ TEditGroup } 

constructor TEditGroup.Create(AOwner: TComponent); 
begin 
    inherited; 

    Width := 213; 
    Height := 104; 
    Color := clWhite; 

    FFirstLabel := TLabel.Create(Self); 
    FFirstLabel.SetSubComponent(True); 
    FFirstLabel.Parent := Self; 
    FFirstLabel.Top := 11; 
    FFirstLabel.Left := 8; 
    FFirstLabel.Name := 'FirstLabel'; 

    FFirstEdit := TEdit.Create(Self); 
    FFirstEdit.SetSubComponent(True); 
    FFirstEdit.Parent := Self; 
    FFirstEdit.Top := 8; 
    FFirstEdit.Left := 84; 
    FFirstEdit.Width := 121; 
    FFirstEdit.Name := 'FirstEdit'; 

    FSecondLabel := TLabel.Create(Self); 
    FSecondLabel.SetSubComponent(True); 
    FSecondLabel.Parent := Self; 
    FSecondLabel.Top := 39; 
    FSecondLabel.Left := 8; 
    FSecondLabel.Name := 'SecondLabel'; 

    FSecondEdit := TEdit.Create(Self); 
    FSecondEdit.SetSubComponent(True); 
    FSecondEdit.Parent := Self; 
    FSecondEdit.Top := 36; 
    FSecondEdit.Left := 84; 
    FSecondEdit.Width := 121; 
    FSecondEdit.Name := 'SecondEdit'; 

    FButton := TButton.Create(Self); 
    FButton.SetSubComponent(True); 
    FButton.Parent := Self; 
    FButton.Top := 71; 
    FButton.Left := 69; 
    FButton.Width := 75; 
    FButton.Name := 'Button'; 
end; 

destructor TEditGroup.Destroy; 
begin 
    FButton.Free; 
    FFirstEdit.Free; 
    FFirstLabel.Free; 
    FSecondEdit.Free; 
    FSecondLabel.Free; 
    inherited; 
end; 

procedure TEditGroup.Paint; 
begin 
    Canvas.Rectangle(ClientRect); 
end; 

procedure Register; 
begin 
    RegisterComponents('Stack Overflow', [TEditGroup]); 
end; 

end. 

这是它看起来像在设计时:

enter image description here

+1

+1为了更好地演示如何使它成为可定制的组件@designtime。 – NGLN

+0

@NGLN,谢谢!但我认为OP要的是你所描述的(以及我已经提出的)。 – TLama

+1

哦,我一直在寻找那样的东西,现在我终于找到了!真的很好的例子! – DanilGholtsman