CiucaS
CiucaS

Reputation: 2128

Move form to bottom right corner

I've been struggling for some time to move a run-time created form to the bottom right corner of the main form.

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.

So we have this example.

F1.Position := poDesktopCenter;

This command works perfectly if you want to center the form onto the desktop, but what i want to achive is position the F1 form at the bottom corner of the main form. I can't figure out how to do it.

Something like this enter image description here

Upvotes: 2

Views: 2912

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21033

In all cases below, use

F1.Position := poDesigned;

F1 parent = Form1, bottom-right inside Form1 borders

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

Self is optional, but makes it clear that you refer to properties of Form1, in which context you are executing the code.

F1 parent not assigned, bottom-right with overlapping borders

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

F1 parent not assigned, bottom-right inside the borders of Form1

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

Credit goes to Sertac for reminding me about ClientOrigin

Upvotes: 8

Related Questions