49

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.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
  • 12
    Please, make sure your code can deal with multiple monitors in some sensible manner. – In silico Jan 07 '11 at 23:44
  • @In silico, what is a sensible manner? Start the game (it is a tiny game) on the primary monitor only? – Prof. Falken Jan 08 '11 at 00: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 08 '11 at 00: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 08 '11 at 01:03
  • @In silico. But what do you want exactly? It will be a small 2D arcade style game. If you tell me what you expect from such an app, I can send you the first release when it's done. :) – Prof. Falken Jan 08 '11 at 01:15
  • 1
    @Amigable Clark Kant: What Anders described (see comment above) is pretty much what I expect from your app. :-) – In silico Jan 09 '11 at 01:39

8 Answers8

86
  • 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)

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Anders
  • 97,548
  • 12
  • 110
  • 164
  • 2
    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 08 '11 at 00: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? – Prof. Falken Jan 09 '11 at 09: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 '11 at 07:25
  • @user396483 ...VIRTUALSCREEN – Anders Jan 06 '13 at 15:44
  • GetDeviceCaps seems to have issues with taskbars on the right or left side of the screen in Windows 7. I haven't been able to get around that issue ... may have to use a different approach? – Cory Trese Aug 18 '14 at 16:31
  • 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 '14 at 17:24
  • @Anders: ENUM_CURRENT_SETTINGS is definitely on for later Win versions. Do you think it's worthy of more description in the answer? – Laurie Stearn Sep 04 '16 at 07:12
  • @LaurieStearn: Not for this question IMHO. If you are just placing a window on a specific monitor then you should be using GetMonitorInfo. Only use the low-level stuff if you need to know color depth or refresh rate. – Anders Sep 04 '16 at 20:09
  • How do I enumerate all supported resolutions/settings given a `HMONITOR`? – Shimmy Weitzhandler Jun 18 '20 at 07:40
  • My workstation has a combined size of 5120 px. However, `SM_CXVIRTUALSCREEN` is returning only 4096 px. Any ideas..? – bobobobo May 11 '21 at 19:48
  • 1
    @bobobobo DPI? Win10 has GetSystemMetricsForDpi. Also make sure your app is DPI aware. – Anders May 12 '21 at 11:13
12

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.

fingeros
  • 121
  • 1
  • 4
8

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

Poni
  • 11,061
  • 25
  • 80
  • 121
  • 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 07 '18 at 19:19
4

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

Petr
  • 259
  • 4
  • 18
  • Isn't this exactly the same answer as https://stackoverflow.com/a/4631313/193892 ? – Prof. Falken Dec 13 '15 at 21:19
  • 4
    `GetSystemMetrics` gives only info about the primary (first) monitor, see https://msdn.microsoft.com/en-us/library/windows/desktop/dd162729(v=vs.85).aspx – Sandburg Jun 07 '18 at 09:11
3

I think SystemParametersInfo might be useful.

Edit: Look at GetMonitorInfo too.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 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 '14 at 17:04
3

MFC Example Multiple monitor support with GetSystemMetrics EnumDisplayMonitors and GetMonitorInfo

Follow this link: Monitor enumeration with source code

Ulterior
  • 2,786
  • 3
  • 30
  • 58
2

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
    `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 03 '17 at 19:29
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

gil123
  • 512
  • 6
  • 12