简体   繁体   English

如何在屏幕上获取Windows窗体的位置?

[英]How to get the position of a Windows Form on the screen?

I am coding a WinForms application in Visual Studio C# 2010 and I want to find out the location of the upper left corner of the WinForm window (the starting location of the window). 我在Visual Studio C#2010中编写WinForms应用程序,我想找出WinForm窗口左上角的位置(窗口的起始位置)。

How can I do that? 我怎样才能做到这一点?

If you are accessing it from within the form itself then you can write 如果您从表单本身访问它,那么您可以编写

int windowHeight = this.Height;
int windowWidth = this.Width;

to get the width and height of the window. 获取窗口的宽度和高度。 And

int windowTop = this.Top; 
int windowLeft = this.Left;

To get the screen position. 获取屏幕位置。

Otherwise, if you launch the form and are accessing it from another form 否则,如果您启动表单并从另一个表单访问它

int w, h, t, l;
using (Form form = new Form())
{
    form.Show();
    w = form.Width;
    h = form.Height;
    t = form.Top;
    l = form.Left;
}

I hope this helps. 我希望这有帮助。

Form.Location.XForm.Location.Y将为您提供左上角的X和Y坐标。

使用Form.Bounds.Top获取“Y”坐标,使用Form.Bounds.Left获取“X”坐标

I had a similar case where for my Form2 i needed the screen location of Form1. 我有一个类似的情况,我的Form2我需要Form1的屏幕位置。 i Solved it by passing the screen locationof form1 to form2 through its constructor : 我通过其构造函数将form1的屏幕位置传递给form2来解决它:

//Form1 // Form1中

 Point Form1Location;
        Form1Location = this.Location;
        Form2 myform2 = new Form2(Form1Location);
        myform2.Show();

//Form2 //窗体2

 Point Form1Loc;
    public Form2(Point Form1LocationRef)
    {
        Form1Loc = Form1LocationRef;
        InitializeComponent();

    }

check this: Form.DesktopLocation Property 检查: Form.DesktopLocation属性

        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.

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