我正在尝试使用 EnumDisplayMonitors 创建每个监视器的动态数组并存储 DISPLAY_DEVICE 结构.为什么下面的代码不正确?
I am trying to use the EnumDisplayMonitors to create a dynamic array of each monitor and store the DISPLAY_DEVICE structure. Why is the below code not correct?
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] 是最大值.
The first in the array (monitorArray[0]) returns correct information for the second monitor but monitorArray[1] is max values.
已解决我使用的方法只是实现我创建的一个函数:
Solved The method I used was just implementing a function I created:
MonitorArray *mA = reinterpret_cast<MonitorArray*>(dwData);
mA->addScreen(&iMonitor);
回调为每个监视器调用一次,但您的回调不会在每次调用时通过数组递增.你需要做更多这样的事情:
The callback is called once for each monitor, but your callback is not incrementing through the array each time it is called. You need to do something more like this instead:
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));
这篇关于EnumDisplayMonitors 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!