【问题标题】:WriteComponentResFile is not including components dynamically added to TTabSheetWriteComponentResFile 不包括动态添加到 TTabSheet 的组件
【发布时间】:2009-12-30 17:33:51
【问题描述】:

我正在尝试将使用自定义工具创建的一组表单转换为 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

【问题讨论】:

    标签: delphi delphi-2007


    【解决方案1】:

    尝试使用表单作为组件的所有者。

    NewPage := TTabSheet.Create(OutputForm);

    NewLabel := TLabel.Create(OutputForm);

    【讨论】:

      猜你喜欢
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      相关资源
      最近更新 更多