我的任务是计算连接到运行我的用户模式代码的计算机的当前监视器(屏幕)的数量:
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/