如何获得每个显示器的尺寸(分辨率)?

时间:2014-05-06 10:29:00

标签: c++ windows winapi multiple-monitors

我需要有关如何检索屏幕分辨率的帮助,如下图所示。

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

我找到了this documentation,这真的很有帮助。以下是我尝试过的代码,基于这些文档:

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";

但是,它仅识别并提供有关监视器的信息。我如何获得有关其他监视器的信息?

2 个答案:

答案 0 :(得分:5)

#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;
}

答案 1 :(得分:2)

要枚举连接到计算机的所有设备,请调用EnumDisplayDevices function并枚举设备。然后拨打EnumDisplayMonitors。这将返回每个监视器(HMONITOR)的句柄,该句柄与GetMonitorInfo一起使用。

如果操作系统是Windows XP SP2或更高版本(在SP1下失败),您也可以使用WMI的Win32_DesktopMonitor class

此外,您可以尝试使用注册表中的EDID值来获取大小,但在许多情况下,EDID值无效。

注册表路径

HKEY_LOCAL_MACHINE \ SYSTEM \ CURRENTCONTROLSET \枚举\ DISPLAY

相关问题