Multi-Monitor Programming

The following samples show how you can get information about the position and resolution of all enabled monitors. You'll need this if you want to position windows/forms on different monitors. All enabled monitors form a single desktop, with the primary monitor at position x = 0, y = 0. Secondary monitors can also be positioned to the left or above the primary monitor, so you'll also need to handle negative x and y coordinates correctly.

C++ with API functions

The following C++ sample uses the EnumDisplayMonitors API function to get information about all enabled monitors:

struct ENUM_DISP_ARG { TCHAR msg[500]; int monId; }; // callback function called by EnumDisplayMonitors for each enabled monitor BOOL CALLBACK EnumDispProc(HMONITOR hMon, HDC dcMon, RECT* pRcMon, LPARAM lParam) { ENUM_DISP_ARG* pArg = reinterpret_cast<ENUM_DISP_ARG*>(lParam); TCHAR str[100] = _T(""); StringCbPrintf(str, sizeof(str), _T("Monitor %d: %d x %d @ %d,%d\n"), pArg->monId, pRcMon->right - pRcMon->left, pRcMon->bottom - pRcMon->top, pRcMon->left, pRcMon->top); StringCbCat(pArg->msg, sizeof(pArg->msg), str); pArg->monId++; return TRUE; } ENUM_DISP_ARG arg = { 0 }; arg.monId = 1; EnumDisplayMonitors(0, 0, EnumDispProc, reinterpret_cast<LPARAM>(&arg)); MessageBox(0, arg.msg, _T("EnumDisp"), MB_ICONINFORMATION | MB_OK);

The C# sample uses the System.Windows.Forms.Screen class to get information about all enabled monitors:

string msg = ""; int monId = 1; foreach (Screen screen in Screen.AllScreens) { string str = String.Format("Monitor {0}: {1} x {2} @ {3},{4}\n", monId, screen.Bounds.Width, screen.Bounds.Height, screen.Bounds.X, screen.Bounds.Y); msg += str; monId++; } MessageBox.Show(msg, "EnumDisp");
C# with UltraMon

This sample uses the UltraMon COM objects instead of the .NET Screen class to get information about installed monitors. Because the UltraMon monitor collection also includes disabled monitors, the sample checks if the monitor is enabled:

string msg = ""; ULTRAMONLib.UltraMonSystemClass sys = new ULTRAMONLib.UltraMonSystemClass(); foreach (ULTRAMONLib.IUltraMonMonitor mon in sys.Monitors) { if (mon.Enabled) { string str = String.Format("Monitor {0}: {1} x {2} @ {3},{4}\n", mon.ID, mon.Width, mon.Height, mon.Left, mon.Top); msg += str; } } MessageBox.Show(msg, "EnumDisp");

The UltraMon COM objects get installed together with UltraMon. See the UltraMon SDK for more information.

Changing display settings

Getting information about enabled monitors is fairly simple, but changing display settings or enabling/disabling monitors is more complicated. .NET has no built-in support for this, you'll need to use the API functions SetDisplayConfig on Windows 7, and ChangeDisplaySettingsEx on earlier systems.

You can do this easily with UltraMon though, the following C# sample changes the resolution of monitor 1 to 1024 x 768 and enables monitor 2:

ULTRAMONLib.UltraMonSystemClass sys = new ULTRAMONLib.UltraMonSystemClass(); ULTRAMONLib.IUltraMonMonitor mon1 = sys.Monitors.Item(0); mon1.Width = 1024; mon1.Height = 768; ULTRAMONLib.IUltraMonMonitor mon2 = sys.Monitors.Item(1); mon2.Enabled = true; sys.ApplyMonitorChanges();
Multi-monitor related API functions

Here's a list of API functions related to multiple monitors:

  • ChangeDisplaySettingsEx: changes display settings
  • EnumDisplayDevices: enumerates all displays, including disabled ones
  • EnumDisplayMonitors: enumerates all enabled displays
  • EnumDisplaySettingsEx: gets display settings
  • GetMonitorInfo: gets information about a monitor such as the working area
  • MonitorFromPoint: returns the monitor containing a given point
  • MonitorFromRect: returns the monitor containing a given rectangle
  • MonitorFromWindow: returns the monitor containing a given window

API functions specific to Windows 7:

  • QueryDisplayConfig: returns current display settings
  • SetDisplayConfig: sets current display settings