我正在尝试将使用自定义工具创建的一组表单转换为Delphi表单。我尝试在运行时添加所有必需的组件,然后使用WriteComponentResFile创建DFM文件。
在我尝试添加TPageControl和TabSheets之前,我的所有初始测试看起来都很好。当前的表单可以有多个页面,所以我打算使用PageControl对其进行镜像。问题是,我添加到TabSheet的任何组件都不会流出到DFM。如果我显示表单,它看起来很好,但是WriteComponentResFile缺少一些东西。
我正在写出一个相应的pas文件,这样一旦完成,我就可以在IDE中打开它。我们的目标是不再使用自定义表单设计器,开始使用Delphi IDE来设计表单设计器。
下面是一些示例代码,展示了我是如何创建组件的:
procedure WriteFormAsDFM(OutputFileName: string);
var
PageIndex: integer;
PageCount: Integer;
OutputForm: TForm;
Pages: TPageControl;
NewPage: TTabSheet;
NewLabel: TLabel;
begin
OutputForm := TForm.Create(nil);
OutputForm.Name := ChangeFileExt(ExtractFileName(OutputFileName), '');
OutputForm.Caption := OutputForm.Name;
OutputForm.Height := 300;
OutputForm.Width := 300;
Pages := TPageControl.Create(OutputForm);
Pages.Parent := OutputForm;
Pages.Top := 50;
Pages.Left := 0;
Pages.Height := 200;
Pages.Width := 200;
NewLabel := TLabel.Create(OutputForm);
NewLabel.Parent := OutputForm;
NewLabel.Caption := 'Label on Form';
//write pages
PageCount := 2;
for PageIndex := 0 to PageCount - 1 do
begin
NewPage := TTabSheet.Create(Pages);
NewPage.Parent := Pages;
NewPage.PageControl := Pages;
NewPage.Caption := 'Page ' + IntToStr(PageIndex);
NewPage.Name := 'tsPage' + IntToStr(PageIndex);
NewLabel := TLabel.Create(NewPage);
NewLabel.Parent := NewPage;
NewLabel.Caption := 'Label on ' + NewPage.Caption;
end;
WriteComponentResFile(OutputFileName, OutputForm);
//WritePasFile(OutputFileName, OutputForm);
OutputForm.ShowModal;
FreeAndNil(OutputForm);
end;下面是输出的DFM文件。您可以看到表单上的标签已创建,但没有添加到TabSheets中的标签。
object Form123: TForm
Left = 69
Top = 69
Caption = 'Form123'
ClientHeight = 264
ClientWidth = 284
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object TLabel
Left = 0
Top = 0
Width = 67
Height = 13
Caption = 'Label on Form'
end
object TPageControl
Left = 0
Top = 50
Width = 200
Height = 200
ActivePage = tsPage0.Owner
TabOrder = 0
object tsPage0: TTabSheet
Caption = 'Page 0'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
end
object tsPage1: TTabSheet
Caption = 'Page 1'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
end
end
end发布于 2009-12-31 01:46:17
尝试以组件所有者的身份使用表单。
NewPage := TTabSheet.Create(OutputForm);
NewLabel := TLabel.Create(OutputForm);
https://stackoverflow.com/questions/1981380
复制相似问题