![](/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.