windows 如何获取窗口中的显示数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7767036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the number of displays in windows?
提问by fredley
I want to count the number of active displays. For Mac I can use the following:
我想计算活动显示器的数量。对于 Mac,我可以使用以下内容:
CGDisplayCount nDisplays;
CGGetActiveDisplayList(0,0, &nDisplays);
log.printf("Displays connected: %d",(int)nDisplays);
How can I achieve the same in Windows? I've found EnumDisplayMonitorsbut I can't work out how to use it.
如何在 Windows 中实现相同的目标?我找到了EnumDisplayMonitors,但我不知道如何使用它。
回答by David Heffernan
As you have discovered, EnumDisplayMonitors()
will do the job but it is a little tricky to call. The documentation states:
正如您所发现的那样,EnumDisplayMonitors()
可以完成这项工作,但调用起来有点棘手。文档指出:
The EnumDisplayMonitors function enumerates display monitors (including invisible pseudo-monitors associated with the mirroring drivers) that intersect a region formed by the intersection of a specified clipping rectangle and the visible region of a device context. EnumDisplayMonitors calls an application-defined MonitorEnumProc callback function once for each monitor that is enumerated. Note that GetSystemMetrics (SM_CMONITORS) counts only the display monitors.
EnumDisplayMonitors 函数枚举显示监视器(包括与镜像驱动程序关联的不可见伪监视器),这些监视器与由指定剪切矩形和设备上下文的可见区域相交形成的区域相交。EnumDisplayMonitors 为每个枚举的监视器调用应用程序定义的 MonitorEnumProc 回调函数一次。请注意,GetSystemMetrics (SM_CMONITORS) 仅计算显示监视器。
This leads us to an easier solution: GetSystemMetrics(SM_CMONITORS)
. Indeed this may be even better than EnumDisplayMonitors()
if you have psuedo-monitors.
这使我们找到了一个更简单的解决方案:GetSystemMetrics(SM_CMONITORS)
. 事实上,这可能比EnumDisplayMonitors()
你有伪监视器更好。
As illustration of calling EnumDisplayMonitors()
try this:
作为调用的说明EnumDisplayMonitors()
试试这个:
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
int *Count = (int*)dwData;
(*Count)++;
return TRUE;
}
int MonitorCount()
{
int Count = 0;
if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&Count))
return Count;
return -1;//signals an error
}
回答by Darcara
Not tested, but essentially you only need to provide the callback for the enum function:
未经测试,但基本上您只需要为 enum 函数提供回调:
int numMonitors = 0;
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
//lprcMonitor holds the rectangle that describes the monitor position and resolution)
numMonitors++;
return true;
}
int main()
{
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, NULL);
}