c - 在 Windows 中发出枚举监视器

标签 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。此类系统的示例:我的笔记本电脑,我在其中连接了一个外部显示器并合上了它的盖子。

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

最佳答案

如果您只想知道连接了多少台显示器,则无需调用 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. 

From the Remarks section

GetSystemMetrics(SM_CMONITORS) counts only visible display monitors. This is different from EnumDisplayMonitors, which enumerates both visible display monitors and invisible pseudo-monitors that are associated with mirroring drivers. An invisible pseudo-monitor is associated with a pseudo-device used to mirror application drawing for remoting or other purposes.

The call then simply becomes

nCnt = GetSystemMetrics(SM_CMONITORS);

关于c - 在 Windows 中发出枚举监视器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21974859/

相关文章:

c - 使用按位运算符解密隐藏消息

c++ - 无法链接 Hello World!

windows - Cordova 录制音频不起作用Windows 10移动版

c++ - 如何将窗口置于通过 CreateProcess 创建的进程之上

c# - 如何以编程方式访问 Windows 8 文件历史记录?

c++ - 有没有办法将 winapi 错误标志作为字符串获取?

c++ - CreateProcess 不遵守命令行

c - 为什么我可以访问另一个文件中的静态变量?

C 结构和指针混淆

c - 我如何使用当前系统时间在C中生成随机数