枚举显示器回调函数

5

我正在尝试使用EnumDisplayMonitors来创建每个监视器的动态数组,并存储DISPLAY_DEVICE结构。为什么下面的代码不正确?

BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {

    MONITORINFOEX iMonitor;
    iMonitor.cbSize = sizeof(MONITORINFOEX);
    GetMonitorInfo(hMonitor, &iMonitor);

    if (iMonitor.dwFlags == DISPLAY_DEVICE_MIRRORING_DRIVER)
    {
        return true;
    }
    else
    {
        *reinterpret_cast<ScreenArray*>(dwData) = ScreenArray(&iMonitor);
        return true;
    };

}

调用方式

ScreenArray monitorArray[15];

int i = 0;
EnumDisplayMonitors(NULL, NULL, MyInfoEnumProc, reinterpret_cast<LPARAM>(&monitorArray[i++]));

在数组中,monitorArray[0]返回第二个监视器的正确信息,但monitorArray[1]是最大值。
编辑:已解决 我使用的方法只是实现了我创建的一个函数:
MonitorArray *mA = reinterpret_cast<MonitorArray*>(dwData);
        mA->addScreen(&iMonitor);

1
EnumDisplayMonitors 会多次调用 MyInfoEnumProc,每次传递 &monitorArray[0]dwData。数组的其他元素都不会被使用。 - Igor Tandetnik
2个回答

9

EnumDisplayMonitors 接受您传递的参数,并为每个监视器调用回调函数,每次传递相同的参数。您对 monitorArray 的索引从未增加。相反,您需要在回调函数中管理索引。

这是一个小类,可以自动构建系统中所有监视器的向量。

struct MonitorRects
{
    std::vector<RECT>   rcMonitors;

    static BOOL CALLBACK MonitorEnum(HMONITOR hMon,HDC hdc,LPRECT lprcMonitor,LPARAM pData)
    {
        MonitorRects* pThis = reinterpret_cast<MonitorRects*>(pData);
        pThis->rcMonitors.push_back(*lprcMonitor);
        return TRUE;
    }

    MonitorRects()
    {
        EnumDisplayMonitors(0, 0, MonitorEnum, (LPARAM)this);
    }
};

使用方法如下:

MonitorRects monitors;
cout << "You have " << monitors.rcMonitors.size() << " monitors connected.";

1
这是一个非常简洁的方法,很棒的答案 :) - BullyWiiPlaza

6
回调函数会针对每个监视器调用一次,但您的回调函数并没有在每次调用时递增整个数组。相反,您需要像这样做一些更多的操作:
struct ScreenArrayInfo
{
    ScreenArray *Array;
    int Count;
    int MaxCount;
};

BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
    MONITORINFOEX iMonitor;
    iMonitor.cbSize = sizeof(MONITORINFOEX);
    GetMonitorInfo(hMonitor, &iMonitor);

    if (iMonitor.dwFlags == DISPLAY_DEVICE_MIRRORING_DRIVER)
    {
        return true;
    }
    else
    {
        ScreenArrayInfo *info = reinterpret_cast<ScreenArrayInfo*>(dwData);
        if (info->Count == info->MaxCount) return false;
        info->Array[info->Count] = ScreenArray(&iMonitor);
        Info->Count++;
        return true;
    };
}

ScreenArray monitorArray[15];
ScreenArrayInfo info;
info.Array = monitorArray;
info.Count = 0;
info.MaxCount = 15;

EnumDisplayMonitors(NULL, NULL, &MyInfoEnumProc, reinterpret_cast<LPARAM>(&info));

或者:

#include <vector>

BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
    MONITORINFOEX iMonitor;
    iMonitor.cbSize = sizeof(MONITORINFOEX);
    GetMonitorInfo(hMonitor, &iMonitor);

    if (iMonitor.dwFlags == DISPLAY_DEVICE_MIRRORING_DRIVER)
    {
        return true;
    }
    else
    {
        reinterpret_cast< std::vector<ScreenArray>* >(dwData)->push_back(ScreenArray(&iMonitor));
        return true;
    };
}

std::vector<ScreenArray> monitorArray;
EnumDisplayMonitors(NULL, NULL, &MyInfoEnumProc, reinterpret_cast<LPARAM>(&monitorArray));

查看GetMonitorInfo的文档,它并没有说明dwFlags可以是DISPLAY_DEVICE_MIRRORING_DRIVER,只有MONITORINFOF_PRIMARY作为一个标志。你在哪里看到它可以是DISPLAY_DEVICE_MIRRORING_DRIVER? - bpeikes
@bpeikes 我并没有从任何地方获取它。这是在原始代码中的。我只是维护了原始代码,但添加了适当的数组处理。 - Remy Lebeau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接