c++ - 如何获取窗口中的显示数量?

标签 c++ windows winapi

我想计算事件显示器的数量。对于 Mac,我可以使用以下内容:

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

如何在 Windows 中实现相同的目的?我找到了 EnumDisplayMonitors但我不知道如何使用它。

最佳答案

如您所见,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.

这使我们找到了一个更简单的解决方案:GetSystemMetrics(SM_CMONITORS) .事实上,如果您有伪监视器,这可能比 EnumDisplayMonitors() 更好。


作为调用 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
}

关于c++ - 如何获取窗口中的显示数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7767036/

相关文章:

c++ - boost asio 的链接器问题

windows - 为什么我必须使用 "rsp"来调用 C++ 函数?

c++ - FindFirstChangeNotification 两次通知更改

Python 无法从返回的字符串中删除空格

c++ - 即使 CMenu 当前打开,如何强制 LVN_HOTTRACK 始终触发

C++ 如何将我的主类与我的头文件链接起来?

c++ - 使用alignas来对齐结构体

c++ - 显示自定义图像并能够捕获鼠标输入

windows - 在 Windows 上编译 Clang

c++ - 如何在 Media Foundation 中支持多 GPU 系统?