How do I get the dimensions (resolution) of each display?
Asked Answered
M

2

11

I need help on how to retrieve the resolutions of my screens, as shown in the image below.

one 1680x1050, another 1366x768, and a third 1280x800

I found this documentation and it was really helpful. Here's the code that I tried, based on those docs:

int numberOfScreens = GetSystemMetrics(SM_CMONITORS);
int width           = GetSystemMetrics(SM_CXSCREEN);
int height          = GetSystemMetrics(SM_CYSCREEN);

std::cout << "Number of monitors: " << numberOfScreens << "\n";  // returns 3
std::cout << "Width:"               << width           << "\n";
std::cout << "Height:"              << height          << "\n";

However, it only identifies and gives information about the main monitor. How do I get information about the other monitors?

Multiplechoice answered 6/5, 2014 at 10:29 Comment(1)
You might want to read about Enumeration and Display Control.Bibbs
W
11
#include <Windows.h>

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,
                              HDC      hdcMonitor,
                              LPRECT   lprcMonitor,
                              LPARAM   dwData)
{
    MONITORINFO info;
    info.cbSize = sizeof(info);
    if (GetMonitorInfo(hMonitor, &info))
    {
        std::cout << "Monitor x: "<< std::abs(info.rcMonitor.left - info.rcMonitor.right)
                  <<" y: "        << std::abs(info.rcMonitor.top  - info.rcMonitor.bottom)
                  << std::endl;
    }
    return TRUE;  // continue enumerating
}

int main()
{
    EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, 0);

    return 0;
}
Waffle answered 6/5, 2014 at 11:1 Comment(1)
This looks very useful. However, what about getting the physical size of the monitor in mm?Balaton
R
2

To enumerate all the devices attached to the computer, call the EnumDisplayDevices function and enumerate the devices. Then call EnumDisplayMonitors. This returns a handle to each monitor (HMONITOR), which is used with GetMonitorInfo.

You can also use WMI's Win32_DesktopMonitor class, if the OS is Windows XP SP2 or higher (it fails under SP1).

Also you can try to use EDID values from the registry to get the size, but in many cases, the EDID value is not valid.

Registry path

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\DISPLAY

Racemose answered 6/5, 2014 at 11:0 Comment(4)
I like how your answer attempted to explain the solution, rather than just posting a code dump like the other guy. But I have to quibble with your proposed solution. What's the point of calling EnumDisplayDevices first, before calling EnumDisplayMonitors? Just the latter should be sufficient here, right?Eckman
Just navigate to the below registry path HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\DISPLAY, it will show all the display devices which are connected to the system and there will be one entry which is valid for the current session. Similarly WMI also gives more than one result for the desktops, one will be default and one will be the current one which is used. I' not sure why we get multiple monitors most probably it could be tracking the record of the number of monitors connected till date to the CPURacemose
Yes, it tracks all monitors that have been connected. I have 15+ entries under that registry key. None of this answers the question of why it is necessary to call EnumDisplayDevices, as opposed to simply calling EnumDisplayMonitors.Eckman
Even a Projector is display device which can be connected to a PC in many ways ie: HDMI, Component Video,VGA, DVI, Composite Video (RCA), S-Video, RS-232. May be thats why we need EnumDisplayMonitorsRacemose

© 2022 - 2024 — McMap. All rights reserved.