44
votes

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.

7
Please, make sure your code can deal with multiple monitors in some sensible manner.In silico
@In silico, what is a sensible manner? Start the game (it is a tiny game) on the primary monitor only?Prof. Falken
@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
@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
@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

7 Answers

76
votes
  • 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
votes

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.

8
votes

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

3
votes

I think SystemParametersInfo might be useful.

Edit: Look at GetMonitorInfo too.

3
votes

MFC Example Multiple monitor support with GetSystemMetrics EnumDisplayMonitors and GetMonitorInfo

Follow this link: Monitor enumeration with source code

2
votes

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);
2
votes

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