简体   繁体   English

如何获得相对于主屏幕的对象位置

[英]how to get an object position relative to primary screen

In C#, how can I get an object's position in form relative to primary screen? 在C#中,如何获取对象相对于主屏幕的位置?

I searched a little but I got really confused. 我搜索了一下,但感到非常困惑。

I get the form location and object location and summed but it wasn't correct: 我得到了表单位置和对象位置的总和,但这是不正确的:

X = this.Location.X + pictureBox1.Location.X;
Y = this.Location.Y + pictureBox1.Location.Y;

I also tried this: 我也试过这个:

mypoint = pictureBox1.PointToScreen(Point.Empty);

and this: 和这个:

mypoint =new Point(this.Location.X + pictureBox1.Location.X - Screen.PrimaryScreen.Bounds.Left,
          this.Location.Y + pictureBox1.Location.Y - Screen.PrimaryScreen.Bounds.Top);

I want the pictruebox1 location related to display point(0,0). 我想要与显示点(0,0)相关的pictruebox1位置。 在此处输入图片说明

pictureBox1.PointToScreen(Point.Empty); is the correct solution. 是正确的解决方案。 You get it wrong from the location changed event handler maybe it is raised before the form is completely loaded. 您从位置更改的事件处理程序中弄错了它,它可能是在表单完全加载之前引发的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM