问题在Windows中枚举监视器

时间:2014-02-23 21:32:36

标签: c windows winapi

我的任务是计算连接到运行我的用户模式代码的计算机的当前监视器(屏幕)的数量:

int nCnt = 0;
if(!EnumDisplayMonitors(NULL, NULL, countMonitorsProc, (LPARAM)&nCnt))
{
    //Error
}

BOOL countMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
    int* pCnt = (int*)dwData;
    (*pCnt)++;

    return TRUE;
}

它适用于大多数情况,但在某些系统上EnumDisplayMonitors返回FALSE。这种系统的例子:我的笔记本电脑,我连接了一个外部显示器并关上它。

我很好奇,我是否使用正确的方法来计算连接的显示器?

1 个答案:

答案 0 :(得分:0)

如果您只想知道连接了多少台显示器,则无需拨打EnumDisplayMonitors。对物理(非虚拟)监视器执行此操作的正确方法是GetSystemMetrics,索引为SM_CMONITORS

SM_CMONITORS         The number of display monitors on a desktop. For more
80                   information, see the Remarks section in this topic. 

来自备注部分

  

GetSystemMetrics(SM_CMONITORS)仅计算可见的显示监视器。这与EnumDisplayMonitors不同,EnumDisplayMonitors枚举可见显示监视器和与镜像驱动程序关联的不可见伪监视器。不可见的伪监视器与用于镜像应用程序绘制以进行远程处理或其他目的的伪设备相关联。

然后呼叫变成

nCnt = GetSystemMetrics(SM_CMONITORS);