Set wnd = CreateObject("UltraMon.Window")
If wnd.GetForegroundWindow() = True Then
	'get the coordinates of the four corners of the window
	Dim corners(7)
	corners(0) = wnd.Left 'TopLeftX
	corners(1) = wnd.Top 'TopLeftY
	corners(2) = corners(0) + wnd.Width 'TopRightX
	corners(3) = corners(1) 'TopRightY
	corners(4) = corners(0) 'BottomLeftX
	corners(5) = corners(1) + wnd.Height 'BottomLeftY
	corners(6) = corners(2) 'BottomRightX
	corners(7) = corners(5) 'BottomRightY

	'get the coordinates of the monitor each of the four corners of the window is on
	TopLeftX = &H7fffffff
	TopLeftY = &H7fffffff
	BottomRightX = -&H7fffffff
	BottomRightY = -&H7fffffff

	Set sys = CreateObject("UltraMon.System")
	For i = 0 To 3
		x = corners(i * 2)
		y = corners((i * 2) + 1)

		For Each mon In sys.Monitors
			monLeft = mon.Left
			monTop = mon.Top
			monRight = monLeft + mon.Width
			monBottom = monTop + mon.Height

			monWorkLeft = mon.WorkLeft
			monWorkTop = mon.WorkTop
			monWorkRight = monWorkLeft + mon.WorkWidth
			monWorkBottom = monWorkTop + mon.WorkHeight

			If x >= monLeft And x <= monRight And y >= monTop And y <= monBottom Then
				'corner is on this monitor
				If monWorkLeft < TopLeftX Then TopLeftX = monWorkLeft
				If monWorkTop < TopLeftY Then TopLeftY = monWorkTop
				If monWorkRight > BottomRightX Then BottomRightX = monWorkRight
				If monWorkBottom > BottomRightY Then BottomRightY = monWorkBottom
				Exit For
			End If
		Next
	Next

	'resize the window
	wnd.ShowState = 2 'SHOWSTATE_NORMAL
	wnd.Left = TopLeftX
	wnd.Top = TopLeftY
	wnd.Width = BottomRightX - TopLeftX
	wnd.Height = BottomRightY - TopLeftY
	wnd.ApplyChanges 0
End If
