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;
source share