这个问题我已经搞定。^_^<br><br>条件:1.要添加的Form(权且叫子窗体)需按指定格式做:所有的<br> 内容都用若干个GroupBox容器装上(当然也可以改用Panel或其<br> 它的容器),按钮加上1-4的Tag值(当然你也可以自己改成其它<br> 数)。<br> 2.做一个空的Form(权且叫父窗体),放上一个PageControl<br> (Left和Top一定要设置好,其它属性随便),命名为pcGlobal。<br><br>其余的你看以下代码:<br><br>type<br> TSubForm = class(TObject)<br> private<br> FTabSheet: TTabSheet;<br> FActive: Boolean;<br> FBoxes, FButtons: TList;<br> FForm: TForm;<br> FPageControl: TPageCOntrol;<br> procedure SetActive(Value: Boolean);<br> protected<br> public<br> constructor Create(AForm: TForm; APageControl: TPageControl);<br> destructor Destroy; override;<br> property Active: Boolean read FActive write SetActive;<br> end;<br><br> TfmOption = class(TForm)<br> pcGlobal: TPageControl;<br> procedure FormCreate(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br> procedure FormActivate(Sender: TObject);<br> procedure pcGlobalChange(Sender: TObject);<br> private<br> { Private declarations }<br> Forms: TList;<br> FIndex: Integer;<br> procedure SetIndex(Value: Integer);<br> public<br> { Public declarations }<br> procedure AddForm(AForm: TForm);<br> property FormIndex: Integer read FIndex write SetIndex;<br> end;<br><br>var<br> fmOption: TfmOption;<br><br>function TestOption: Boolean;<br><br>implementation<br><br>{$R *.DFM}<br><br>function TestOption: Boolean;<br>begin<br> fmOption := TfmOption.Create(nil);<br> fmSub1 := TfmSub1.Create(nil); //创建第一个子窗体实例<br> fmSub2 := TfmSub2.Create(nil); //创建第二个子窗体实例<br> ...<br> with fmOption do<br> try<br> AddForm(fmSub1); //加入第一个窗体的实例<br> AddForm(fmSub2); //加入第一个窗体的实例<br> ...<br> Result := ShowModal = mrOK;<br> finally<br> Free;<br> fmSub1.Free; //如果不是本过程创建就不必释放<br> fmSub2.Free; //如果不是本过程创建就不必释放<br> ...<br> end;<br>end;<br><br>// TSubForm 对象<br><br>constructor TSubForm.Create(AForm: TForm; APageControl: TPageControl);<br>var<br> I, W, H: Integer;<br>begin<br> FForm := AForm;<br> FPageControl := APageControl;<br> FBoxes := TList.Create;<br> FButtons := TList.Create;<br> FTabSheet := TTabSheet.Create(FPageControl.Parent);<br> FTabSheet.PageControl := FPageControl;<br> FTabSheet.Caption := AForm.Caption;<br> for I := 0 to AForm.ComponentCount - 1 do begin<br> if AForm.Components is TGroupBox then begin<br> //当然这里暂时只处理TGroupBox<br> TGroupBox(AForm.Components).Parent := FTabSheet;<br> FBoxes.Add(AForm.Components);<br> W := TGroupBox(AForm.Components).Left<br> + TGroupBox(AForm.Components).Width + 14;<br> H := TGroupBox(AForm.Components).Top<br> + TGroupBox(AForm.Components).Height + 36;<br> if FPageControl.Height < H then begin<br> FPageControl.Height := H;<br> FPageControl.Parent.Height := H + 75;<br> end;<br> if FPageControl.Width < W then begin<br> FPageControl.Width := W;<br> FPageControl.Parent.Width := W + 21;<br> end;<br> end else if AForm.Components is TButton then<br> //按钮在这里处理<br> if AForm.Components.Tag > 0 then<br> with TButton(AForm.Components) do begin<br> Parent := FPageControl.Parent;<br> Visible := False;<br> FButtons.Add(AForm.Components);<br> end;<br> end;<br>end;<br><br>destructor TSubForm.Destroy;<br>var<br> I: Integer;<br>begin<br> //啊哈!借东西要还,要不然下次没得借的说!<br> for I := 0 to FButtons.Count - 1 do<br> TWinControl(FButtons).Parent := FForm;<br> for I := 0 to FBoxes.Count - 1 do<br> TGroupBox(FBoxes).Parent := FForm;<br> FBoxes.Free;<br> FButtons.Free;<br> inherited Destroy;<br>end;<br><br>procedure TSubForm.SetActive(Value: Boolean);<br>var<br> I: Integer;<br>begin<br> //根据Tag的指引,在最适当的地方显示按钮<br> if FActive = Value then Exit;<br> FActive := Value;<br> for I := 0 to FButtons.Count - 1 do begin<br> case TButton(FButtons).Tag of<br> 1: TButton(FButtons).Left := FPageControl.Width - 305;<br> 2: TButton(FButtons).Left := FPageControl.Width - 201;<br> 3: TButton(FButtons).Left := FPageControl.Width - 97;<br> 4: TButton(FButtons).Left := FPageControl.Width - 409;<br> end;<br> TButton(FButtons).Top := FPageControl.Height + 14;<br> TButton(FButtons).Visible := FActive;<br> end;<br>end;<br><br>//TfmOption 对象<br><br>procedure TfmOption.SetIndex(Value: Integer);<br>begin<br> //激活相应的"虚窗体"<br> if FIndex = Value then Exit;<br> if FIndex >= 0 then TSubForm(Forms[FIndex]).Active := False;<br> FIndex := Value;<br> TSubForm(Forms[FIndex]).Active := True;<br>end;<br><br>procedure TfmOption.AddForm(AForm: TForm);<br>var<br> ASubForm: TSubForm;<br>begin<br> ASubForm := TSubForm.Create(AForm, pcGlobal);<br> Forms.Add(ASubForm);<br> Position := poScreenCenter;<br>end;<br><br>procedure TfmOption.FormCreate(Sender: TObject);<br>begin<br> Forms := TList.Create;<br> FIndex := - 1;<br>end;<br><br>procedure TfmOption.FormDestroy(Sender: TObject);<br>begin<br> while Forms.Count > 0 do begin<br> TSubForm(Forms[Forms.Count - 1]).Free;<br> Forms.Delete(Forms.Count - 1);<br> end;<br> Forms.Free;<br>end;<br><br>procedure TfmOption.FormActivate(Sender: TObject);<br>begin<br> pcGlobal.ActivePage := pcGlobal.Pages[0];<br> SetIndex(0);<br>end;<br><br>procedure TfmOption.pcGlobalChange(Sender: TObject);<br>begin<br> FormIndex := pcGlobal.ActivePage.TabIndex;<br>end;<br><br>end.