'script configuration:
'the coordinates of each monitor, either of the full screen area or parts of it, need to be specified, ordered from left to right.
'to take taskbars and desktop toolbars into account when maximizing or moving a window, specify the workspace coordinates instead of
'the screen coordinates. both types of coordinates are shown for each monitor under UltraMon menu > About
'
'sample script configuration:
'two monitors at 1024x768, the first monitor is split in a left and right half, the second monitor isn't split:
'MONITORS = Array("0,0,512,768","512,0,1024,768","1024,0,2048,768")

Option Explicit
Dim MONITORS
MONITORS = Array()

If UBound(MONITORS) = -1 Then
	MsgBox "You'll need to configure the script before using it for the first time. To do this, right-click the script and select Edit from the menu, then read the instructions at the top of the script.",, "VMonMaximizeWnd3"
	WScript.Quit
End If

Const SHOWSTATE_NORMAL = 2
Const POS_LEFT = 0
Const POS_TOP = 1
Const POS_RIGHT = 2
Const POS_BOTTOM = 3

Dim wnd, newLeft, newTop, newWidth, newHeight
Set wnd = CreateObject("UltraMon.Window")
If wnd.GetForegroundWindow() = True Then
	Dim wndLeft, wndRight, wndTop, wndBottom, wndWidth, wndHeight
	wndLeft = wnd.Left
	wndTop = wnd.Top
	wndWidth = wnd.Width
	wndHeight = wnd.Height
	wndRight = wnd.Left + wndWidth
	wndBottom = wnd.Top + wndHeight
	
	'msgbox "wnd: " & wndLeft & "," & wndTop & " - " & wndRight & "," & wndBottom
		
	Dim i, str, rect, intLeft, intTop, intRight, intBottom, area, maxArea, maxAreaMonIndex
	maxAreaMonIndex = -1
	For i = 0 To UBound(MONITORS)
		str = Split(MONITORS(i), ",")
		rect = Array(CLng(str(0)), CLng(str(1)), CLng(str(2)), CLng(str(3)))
		If Not (wndRight <= rect(POS_LEFT) Or wndLeft >= rect(POS_RIGHT) Or wndTop >= rect(POS_BOTTOM) Or wndBottom <= rect(POS_TOP)) Then
			intLeft = wndLeft
			If intLeft < rect(POS_LEFT) Then intLeft = rect(POS_LEFT)
			intTop = wndTop
			If intTop < rect(POS_TOP) Then intTop = rect(POS_TOP)
			intRight = wndRight
			If intRight > rect(POS_RIGHT) Then intRight = rect(POS_RIGHT)
			intBottom = wndBottom
			If intBottom > rect(POS_BOTTOM) Then intBottom = rect(POS_BOTTOM)
			
			area = (intRight - intLeft) * (intBottom - intTop)
			If area > maxArea Then
				maxArea = area
				maxAreaMonIndex = i
			End If
			
			'msgbox "mon: " & MONITORS(i) & " int: " & intLeft & "," & intTop & " - " & intRight & "," & intBottom & " area: " & area
		End If
	Next
	
	If maxAreaMonIndex <> -1 Then
		'get the maximized position and size for the window
		Dim maxLeft, maxTop, maxWidth, maxHeight
		str = Split(MONITORS(maxAreaMonIndex), ",")
		rect = Array(CLng(str(0)), CLng(str(1)), CLng(str(2)), CLng(str(3)))
		maxLeft = rect(POS_LEFT)
		maxTop = rect(POS_TOP)
		maxWidth = rect(POS_RIGHT) - rect(POS_LEFT)
		maxHeight = rect(POS_BOTTOM) - rect(POS_TOP)
		
		'msgbox "max: " & maxLeft & "," & maxTop & " - " & maxLeft + maxWidth & "," & maxTop + maxHeight
		
		'check if window is currently maximized
		If wnd.ShowState = SHOWSTATE_NORMAL And wndLeft = maxLeft And wndTop = maxTop And wndWidth = maxWidth And wndHeight = maxHeight Then
			'window is maximized, restore it
			newWidth = wndWidth * 0.9
			newHeight = wndHeight * 0.9
			newLeft = wndLeft + (wndWidth - newWidth) / 2
			newTop = wndTop + (wndHeight - newHeight) / 2
		Else
			'window isn't maximized, maximize it
			newWidth = maxWidth
			newHeight = maxHeight
			newLeft = maxLeft
			newTop = maxTop
		End If
		
		wnd.ShowState = SHOWSTATE_NORMAL
		wnd.Left = newLeft
		wnd.Top = newTop
		wnd.Width = newWidth
		wnd.Height = newHeight
	End If

	wnd.ApplyChanges 0
End If