gpt4 book ai didi

c++ - EnumDisplayMonitors 回调

转载 作者:可可西里 更新时间:2023-11-01 09:43:11 26 4
gpt4 key购买 nike

我正在尝试使用 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);

最佳答案

EnumDisplayMonitors 获取您传递给它的参数并为每个监视器调用一次回调,每次都传递相同的参数。您在 monitorArray 中的索引永远不会增加。相反,您需要在回调本身内管理索引。

这是一个自动构建系统中所有监视器的 vector 的小类。

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.";

关于c++ - EnumDisplayMonitors 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26541484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com