【发布时间】:2013-01-09 16:05:23
【问题描述】:
我在 Visual Studio C# 2010 中编写一个 WinForms 应用程序,我想找出 WinForm 窗口左上角的位置(窗口的起始位置)。
我该怎么做?
【问题讨论】:
我在 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.DesktopLocation Property
int left = this.DesktopLocation.X;
int top = this.DesktopLocation.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();
//Form2
Point Form1Loc;
public Form2(Point Form1LocationRef)
{
Form1Loc = Form1LocationRef;
InitializeComponent();
}
【讨论】:
也是 Left 和 Top 属性的组合(例如表单中的 this.Top)
【讨论】: