![](/img/trans.png)
[英]How to get the correct position of a Windows Form relative to the display?
[英]How to get the position of a Windows Form on the screen?
我在Visual Studio C#2010中编写WinForms应用程序,我想找出WinForm窗口左上角的位置(窗口的起始位置)。
我怎样才能做到这一点?
如果您从表单本身访问它,那么您可以编写
int windowHeight = this.Height;
int windowWidth = this.Width;
获取窗口的宽度和高度。 和
int windowTop = this.Top;
int windowLeft = this.Left;
获取屏幕位置。
否则,如果您启动表单并从另一个表单访问它
int w, h, t, l;
using (Form form = new Form())
{
form.Show();
w = form.Width;
h = form.Height;
t = form.Top;
l = form.Left;
}
我希望这有帮助。
Form.Location.X
和Form.Location.Y
将为您提供左上角的X和Y坐标。
使用Form.Bounds.Top
获取“Y”坐标,使用Form.Bounds.Left
获取“X”坐标
我有一个类似的情况,我的Form2我需要Form1的屏幕位置。 我通过其构造函数将form1的屏幕位置传递给form2来解决它:
// Form1中
Point Form1Location;
Form1Location = this.Location;
Form2 myform2 = new Form2(Form1Location);
myform2.Show();
//窗体2
Point Form1Loc;
public Form2(Point Form1LocationRef)
{
Form1Loc = Form1LocationRef;
InitializeComponent();
}
int left = this.DesktopLocation.X;
int top = this.DesktopLocation.Y;
也是Left和Top属性的组合(例如this.Top from the form in)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.