'script configuration:
'the monitor splits (horizontal and vertical) need to be specified
'in MON_SPLIT_HORZ and MON_SPLIT_VERT.
'
'sample script configuration:
'your system has 3 monitors. monitor 1 should be split in two vertically, with the left half 75% wide, and the right half 25% wide.
'monitor 3 should be split in four (two horizontally and two vertically), with each quadrant having the same size.
'the MON_SPLIT_HORZ and MON_SPLIT_VERT arrays would look like this:
'
'MON_SPLIT_HORZ = Array("", "", "0.5,0.5")
'MON_SPLIT_VERT = Array("0.75,0.25", "", "0.5,0.5")

Option Explicit
Dim MON_SPLIT_HORZ, MON_SPLIT_VERT
MON_SPLIT_HORZ = Array()
MON_SPLIT_VERT = Array()

If UBound(MON_SPLIT_HORZ) = -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.",, "VMonMaximizeWnd2"
	WScript.Quit
End If

Const SHOWSTATE_NORMAL = 2
Const SHOWSTATE_MAXIMIZED = 3
Dim wnd, sys, mon, newLeft, newTop, newWidth, newHeight
Set wnd = CreateObject("UltraMon.Window")
If wnd.GetForegroundWindow() = True Then
	Dim splitX, splitY
	splitX = MON_SPLIT_VERT(wnd.Monitor - 1)
	splitY = MON_SPLIT_HORZ(wnd.Monitor - 1)
	
	If splitX = "" And splitY = "" Then
		'monitor isn't split, maximize/restore normally
		If wnd.ShowState = SHOWSTATE_MAXIMIZED Then
			'restore window
			wnd.ShowState = SHOWSTATE_NORMAL
		Else
			'maximize window
			wnd.ShowState = SHOWSTATE_MAXIMIZED
		End If
	Else
		'monitor is split
		Set sys = CreateObject("UltraMon.System")
		Set mon = sys.Monitors(wnd.Monitor - 1)
		
		Dim wndLeft, wndTop, wndWidth, wndHeight
		wndLeft = wnd.Left
		wndTop = wnd.Top
		wndWidth = wnd.Width
		wndHeight = wnd.Height
		
		Dim xSplits, xPos, ySplits, yPos, i
		If splitX = "" Then splitX = "1.0"
		xSplits = Split(splitX, ",")
		ReDim xPos(UBound(xSplits) + 1)
		xPos(0) = mon.WorkLeft
		For i = 1 To UBound(xSplits)
			xPos(i) = Int(xPos(i - 1) + (CDbl(xSplits(i - 1)) * mon.WorkWidth))
			'msgbox "xPos(" & i & ") = " & xPos(i)
		Next
		xPos(UBound(xPos)) = mon.WorkLeft + mon.WorkWidth
		
		Dim maxLeft, maxTop, maxWidth, maxHeight
		maxLeft = xPos(0)
		For i = 0 To UBound(xPos) - 1
			If wndLeft >= xPos(i) And wndLeft < xPos(i + 1) Then
				maxLeft = xPos(i)
				maxWidth = xPos(i + 1) - maxLeft
				Exit For
			End If
		Next
		
		If splitY = "" Then splitY = "1.0"
		ySplits = Split(splitY, ",")
		ReDim yPos(UBound(ySplits) + 1)
		yPos(0) = mon.WorkTop
		For i = 1 To UBound(ySplits)
			yPos(i) = Int(yPos(i - 1) + (CDbl(ySplits(i - 1)) * mon.WorkHeight))
			'msgbox "yPos(" & i & ") = " & yPos(i)
		Next
		yPos(UBound(yPos)) = mon.WorkTop + mon.WorkHeight
		'msgbox "yPos(0) = " & yPos(0) & " yPos(1) = " & yPos(1)
		
		maxTop = yPos(0)
		For i = 0 To UBound(yPos) - 1
			If wndTop >= yPos(i) And wndTop < yPos(i + 1) Then
				maxTop = yPos(i)
				maxHeight = yPos(i + 1) - maxTop
				Exit For
			End If
		Next
		
		'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