MultiMonitorEnumerator.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "stdafx.h"
  2. #include "MultiMonitorEnumerator.h"
  3. CMultiMonitorEnumerator::CMultiMonitorEnumerator()
  4. {
  5. m_iMontorNumber = 0;
  6. EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)this);
  7. }
  8. BOOL CALLBACK CMultiMonitorEnumerator::MonitorEnumProc( HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData )
  9. {
  10. CMultiMonitorEnumerator* pThis = (CMultiMonitorEnumerator*)dwData;
  11. //The rectangle coordinates are virtual-screen coordinates
  12. if (lprcMonitor)
  13. {
  14. // TRACE(_T("%p (l=%d t=%d r=%d b=%d)\n"),
  15. // hMonitor,
  16. // lprcMonitor->left, lprcMonitor->top,
  17. // lprcMonitor->right, lprcMonitor->bottom);
  18. }
  19. //GetMonitorInfo 获取显示器信息
  20. MONITORINFOEX infoEx;
  21. memset(&infoEx, 0, sizeof(infoEx));
  22. infoEx.cbSize = sizeof(infoEx);
  23. if (GetMonitorInfo(hMonitor, &infoEx))
  24. {
  25. // TRACE(_T("%p [%s] %s rcMonitor(l=%d t=%d r=%d b=%d) rcWork(l=%d t=%d r=%d b=%d)\n"),
  26. // hMonitor,
  27. // infoEx.szDevice,
  28. // infoEx.dwFlags == MONITORINFOF_PRIMARY ? _T("Primary") : _T("Slave"),
  29. // infoEx.rcMonitor.left, infoEx.rcMonitor.top, infoEx.rcMonitor.right, infoEx.rcMonitor.bottom,
  30. // infoEx.rcWork.left, infoEx.rcWork.top, infoEx.rcWork.right, infoEx.rcWork.bottom);
  31. }
  32. //保存显示器信息
  33. if (pThis->m_iMontorNumber < _countof(pThis->m_MonitorInfoExArray) - 1)
  34. {
  35. VMONITORINFO* pInfo = &pThis->m_MonitorInfoExArray[pThis->m_iMontorNumber];
  36. pInfo->hMonitor = hMonitor;
  37. if (lprcMonitor)
  38. {
  39. pInfo->rcVirtual = *lprcMonitor;
  40. }
  41. pInfo->rcMonitor = infoEx.rcMonitor;
  42. pInfo->rcWork = infoEx.rcWork;
  43. pInfo->bPrimary = infoEx.dwFlags == MONITORINFOF_PRIMARY;
  44. _tcscpy_s(pInfo->szDevice, infoEx.szDevice);
  45. pThis->m_iMontorNumber++;
  46. return TRUE; //continue the enumeration
  47. }
  48. return FALSE; //stop the enumeration
  49. }