How to get the number of displays in windows?

I want to count the number of active displays. For Mac, I can use the following:

CGDisplayCount nDisplays; CGGetActiveDisplayList(0,0, &nDisplays); log.printf("Displays connected: %d",(int)nDisplays); 

How can I do the same in windows? I found EnumDisplayMonitors , but I cannot figure out how to use it.

+6
source share
2 answers

As you discovered, EnumDisplayMonitors() will do the job, but it's a little tricky to call. The documentation states:

The EnumDisplayMonitors function lists display monitors (including invisible pseudo-monitors associated with mirroring drivers) that intersect the area formed by the intersection of the specified crop rectangle and the visible area of ​​the device context. EnumDisplayMonitors calls the application-defined MonitorEnumProc callback function once for each monitor listed. Please note that GetSystemMetrics (SM_CMONITORS) only considers display monitors.

This leads us to a simpler solution: GetSystemMetrics(SM_CMONITORS) . Indeed, it can be even better than EnumDisplayMonitors() if you have pseudo-monitors.


To illustrate the call to EnumDisplayMonitors() try the following:

 BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { int *Count = (int*)dwData; (*Count)++; return TRUE; } int MonitorCount() { int Count = 0; if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&Count)) return Count; return -1;//signals an error } 
+19
source

Not tested, but essentially you need to provide a callback for the enumeration function:

 int numMonitors = 0; BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { //lprcMonitor holds the rectangle that describes the monitor position and resolution) numMonitors++; return true; } int main() { EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, NULL); } 
+2
source

Source: https://habr.com/ru/post/899294/


All Articles