简体   繁体   中英

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).

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. i Solved it by passing the screen locationof form1 to form2 through its constructor :

//Form1

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

//Form2

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

    }

check this: Form.DesktopLocation Property

        int left = this.DesktopLocation.X;
        int top = this.DesktopLocation.Y;

也是Left和Top属性的组合(例如this.Top from the form in)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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