答:随便做一个过程用来创建你需要的东西,因为你要在Form1的基础上创建,所以释放是自动的不需要手工释放。
procedure CreateMyform();
var
My_Panel1: TPanel;
My_Label1: TLabel;
begin
My_Panel1:= TPanel.Create(form2); //用Form2为父级创建出来,
//所以当Form2关闭的时候它就会自动被释放。
With My_Panel do begin
Parent:=form2; //使用Form2来控制这个控件。
Top:=0; //下面就可以定义其他的属性了。
Left:=0;
end;
//然后创建Label。
My_Label1:=TLabel.Create(My_Panel1);
With My_Label1 do begin
Parent:=My_Panel1;
//以下写其他属性。
end;
form2.Show;//将form2显示出来。
end;
问题2、如何确定光标在哪个组件上?
答:用ActiveControl属性就可以了,因为动态创建出来的控件没有名字,而且往往会有相同类型的控件,所以你最好添加他们的Tag属性这样你就可以很自如的判断他们到底是谁了。
如:
if TObject(form2.ActiveControl).Tag=1 then begin
TEdit(form2.ActiveControl).Text:='123abc';
end;
q1:
procedure TForm1.Button1Click(Sender: TObject);
var
p1, p2: TPanel;
begin
p1 := TPanel.Create(Form2);
p2 := TPanel.Create(Form2);
p1.Caption := 'panel1';
p2.Caption := 'panel2';
p1.Parent := Form2;
p2.Parent := Form2;
p2.Top := p1.Top + 50;
p2.Left := p1.Left + 50;
Form2.Show;
end; // 释放是自动的。
q2:
function FindVCLWindow(const Pos: TPoint): TWinControl;
var
Handle: HWND;
begin
Handle := WindowFromPoint(Pos);
Result := nil;
while Handle <> 0 do
begin
Result := FindControl(Handle);
if Result <> nil then Exit;
Handle := GetParent(Handle);
end;
end;
// DELPHI自带的函数。返回后直接使用try转换下应该可以的,因为我也是初学的,希望高手指点。