Placing a TLabel on the bottom center of the screen.

ST3VO
ST3VO used Ask the Experts™
on
Hi all,

I need to place a label on the bottom center of the screen irrelevent of the screen size and width.

So, even if I changed the screen resolution it would still be on the same possition.

Can anyone help please?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Place your label, or any other component, where you want it in design-time.

Set the following properties:

Anchors
 akLeft : False
 akTop : False
 akRight: False
 akBottom: TRUE

This will keep is centered no matter what your screen or from size does.

JOhn

Commented:
Well... it will keep it in that position, like bottom center or wherever you put it.

John

Commented:

- put the label on a form
- resize the form according to your label size
- set the position of the form >    myForm.Left := Round( (Screen.Width / 2) - (myLabel.Width /2) );
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
I need it on x, y positioning.

For example  Label1 x position and label1 y position.

Sorry, if I didn't explain myself properly.

Also, I don't want to position it at designtime as it must position itself when windows resolutions are changed too.

Thanks

Martin BarredaPropietario asociado

Commented:
If i understand right, then you want something like this.

Code for Form.OnCanResize event
============================
Button.Left := (Form.width - Button.width) div 2;
Button.Top := (Form.Height - Button.Height - WhateverBottomMarginYouWant);

Hope this help!
Martin BarredaPropietario asociado

Commented:
Sorry... the you see Button, it must be Label
And the properties of label might be:
AutoSize -> true
Anchors -> left and top TRUE, right and bottom FALSE

Commented:

what has to do a resolution change with a form resize  ?!?

if i understand the original post right, the author doesn`t mention anything about moving the label inside the form ...
Martin BarredaPropietario asociado

Commented:
A TLabel must be on a form. With the code i post you ensure that the Tlabel will be centered at bottom when you resize the form to maximize it to entire workarea as resolution change.
Martin BarredaPropietario asociado

Commented:
To capture screen resolution change, and then resize your form, you can put some code in Application.OnEvent procudure.
I will try to find something for this.
Martin BarredaPropietario asociado

Commented:
You can found some code at:

http://delphi.about.com/od/graphics/a/change_display.htm

under "Detecting Changes in Display".

For resizing your form to the entirely workarea you should use this function:
You can call it at FormActivate as MaximizeForm(TForm(Self))

procedure MaximizeForm(var F: TForm; MaxHeight: integer = 0; MaxWidth: integer = 0; Center: boolean = TRUE);
var Work_Space      : TRect;
begin
  SystemParametersInfo(SPI_GETWORKAREA, 0, @Work_Space, 0);
  LockWindowUpdate(F.Handle);
  F.Left   := Work_Space.Left;
  if MaxWidth = 0 then
    F.Width  := Work_Space.Right
  else
    begin
      if Work_Space.Right < MaxWidth then
        F.Width := Work_Space.Right
      else
        F.Width := MaxWidth;
    end;
  F.Top    := Work_Space.Top;
  if MaxHeight = 0 then
    F.Height := Work_Space.Bottom
  else
    begin
      if Work_Space.Bottom < MaxHeight then
        F.Height := Work_Space.Bottom
      else
        F.Height := MaxHeight;
    end;
  if ((MaxWidth <> 0) or (MaxHeight <> 0)) and (Center) then
    begin
      F.Left := (Work_Space.Right  - F.Width ) div 2;
      F.Top  := (Work_Space.Bottom - F.Height) div 2;
    end;
  LockWindowUpdate(0);
end;

This code, the above from delphi about and my previously posted comment should do the work.

Author

Commented:
Hmmm...let me explain a bit more please...

I have 2 variables: x and y as integer.

I am creating a label and position it in the x and y co-ordinates.

It's a 3D Engin label that only contains 2 co-ordinates....

so....I would need something like:

x:=(form.width/2)
y:=(form.height..whatever)...

text.draw('some text', x,y...etc)

Is this a bit clearer?  Just trying to help explaining it further.

I am upgrading this question to 350 points.

Thanks
Propietario asociado
Commented:
Just do
x := (Form1.width - text1.width) div 2;
y := (Form1.Height - text1.Height - WhateverBottomMarginYouWant);
x is Left... y is Top. Two coordinates.
Just a translation of prior comment.
Put this code in OnResize event of Form1
Then you can resize your form, with MaximizeForm to make it the size of screen and call this method each time you detect a resolution change.

Author

Commented:
Works great ...Thanks!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial