51

How do I from Winapi (in C or C++) detect the current screen resolution?

Some background:

I want to start a new OpenGL fullscreen window, but want it open with the same horizontal and vertical size which the desktop already is set to. (Now when everyone uses LCD screens, I figured this is the best way to get the native resolution of the screen.)

I don't desperately need to also know the desktop color depth, although that would be a nice bonus.

6
  • 13
    Please, make sure your code can deal with multiple monitors in some sensible manner.
    – In silico
    Jan 7, 2011 at 23:44
  • @In silico, what is a sensible manner? Start the game (it is a tiny game) on the primary monitor only? Jan 8, 2011 at 0:40
  • 3
    @Amigable Clark Kant: Default to primary monitor but allow the user to set a specific monitor in your in-game setup screen. Maybe also support mygame.exe /Monitor1 etc And if you also support windowed mode, use MonitorFromWindow to know where to restore to if they switch back to fullscreen.
    – Anders
    Jan 8, 2011 at 0:50
  • 1
    @Amigable Clark Kant: Anders pretty much hit it on the head on what is "sensible" for your tiny game. When I said "sensible," what I really meant was "behavior appropriate to your application." In the case of computer games, I should be able to configure the monitor settings properly even in multi-monitor environments.
    – In silico
    Jan 8, 2011 at 1:03
  • 1
    @Amigable Clark Kant: What Anders described (see comment above) is pretty much what I expect from your app. :-)
    – In silico
    Jan 9, 2011 at 1:39

8 Answers 8

88
  • Size of the primary monitor: GetSystemMetrics SM_CXSCREEN / SM_CYSCREEN (GetDeviceCaps can also be used)
  • Size of all monitors (combined): GetSystemMetrics SM_CX/YVIRTUALSCREEN
  • Size of work area (screen excluding taskbar and other docked bars) on primary monitor: SystemParametersInfo SPI_GETWORKAREA
  • Size of a specific monitor (work area and "screen"): GetMonitorInfo

Edit: It is important to remember that a monitor does not always "begin" at 0x0 so just knowing the size is not enough to position your window. You can use MonitorFromWindow to find the monitor your window is on and then call GetMonitorInfo

If you want to go the low-level route or change the resolution you need to use EnumDisplayDevices, EnumDisplaySettings and ChangeDisplaySettings (This is the only way to get the refresh rate AFAIK, but GetDeviceCaps will tell you the color depth)

11
  • 3
    I just tested and GetDeviceCaps does work in a multimon setup and both VERT/HORZRES and VREFRESH do seem to work, but I could only get CreateDC to accept a "\\.\DISPLAY2" style string and not "\\.\DISPLAY2\Monitor0" so I'm not sure what one would do if one adapter has more than one monitor, so stick with EnumDisplaySettings if you got the monitor list from EnumDisplayDevices...
    – Anders
    Jan 8, 2011 at 0:47
  • I am not sure how all of this applies when using OpenGL in fullscreen mode? Things like "begin" at 0x0, does this still apply? Jan 9, 2011 at 9:30
  • 1
    Probably not, I was talking about windows, one would hope that OpenGL abstracts that away and you are able to just tell it which monitor to use and it will take care of the rest
    – Anders
    Jan 10, 2011 at 7:25
  • 1
    @CoryTrese: To position a window you should be using GetMonitorInfo. You can also try EnumDisplaySettings with ENUM_CURRENT_SETTINGS if you are looking for low-level information...
    – Anders
    Aug 18, 2014 at 17:24
  • 1
    @bobobobo DPI? Win10 has GetSystemMetricsForDpi. Also make sure your app is DPI aware.
    – Anders
    May 12, 2021 at 11:13
13

When system use DPI virtualization (Vista and above) using GetSystemMetrics or GetWindowRect will fail to get the real screen resolution (you will get the virtual resolution) unless you created DPI Aware Application.

So the best option here (simple and backward compatible) is to use EnumDisplaySettings with ENUM_CURRENT_SETTINGS.

7

It's GetSystemMetrics with these parameters:
SM_CXSCREEN < width
SM_CYSCREEN < height

As it says (SM_CXSCREEN):

The width of the screen of the primary display monitor, in pixels. This is the same value obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor, HORZRES).

1
  • 1
    Note that GetSystemMetrics only exists for backwards compatibility with pre-Windows 98 (and multi-monitor) support. You should be using SM_CXVIRTUALSCREEN/SM_CYVIRTUALSCREEN to get users entire screen.
    – Ian Boyd
    Mar 7, 2018 at 19:19
3

I think SystemParametersInfo might be useful.

Edit: Look at GetMonitorInfo too.

1
  • SystemParametersInfo is useful when you only want the "work area" = portion of the screen not obscured by the system taskbar or by application desktop toolbars :)
    – jave.web
    Nov 21, 2014 at 17:04
3

MFC Example Multiple monitor support with GetSystemMetrics EnumDisplayMonitors and GetMonitorInfo

Follow this link: Monitor enumeration with source code

0
3

I use the GetSystemMetrics function

GetSystemMetrics(SM_CXSCREEN) returns screen width(in pixels)

GetSystemMetrics(SM_CYSCREEN) - height in pixels

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724385%28v=vs.85%29.aspx

2
2

To get real monitor resolution

void GetMonitorRealResolution(HMONITOR monitor, int* pixelsWidth, int* pixelsHeight)
{
    MONITORINFOEX info = { sizeof(MONITORINFOEX) };
    winrt::check_bool(GetMonitorInfo(monitor, &info));
    DEVMODE devmode = {};
    devmode.dmSize = sizeof(DEVMODE);
    winrt::check_bool(EnumDisplaySettings(info.szDevice, ENUM_CURRENT_SETTINGS, &devmode));
    *pixelsWidth = devmode.dmPelsWidth;
    *pixelsHeight = devmode.dmPelsHeight;
}

It will return that native resolution in any case, even if the OS tries to lie to you due to the DPI awareness of the process.

To get the scaling ratio between the virtual resolution and real resolution

float GetMonitorScalingRatio(HMONITOR monitor)
{
    MONITORINFOEX info = { sizeof(MONITORINFOEX) };
    winrt::check_bool(GetMonitorInfo(monitor, &info));
    DEVMODE devmode = {};
    devmode.dmSize = sizeof(DEVMODE);
    winrt::check_bool(EnumDisplaySettings(info.szDevice, ENUM_CURRENT_SETTINGS, &devmode));
    return (info.rcMonitor.right - info.rcMonitor.left) / static_cast<float>(devmode.dmPelsWidth);
}

This will give you a ratio of the real resolution relative to the virtual resolution of the given monitor.

If the main DPI of the main monitor is 225% and on the second monitor it is 100%, and you run this function for the second monitor, you will get 2.25. because 2.25 * real resolution = the virtual resolution of the monitor.

If the second monitor has 125% scaling (while the main monitor is still 225% scaling), then this function will return you 1.79999995 because 125% relative to 225% is this value (225/125 = 1.8), and again - 1.8 * real resolution=the virtual resolution of 125%`

To get the real DPI value (not relative to anything)

Given that monitor, A has 225% DPI, and monitor B has 125% DPI, as I said above, you will not get 1.25 for the second monitor (if you run the function on the second monitor. You will get 1.8 as I said).

To overcome this, use this function:

float GetRealDpiForMonitor(HMONITOR monitor)
{
    return GetDpiForSystem() / 96.0 / GetMonitorScalingRatio(monitor);
}

This function depends on the previous function that I wrote above (the function GetMonitorScalingRatio that you need to copy)

This will give you the correct value

1

Deleted about a week ago, then edited 3-4-13.

Here's a good one for situations where the user has decided to run their desktop in a lower resolution (bad idea) or corner cases where a person decided to get a monitor that their graphics controller couldn't take full advantage of:

// get actual size of desktop
RECT actualDesktop;
GetWindowRect(GetDesktopWindow(), &actualDesktop);
1
  • 1
    GetWindowRect(GetDesktopWindow) is completely wrong. It will intentionally return only the rectangle of the primary monitor. "The rectangle of the desktop window returned by GetWindowRect or GetClientRect is always equal to the rectangle of the primary monitor, for compatibility with existing applications."
    – Ian Boyd
    Apr 3, 2017 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.