How to detect the current screen resolution?

98,592

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

MFC Example Multiple monitor support with GetSystemMetrics EnumDisplayMonitors and GetMonitorInfo

Follow this link: Monitor enumeration with source code

Solution 5

I think SystemParametersInfo might be useful.

Edit: Look at GetMonitorInfo too.

Share:
98,592
Prof. Falken
Author by

Prof. Falken

Updated on July 09, 2022

Comments

  • Prof. Falken
    Prof. Falken almost 2 years

    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.

    • In silico
      In silico over 13 years
      Please, make sure your code can deal with multiple monitors in some sensible manner.
    • Prof. Falken
      Prof. Falken over 13 years
      @In silico, what is a sensible manner? Start the game (it is a tiny game) on the primary monitor only?
    • Anders
      Anders over 13 years
      @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.
    • In silico
      In silico over 13 years
      @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.
    • Prof. Falken
      Prof. Falken over 13 years
      @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. :)
    • In silico
      In silico over 13 years
      @Amigable Clark Kant: What Anders described (see comment above) is pretty much what I expect from your app. :-)
  • Anders
    Anders over 13 years
    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...
  • Prof. Falken
    Prof. Falken over 13 years
    I am not sure how all of this applies when using OpenGL in fullscreen mode? Things like "begin" at 0x0, does this still apply?
  • Anders
    Anders over 13 years
    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
    Anders over 11 years
    @user396483 ...VIRTUALSCREEN
  • Cory Trese
    Cory Trese almost 10 years
    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?
  • Anders
    Anders almost 10 years
    @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...
  • jave.web
    jave.web over 9 years
    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 :)
  • Prof. Falken
    Prof. Falken over 8 years
    Isn't this exactly the same answer as stackoverflow.com/a/4631313/193892 ?
  • Laurie Stearn
    Laurie Stearn almost 8 years
    @Anders: ENUM_CURRENT_SETTINGS is definitely on for later Win versions. Do you think it's worthy of more description in the answer?
  • Anders
    Anders almost 8 years
    @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.
  • mistertodd
    mistertodd about 7 years
    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."
  • mistertodd
    mistertodd over 6 years
    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.
  • Sandburg
    Sandburg about 6 years
    GetSystemMetrics gives only info about the primary (first) monitor, see msdn.microsoft.com/en-us/library/windows/desktop/…
  • Shimmy Weitzhandler
    Shimmy Weitzhandler about 4 years
    How do I enumerate all supported resolutions/settings given a HMONITOR?
  • bobobobo
    bobobobo about 3 years
    My workstation has a combined size of 5120 px. However, SM_CXVIRTUALSCREEN is returning only 4096 px. Any ideas..?
  • Anders
    Anders about 3 years
    @bobobobo DPI? Win10 has GetSystemMetricsForDpi. Also make sure your app is DPI aware.