How can I get the X,Y position of a TWinControl (relative to the screen)
Asked Answered
H

2

15

I'm trying to show a custom hint in a TWinControl but I can't figure out how to get it's position.

Using position 0,0 shows the hint on the top of my screen (outside the window) so I guess it must be the position of the control on the screen.

Edit:

I've found the TControl Property ClientOrigin which returns what I've expected, is it correct to use ClientOrigin.X and ClientOrigin.Y?

Hafnium answered 14/11, 2008 at 12:59 Comment(0)
J
48

TControl.ClientToScreen gives you the screen coordinates for a given point within the control.

lPoint := Panel1.ClientToScreen(Point(0,0));
Label1.Caption := Format('Screen: %d, %d', [lPoint.X, lPoint.Y]);
Juratory answered 14/11, 2008 at 13:9 Comment(1)
Point -- Delphi has a really strange policy for naming global functions ;-)Viscus
U
0

If your TControl has one or more Parents (e.g. TPanel) this gives you the Left Top Point relative to the TForm:

function TControl.GePos: TPoint;
var
  tc: TControl; 
  rp: TPoint;
begin                                 
  tc := self;
  Result.X := tc.Left;
  Result.Y := tc.top;
  while tc <> nil do
  begin
    tc := tc.parent;
    if (tc <> nil) and tc.HasParent then    
    begin
      Inc(Result.X, tc.Left);
      Inc(Result.Y, tc.top);
    end
    else
      break;
  end;
end;
Unspent answered 18/3 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.