2009-12-30 39 views
0

的组件我试图将一组使用自定义工具创建的窗体转换为Delphi窗体。我试图在运行时添加所有必要的组件,然后使用WriteComponentResFile创建DFM文件。WriteComponentResFile不包括动态添加到TTabSheet

我尝试添加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 

回答

5

尝试使用窗体作为组件的所有者。

NewPage:= TTabSheet.Create(OutputForm);

NewLabel:= TLabel.Create(OutputForm);

+0

谢谢你照顾它。 – 2009-12-30 18:01:43