首页  编辑  

窗体嵌入其他窗体

Tags: /超级猛料/VCL/Form,窗体/   Date Created:

窗体嵌入其他窗体是可行的,只要设置Parent即可,但是窗体上的控件会丢失鼠标事件和焦点等,需要对窗体作些处理才可以:

下面的代码使用Tab Control来实现多个窗体的切换:

Q:  How do I fake TTabbedNotebook with multiple forms?

A:  This uses a TabSet to do the "page choosing".  First, you must make an array that will hold the form pointers that looks something like this:

FakePage: array[0..2] of TForm;

Then you need these methods:

procedure TForm1.FormShow(Sender: TObject);

var i: integer;

begin

 FakePage[0] := TForm2.create(application);

 FakePage[1] := TForm3.create(application);

 FakePage[2] := TForm4.create(application);

 for i := 0 to 2 do begin

   FakePage[i].parent := panel1;

   FakePage[i].TabOrder := panel1.TabOrder;

   with panel1 do

     FakePage[i].SetBounds(left + 2, top + 2, width - 4, height - 4);

 end;

 FakePage[TabSet1.TabIndex].show;

end;

procedure TForm1.TabSet1Change(Sender: TObject; NewTab: Integer;

 var AllowChange: Boolean);

begin

 FakePage[TabSet1.TabIndex].hide;

 FakePage[NewTab].show;

end;

This is the code that we place on the forms :

First, the prototype in the form's declaration:

procedure CreateParams(var params: TCreateParams); override;

Then the custom code:

procedure TForm2.CreateParams(var params: TCreateParams);

begin

 inherited CreateParams(params);

 with params do begin

   WndParent := application.MainForm.handle;

   style := ws_child or ws_ClipSiblings;

   x := 0;

   y := 0;

 end;

end;

{This code came from Lloyd's help file!}