将表单移动到右下角

时间:2015-02-27 10:30:11

标签: forms delphi delphi-xe7

我一直在努力将运行时创建的表单移动到主窗体的右下角。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    procedure Button1Click(Sender: TObject);
//    procedure FormClick(Sender: TObject);
  private
    { Private declarations }
//    procedure WindowPosChanging(var Msg : TMessage); message WM_WINDOWPOSCHANGING;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  F1 : TForm;
begin
   F1 := TForm.Create(nil);
   F1.Height := 300;
   F1.Width :=300;
   F1.Position := poDesktopCenter;
   F1.Name := 'asdf';
   F1.Left:=ClientOrigin.X;//+ ActiveControl.Left+ ClientOrigin.X;
   F1.Top:=ClientOrigin.Y;//+ClientOrigin.Y;
   F1.Show;
end;

//procedure TForm1.FormClick(Sender: TObject);
//var
//  pt : TPoint;
//begin
//   pt := mOUse.CursorPos;
//   lABEL3.Caption := IntToStr(pt.X);
//   label4.Caption := IntToStr(pt.Y);
//end;
//
//procedure TForm1.WindowPosChanging(var Msg: TMessage);
//begin
//    Label1.Caption :=  IntToStr(ClientOrigin.X);
//    Label2.Caption :=  IntToStr(ClientOrigin.Y);
//end;

end.

所以我们有这个例子。

F1.Position := poDesktopCenter;

如果您希望将表单置于桌面中心,此命令可以正常工作,但我想要实现的是将F1表单放在主窗体的底角。我无法弄明白该怎么做。

像这样enter image description here

1 个答案:

答案 0 :(得分:7)

在以下所有情况下,请使用

F1.Position := poDesigned;

F1 parent = Form1,在Form1边框内右下角

F1.Parent := self;
F1.Left := self.ClientWidth - F1.Width;
F1.Top  := self.ClientHeight - F1.Height;

Self是可选的,但是明确指出您在执行代码的上下文中引用Form1的属性。

未分配F1父级,右下角重叠边框

F1.Left := Left + Width - F1.Width;
F1.Top  := Top + Height - F1.Height;

未分配F1父级,在Form1的边框内右下角

F1.Left := ClientOrigin.X + ClientWidth - F1.Width;
F1.Top  := ClientOrigin.Y + ClientHeight - F1.Height;

感谢Sertac提醒我有关ClientOrigin的信息