如何获取Windows中的显示数量? [英] How do I get the number of displays in windows?

查看:129
本文介绍了如何获取Windows中的显示数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算活动显示的数量。对于Mac我可以使用以下:

I want to count the number of active displays. For Mac I can use the following:

CGDisplayCount nDisplays;
CGGetActiveDisplayList(0,0, &nDisplays);
log.printf("Displays connected: %d",(int)nDisplays);

如何在Windows中实现相同的功能?我找到了 EnumDisplayMonitors ,但我找到了

How can I achieve the same in Windows? I've found EnumDisplayMonitors but I can't work out how to use it.

推荐答案

正如您所发现的, EnumDisplayMonitors() 会做这项工作,但它是一个有点棘手的调用。该文档声明:

As you have discovered, EnumDisplayMonitors() will do the job but it is a little tricky to call. The documentation states:


EnumDisplayMonitors函数枚举显示监视器(包括与镜像驱动程序关联的不可见伪监视器)指定剪切矩形与设备上下文的可见区域的交集。 EnumDisplayMonitors为枚举的每个监视器调用一个应用程序定义的MonitorEnumProc回调函数。注意GetSystemMetrics(SM_CMONITORS)只计算显示监视器。

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.

这使我们更容易的解决方案: GetSystemMetrics(SM_CMONITORS) 。事实上,如果你有psuedo监视器,这可能比 EnumDisplayMonitors()更好。

This leads us to an easier solution: GetSystemMetrics(SM_CMONITORS). Indeed this may be even better than EnumDisplayMonitors() if you have psuedo-monitors.

如下调用 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
}

这篇关于如何获取Windows中的显示数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆