/ src / common / monitor_utils.h
monitor_utils.h
 1  #pragma once
 2  
 3  #include "gdiplus.h"
 4  
 5  template<RECT MONITORINFO::*member>
 6  std::vector<std::pair<HMONITOR, RECT>> GetAllMonitorRects()
 7  {
 8      using result_t = std::vector<std::pair<HMONITOR, RECT>>;
 9      result_t result;
10  
11      auto enumMonitors = [](HMONITOR monitor, HDC hdc, LPRECT pRect, LPARAM param) -> BOOL {
12          MONITORINFOEX mi;
13          mi.cbSize = sizeof(mi);
14          result_t& result = *reinterpret_cast<result_t*>(param);
15          if (GetMonitorInfo(monitor, &mi))
16          {
17              result.push_back({ monitor, mi.*member });
18          }
19  
20          return TRUE;
21      };
22  
23      EnumDisplayMonitors(NULL, NULL, enumMonitors, reinterpret_cast<LPARAM>(&result));
24      return result;
25  }
26  
27  template<RECT MONITORINFO::*member>
28  std::vector<std::pair<HMONITOR, MONITORINFOEX>> GetAllMonitorInfo()
29  {
30      using result_t = std::vector<std::pair<HMONITOR, MONITORINFOEX>>;
31      result_t result;
32  
33      auto enumMonitors = [](HMONITOR monitor, HDC hdc, LPRECT pRect, LPARAM param) -> BOOL {
34          MONITORINFOEX mi;
35          mi.cbSize = sizeof(mi);
36          result_t& result = *reinterpret_cast<result_t*>(param);
37          if (GetMonitorInfo(monitor, &mi))
38          {
39              result.push_back({ monitor, mi });
40          }
41  
42          return TRUE;
43      };
44  
45      EnumDisplayMonitors(NULL, NULL, enumMonitors, reinterpret_cast<LPARAM>(&result));
46      return result;
47  }