Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 902
OPMagicPotato Avatar asked Jan 25 '13 16:01

OPMagicPotato


People also ask

What determines the position of form on screen?

The position of the form is determined by the Location property. The form is positioned at the Windows default location and is resized to the default size determined by Windows. The form is positioned at the Windows default location and isn't resized.

How do you center a form on a screen?

To center your form in the application screen, open the form in design view. View the Form Properties. Set the "Auto Center" property to "Yes".

How do I open Windows in center of screen?

Use Form. CenterToScreen() method. On system with two monitors the form will be centered on one that currently have the cursor.

How can I bring my application window to the front?

When the user has the program minimized and presses F3 a global hook keys gets the press and translation window should be made and brought to front.


1 Answers

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.

like image 178
MoonKnight Avatar answered Oct 14 '22 18:10

MoonKnight