【发布时间】:2016-01-25 08:02:50
【问题描述】:
我想获得一个监视器句柄 (HMONITOR),它可以与 Windows 多监视器 API 一起用于按索引连接到系统的特定监视器 .例如,假设我有三台显示器连接到我的系统并构成我的桌面的一部分;我想得到一个句柄来监控 3。
我已经知道如何通过调用EnumDisplayDevices 函数按索引获取特定监视器的设备名称。例如:
HMONITOR MonitorFromIndex(int index /* (zero-indexed) */)
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
if (EnumDisplayDevices(NULL, index, &dd, 0) != FALSE)
{
// We found a match; make sure that it's part of the desktop.
if ((dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) == DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
{
// Yup. Now we've got the name of the device:
std::cout << dd.DeviceName << std::endl;
// But how do I obtain an HMONITOR for this device?
// ...
}
}
return NULL; // indicate failure
}
在上面的代码中,我们找到了所需设备的名称 (dd.DeviceName)。我可以使用此名称通过调用CreateDC 为该监视器创建一个 DC:
HDC hDC = CreateDC(dd.DeviceName, dd.DeviceName, NULL, NULL);
我可以通过调用EnumDisplaySettings获取有关该监视器的信息:
DEVMODE dm;
dm.dmSize = sizeof(dm);
dm.dmDriverExtra = 0;
if (EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm) != FALSE)
{
std::cout << "The monitor supports " << dm.dmBitsPerPel << " bits per pixel." << std::endl;
}
这一切都很好,但我想要那个显示器的句柄。我怎样才能得到它?
我尝试调用EnumDisplayMonitors,将句柄传递给我使用CreateDC 创建的设备上下文,希望获得传递给回调函数的监视器句柄,但没有这样的运气。回调函数从未被调用,EnumDisplayMonitors 返回FALSE(未设置错误代码):
struct FoundMatch
{
BOOL found;
HMONITOR hMonitor;
};
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData)
{
FoundMatch* pfm = reinterpret_cast<FoundMatch*>(dwData);
pfm->found = TRUE;
pfm->hMonitor = hMonitor;
return FALSE; // stop enumerating
}
// elsewhere, after getting the device name and using it to create a DC
FoundMatch fm;
fm.found = FALSE;
fm.hMonitor = NULL;
BOOL result = EnumDisplayMonitors(hDC, NULL, MonitorEnumProc, reinterpret_cast<LPARAM>(&fm));
【问题讨论】:
标签: windows winapi multiple-monitors