Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I 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 achieve the same in Windows? I've found EnumDisplayMonitors but I can't work out how to use it.

like image 402
fredley Avatar asked Oct 14 '11 11:10

fredley


People also ask

How do I see all of my monitors?

Select Start , then open Settings . Under System , select Display . Your PC should automatically detect your monitors and show your desktop. If you don't see the monitors, select Multiple displays , then Detect.

How do you change which monitor is 1/2 and 3?

To adjust your display settings, right click on any open area of your computer's desktop. Select Display settings. In the Display Settings window, you can identify which monitor is which by clicking on Identify. A number 1 or 2 will display momentarily on each monitor.

How many displays can my computer have?

The number of monitors you can plug into your computer majorly depends on the type of graphics card in your PC. Most graphics cards support two monitors. For desktops, two independent screens can be plugged into the back of the PC; for laptops, the graphics card can drive the integrated screen and one outside monitor.


2 Answers

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

The EnumDisplayMonitors function enumerates display monitors (including invisible pseudo-monitors associated with the mirroring drivers) that intersect a region formed by the intersection of a specified clipping rectangle and the visible region of a device context. EnumDisplayMonitors calls an application-defined MonitorEnumProc callback function once for each monitor that is enumerated. Note that GetSystemMetrics (SM_CMONITORS) counts only the display monitors.

This leads us to an easier solution: GetSystemMetrics(SM_CMONITORS). Indeed this may be even better than EnumDisplayMonitors() if you have psuedo-monitors.


As illustration of calling EnumDisplayMonitors() try this:

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
}
like image 100
David Heffernan Avatar answered Oct 14 '22 00:10

David Heffernan


Not tested, but essentially you only need to provide the callback for the enum 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);
  }
like image 39
Darcara Avatar answered Oct 14 '22 00:10

Darcara