EnumDisplayMonitors回调 [英] EnumDisplayMonitors callback
问题描述
我试图使用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回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!