I want to get a monitor descriptor ( HMONITOR), which can be used with the API for multiple Windows monitors for a specific monitor connected to the system by index. For example, let's say I have three monitors attached to my system and part of my desktop; I want to get a handle for monitoring 3.
I already know how to get the device name for a specific monitor by index by calling a function EnumDisplayDevices. For example:
HMONITOR MonitorFromIndex(int index )
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
if (EnumDisplayDevices(NULL, index, &dd, 0) != FALSE)
{
if ((dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) == DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
{
std::cout << dd.DeviceName << std::endl;
}
}
return NULL;
}
In the above code, we found the name of the desired device ( dd.DeviceName). I can use this name to create a DC for this monitor by calling CreateDC:
HDC hDC = CreateDC(dd.DeviceName, dd.DeviceName, NULL, NULL);
And I can get information about this monitor by calling 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;
}
FoundMatch fm;
fm.found = FALSE;
fm.hMonitor = NULL;
BOOL result = EnumDisplayMonitors(hDC, NULL, MonitorEnumProc, reinterpret_cast<LPARAM>(&fm));