11 March 2017

Windows API - Retrieve the size of the screen

Retrieving the size — in pixels that is — of the desktop screen is not that hard. Here's how to do it:
//Retrieve the window size.
IntPtr windowHandle = GetDesktopWindow();
Rect windowSize;
GetWindowRect(windowHandle, out windowSize);

//DLL import methods.
[DllImport("user32", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();

[DllImport("user32", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr windowHandle, out Rect rectangle);

//Rect(angle) struct.
public struct Rect
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

No comments:

Post a Comment