C# 如何获取Windows窗体在屏幕上的位置?
我正在Visual Studio C#2010中编写一个WinForms应用程序,我想找出WinForm窗口左上角的位置(窗口的起始位置)C# 如何获取Windows窗体在屏幕上的位置?,c#,winforms,position,C#,Winforms,Position,我正在Visual Studio C#2010中编写一个WinForms应用程序,我想找出WinForm窗口左上角的位置(窗口的起始位置) 我该怎么做呢?Form.Location.X和Form.Location.Y将为您提供左上角的X和Y坐标。还包括左属性和顶属性的组合(例如,表单中的this.top)如果您是从表单本身中访问它,那么您可以编写 int windowHeight = this.Height; int windowWidth = this.Width; 获取窗口的宽度和高度。及
我该怎么做呢?
Form.Location.X
和Form.Location.Y
将为您提供左上角的X和Y坐标。还包括左属性和顶属性的组合(例如,表单中的this.top)如果您是从表单本身中访问它,那么您可以编写
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.Bounds.Top
获取“Y”坐标,使用Form.Bounds.Left
获取“X”坐标我有一个类似的例子,我的Form2需要Form1的屏幕位置。我通过将form1的屏幕位置传递给form2的构造函数解决了这个问题:
//表格1
Point Form1Location;
Form1Location = this.Location;
Form2 myform2 = new Form2(Form1Location);
myform2.Show();
//表格2
Point Form1Loc;
public Form2(Point Form1LocationRef)
{
Form1Loc = Form1LocationRef;
InitializeComponent();
}
选中此项:
有一个问题:当我键入“Control.”时,在您通常希望下拉的下拉菜单中(键入“.”后),没有选择“Location”的选项,我该怎么办?我是否必须在代码的顶部包含一些“使用”的内容?可能还涉及到其他拼写。请尝试form.DesktopBounds、form.DesktopLocation、form.Location、form.RestoreBounds、form.Size,也许其中一个将为您提供所需的信息。同样值得注意的是,WinForms属性列表随着.NET Framework版本的发展而发展--4.5版本的列表比2.0版本更丰富。但是所有关于表单的内容,如果您想要控制,那么选择列表包括:Control.Bounds、Control.clientrangle、Control.ClientSize、Control.Location、Control.Size,或者只是Control.Left、Control.Top、Control.Width,控制高度。由于表单是一个控件,.+1,但int可能应该是double类型。我正在使用WPF,不确定它是否不同。
int left = this.DesktopLocation.X;
int top = this.DesktopLocation.Y;