Multi-Monitor Programming

http://www.realtimesoft.com/multimon/programming/
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);
<br /><span id="_xhe_temp" width="0" height="0"><br /></span>
C#

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");
<br /><span id="_xhe_temp" width="0" height="0"><br /></span>
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");
<br /><span id="_xhe_temp" width="0" height="0"><br /></span>

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值