How can I get an HMONITOR handle from a display device name?

◇◆丶佛笑我妖孽 提交于 2019-12-05 12:58:53

Sorry for such a late reply but maybe someone can find this useful.

The multi-monitor API is really minimalist to say the least. Once you've got your dd.DeviceName, it appears you have to go through EnumDisplayMonitors() enumeration until you find a match of dd.DeviceName against MONITORINFOEX.szDevice.

The MONITORINFOEX structure can be obtained by calling GetMonitorInfo().

Here is a non-compilable C++11 pseudo code:

struct DataBag
{
    HMONITOR hmon;
    TCHAR* devname;
} bag;

bag.hmon = NULL;
bag.devname = &dd.DeviceName;

BOOL bRes = EnumDisplayMonitors(
    NULL, NULL,
    [](HMONITOR hMonitor, HDC hDC, LPRECT rc, LPARAM data) -> BOOL {
        auto& bag = *reinterpret_cast<DataBag*>(data);
        MONITORINFOEX mi;
        mi.cbSize = sizeof(mi);
        if (/* match bag.devname against mi.szDevice */ && GetMonitorInfo(hMonitor, &mi))
        {
            bag.hmon = hMonitor;
            return FALSE;
        }
        return TRUE;
    },
    reinterpret_cast<LPARAM>(&bag));
if (bRes && bag.hmon)
{
    // Monitor found!
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!