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

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

To get the position of a Windows Form on the screen in C#, you can use the Location property of the form. Here's an example:

int x = this.Location.X; int y = this.Location.Y; 

In this example, this refers to the current form instance. The Location property returns a Point object that represents the form's location on the screen. We can access the X and Y properties of the Point object to get the horizontal and vertical coordinates of the form's top-left corner.

Alternatively, you can use the DesktopLocation property to get the form's location relative to the desktop, rather than relative to the parent control. Here's an example:

int x = this.DesktopLocation.X; int y = this.DesktopLocation.Y; 

In this example, we use the DesktopLocation property instead of the Location property to get the form's position relative to the desktop.

Examples

  1. "C# get Windows Form position on screen"

    Code:

    int formX = this.Location.X; int formY = this.Location.Y; Console.WriteLine($"Form position: X={formX}, Y={formY}"); 

    Description: This C# code retrieves the X and Y coordinates of the current Windows Form's top-left corner.

  2. "WinForms get screen coordinates of form"

    Code:

    Point screenPos = this.PointToScreen(new Point(0, 0)); Console.WriteLine($"Form screen position: X={screenPos.X}, Y={screenPos.Y}"); 

    Description: Converts the form's client coordinates (0,0) to screen coordinates, providing the position on the screen.

  3. "C# determine if form is maximized"

    Code:

    bool isMaximized = this.WindowState == FormWindowState.Maximized; Console.WriteLine($"Is Form maximized: {isMaximized}"); 

    Description: Checks if the current Windows Form is in a maximized state.

  4. "Get form size in C#"

    Code:

    int formWidth = this.Width; int formHeight = this.Height; Console.WriteLine($"Form size: Width={formWidth}, Height={formHeight}"); 

    Description: Retrieves the width and height of the Windows Form.

  5. "C# get primary screen dimensions"

    Code:

    int screenWidth = Screen.PrimaryScreen.Bounds.Width; int screenHeight = Screen.PrimaryScreen.Bounds.Height; Console.WriteLine($"Primary screen dimensions: Width={screenWidth}, Height={screenHeight}"); 

    Description: Obtains the width and height of the primary screen.

  6. "C# get screen DPI"

    Code:

    float dpiX = this.CreateGraphics().DpiX; float dpiY = this.CreateGraphics().DpiY; Console.WriteLine($"Screen DPI: X={dpiX}, Y={dpiY}"); 

    Description: Retrieves the DPI (dots per inch) of the screen.

  7. "C# get working area of screen"

    Code:

    Rectangle workingArea = Screen.PrimaryScreen.WorkingArea; Console.WriteLine($"Primary screen working area: {workingArea}"); 

    Description: Obtains the working area (excluding taskbars, etc.) of the primary screen.

  8. "C# move form to specific coordinates"

    Code:

    this.Location = new Point(desiredX, desiredY); 

    Description: Sets the position of the Windows Form to specific X and Y coordinates on the screen.

  9. "WinForms detect screen resolution change"

    Code:

    SystemEvents.DisplaySettingsChanged += (sender, e) => { Console.WriteLine("Screen resolution changed."); }; 

    Description: Registers an event handler to detect changes in the screen resolution.

  10. "C# get form bounds"

    Code:

    Rectangle formBounds = this.Bounds; Console.WriteLine($"Form bounds: {formBounds}"); 

    Description: Retrieves the bounding rectangle of the Windows Form, including position and size.


More Tags

c#-ziparchive popen python-telegram-bot asp.net-core-webapi kubernetes-health-check strcpy draw dma panel sling

More C# Questions

More Stoichiometry Calculators

More Investment Calculators

More Statistics Calculators

More Cat Calculators