如何知道子控件在屏幕上的坐标.(24分)

  • 主题发起人 主题发起人 3333W
  • 开始时间 开始时间
3

3333W

Unregistered / Unconfirmed
GUEST, unregistred user!
一个form,然后这个窗口中有一个panel
我想知道如何得到这个panel在屏幕中的位置.

我现在通过程序只能知道panel的大小,但是确定不了它在屏幕中的位置,
是屏幕中的位置,不是在form中的位置.

用ClientToScreen转换的坐标不对劲.
 
form的LEFT+控件的LEFT 和 form的TOP+控件的TOP
 
如果是未知窗口呢.
 
Panel1.Parent.Left + Panel1.Left
Panel1.Parent.Top + Panel1.Top
 
ClientToScreen这函数没什么问题吧,你这样测试一下:
//移动form
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.TopLeft).X);
Edit2.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.TopLeft).Y);
Edit3.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.BottomRight).X);
Edit4.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.BottomRight).Y);
end;
//移动Panel1
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
TControl(Sender).Perform(WM_SYSCOMMAND,$F012,0);
Edit1.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.TopLeft).X);
Edit2.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.TopLeft).Y);
Edit3.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.BottomRight).X);
Edit4.Text:=IntToStr(ClientToScreen(Panel1.BoundsRect.BottomRight).Y);
end;
 
这样转不对吗?
procedure TForm1.Button1Click(Sender: TObject);
var P:TPoint;
begin
P:=Panel1.ClientToScreen(Point(0,0));
ShowMessage(IntToStr(P.X)+' '+IntTostr(P.Y));
end;
 
多人接受答案了。
 
后退
顶部