下面是个简单的例子,说明动态添加控件到窗体的做法。
Form1上先放个Panel,令其Align属性为alClient,然后:
[函数和变量声明]
TForm1 = class(TForm)
...
private
{ Private declarations }
NewPanel: TPanel
//如果你要加多个分割窗口,可以声明多个Panel和
NewSplitter: TSplitter;//Splitter,再根据需要对程序做些修改就行了
procedure AddSplitWindow(Panel: TPanel
Splitter: TSplitter);
procedure DeleteSplitWindow(Panel: TPanel
Splitter: TSplitter);
public
...
end;
[代码实现]
//按Button1添加分割窗口
procedure TForm1.Button1Click(Sender: TObject);
begin
NewPanel := TPanel.Create(Self);
NewSplitter := TSplitter.Create(Self);
AddSplitWindow(NewPanel, NewSplitter);
end;
//按Button2删除分割窗口
procedure TForm1.Button2Click(Sender: TObject);
begin
DeleteSplitWindow(NewPanel, NewSplitter);
end;
//添加分割窗口的过程
procedure TForm1.AddSplitWindow(Panel: TPanel
Splitter: TSplitter);
begin
Splitter.Parent := Self;
Splitter.Align := alLeft;
Panel.Parent := Self;
Panel.Width := 100;
Panel.Align := alLeft;
end;
//删除分割窗口的过程
procedure TForm1.DeleteSplitWindow(Panel: TPanel
Splitter: TSplitter);
begin
Splitter.Free;
Panel.Free;
end;