假设我有 3 个显示器。如何仅通过其索引获取第二个句柄? EnumDisplayMonitors() 不会工作,因为它也枚举了伪设备,而 EnumDisplayDevices() 没有给我句柄。
最佳答案
您需要使用 EnumDisplayMonitors()
而不是 EnumDisplayDevices()
来访问每个监视器的 HMONITOR
句柄。
但是,监视器不是由索引标识的。 GetMonitorInfo()
可以告诉您哪个监视器是“主监视器”,但仅此而已。没有办法知道哪个显示器是“第二”、“第三”等等。而且您也不能使用显示器位置来确定这一点,因为“第二”显示器可以放置在与“主要”显示器相关的任何位置显示器,然后“第三”显示器可以放置在与“第一”或“第二”显示器相关的任何位置。
所以你必须希望 EnumDisplayMonitors()
按照安装监视器的顺序进行枚举,那么你可以这样做:
struct sEnumInfo
{
int iIndex;
HMONITOR hMonitor;
};
BOOL CALLBACK GetMonitorByIndex(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
sEnumInfo *info = (sEnumInfo*) dwData;
if (--info->iIndex < 0)
{
info->hMonitor = hMonitor;
return FALSE;
}
return TRUE;
}
sEnumInfo info;
info.iIndex = 1;
info.hMonitor = NULL;
EnumDisplayMonitors(NULL, NULL, GetMonitorByIndex, (LPARAM)&info);
if (info.hMonitor != NULL)
{
//...
}
关于c++ - 通过其索引获取(真实)监视器的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29412926/