Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 310
benjtupas Avatar asked May 06 '14 10:05

benjtupas


People also ask

How can different screen sizes all be the same resolution?

For the window to be the same size on both screens, either they must be the same size and resolution, or they must have the same pixel density. Neither is true with these two screens. To fix this, one needs to set the resolution of the 21.5in screen to 2646x1488.


2 Answers

#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;
}
like image 164
user3607571 Avatar answered Oct 12 '22 23:10

user3607571


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

like image 35
DNamto Avatar answered Oct 12 '22 22:10

DNamto