#ifndef CMULTIMONITORENUMERATOR_H
#define CMULTIMONITORENUMERATOR_H

#include <windows.h>
#include <vector>

class CMultiMonitorEnumerator
{
public:
    CMultiMonitorEnumerator()
    {
        m_iMontorNumber = 0;
        EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)this);
    }

    int getMontorNumber()
    {
        return m_iMontorNumber;
    }
protected:
  //枚举显示器回调函数
  static BOOL CALLBACK MonitorEnumProc(
    HMONITOR hMonitor,  // handle to display monitor
    HDC hdcMonitor,     // handle to monitor-appropriate device context
    LPRECT lprcMonitor, // pointer to monitor intersection rectangle
    LPARAM dwData       // data passed from EnumDisplayMonitors
    )
    {
        CMultiMonitorEnumerator *pThis = (CMultiMonitorEnumerator *)dwData;

        //GetMonitorInfo 获取显示器信息
        MONITORINFOEX infoEx;
        memset(&infoEx, 0,  sizeof(infoEx));
        infoEx.cbSize = sizeof(infoEx);
        if(GetMonitorInfo(hMonitor, &infoEx))
        {
            DISPLAYCONFIG_TOPOLOGY_ID currentTopologyId;
            UINT32 requiredPaths, requiredModes;
            GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &requiredPaths, &requiredModes);
            std::vector<DISPLAYCONFIG_PATH_INFO> paths(requiredPaths);
            std::vector<DISPLAYCONFIG_MODE_INFO> modes(requiredModes);
            QueryDisplayConfig(QDC_DATABASE_CURRENT, &requiredPaths, paths.data(), &requiredModes, modes.data(), &currentTopologyId);

            if(currentTopologyId == DISPLAYCONFIG_TOPOLOGY_CLONE)
            {//复制屏
                pThis->m_iMontorNumber += paths.size();
            }
            else
            {
                pThis->m_iMontorNumber++;
            }
            return true; //continue the enumeration
        }

        return false; //stop the enumeration
  }
private:
    int m_iMontorNumber;
};

#endif // CMULTIMONITORENUMERATOR_H
