Skip to content

Instantly share code, notes, and snippets.

@danielres
Created January 23, 2011 17:31
Show Gist options
  • Select an option

  • Save danielres/792246 to your computer and use it in GitHub Desktop.

Select an option

Save danielres/792246 to your computer and use it in GitHub Desktop.
ultramon script to maximize window on virtual monitor
'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("0,0,1440,900","1440,-180,2400,1080","2400,-180,3360,1080")
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
'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")
'
'optional command-line arguments for the script:
'
'/m: maximize window after moving it
'/n: move window to next monitor (default)
'/p: move window to previous monitor
'/r: only resize window if necessary (resize to fit). default is resize proportionally
Option Explicit
Dim MONITORS
MONITORS = Array("0,0,1440,900","1440,-180,2400,1080","2400,-180,3360,1080")
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.",, "VMonMoveWnd3"
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 moveNext, resizeToFit, args, maximize
moveNext = True
resizeToFit = False
maximize = False
Set args = WScript.Arguments
For i = 0 To args.Count - 1
Select Case args(i)
Case "/m"
maximize = True
Case "/n"
moveNext = True
Case "/p"
moveNext = False
Case "/r"
resizeToFit = True
End Select
Next
Dim wnd
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 MoveWindow wnd, maxAreaMonIndex, moveNext, resizeToFit, maximize
End If
Sub MoveWindow(wnd, curMonIndex, moveNext, resizeToFit, maximize)
Dim curLeft, curTop, curWidth, curHeight, curMonLeft, curMonTop, curMonWidth, curMonHeight
Dim newMonIndex, newMonLeft, newMonTop, newMonWidth, newMonHeight
curLeft = wnd.Left
curTop = wnd.Top
curWidth = wnd.Width
curHeight = wnd.Height
str = Split(MONITORS(curMonIndex), ",")
rect = Array(CLng(str(0)), CLng(str(1)), CLng(str(2)), CLng(str(3)))
curMonLeft = rect(POS_LEFT)
curMonTop = rect(POS_TOP)
curMonWidth = rect(POS_RIGHT) - rect(POS_LEFT)
curMonHeight = rect(POS_BOTTOM) - rect(POS_TOP)
newMonIndex = curMonIndex
newMonLeft = 0
newMonTop = 0
newMonWidth = 0
newMonHeight = 0
If moveNext Then
newMonIndex = newMonIndex + 1
If newMonIndex > UBound(MONITORS) Then newMonIndex = 0
Else
newMonIndex = newMonIndex - 1
If newMonIndex < 0 Then newMonIndex = UBound(MONITORS)
End If
str = Split(MONITORS(newMonIndex), ",")
rect = Array(CLng(str(0)), CLng(str(1)), CLng(str(2)), CLng(str(3)))
newMonLeft = rect(POS_LEFT)
newMonTop = rect(POS_TOP)
newMonWidth = rect(POS_RIGHT) - rect(POS_LEFT)
newMonHeight = rect(POS_BOTTOM) - rect(POS_TOP)
If newMonWidth <> 0 Then
Dim newLeft, newTop, newWidth, newHeight, offsetX, offsetY, offsetXRel, offsetYRel
Dim newOffsetX, newOffsetY, relWidth, relHeight
If maximize = True Then
newLeft = newMonLeft
newTop = newMonTop
newWidth = newMonWidth
newHeight = newMonHeight
Else
offsetX = curLeft - curMonLeft
offsetY = curTop - curMonTop
If newMonWidth = curMonWidth And newMonHeight = curMonHeight Then
newLeft = newMonLeft + offsetX
newTop = newMonTop + offsetY
newWidth = curWidth
newHeight = curHeight
Else
If resizeToFit = True Then
offsetXRel = offsetX / curMonWidth
offsetYRel = offsetY / curMonHeight
newOffsetX = newMonWidth * offsetXRel
newOffsetY = newMonHeight * offsetYRel
newWidth = curWidth
If newWidth > newMonWidth Then newWidth = newMonWidth
newHeight = curHeight
If newHeight > newMonHeight Then newHeight = newMonHeight
If newOffsetX + newWidth > newMonWidth Then newOffsetX = newMonWidth - newWidth
If newOffsetY + newHeight > newMonHeight Then newOffsetY = newMonHeight - newHeight
newLeft = newMonLeft + newOffsetX
newTop = newMonTop + newOffsetY
Else
offsetXRel = offsetX / curMonWidth
offsetYRel = offsetY / curMonHeight
relWidth = curWidth / curMonWidth
relHeight = curHeight / curMonHeight
newLeft = newMonLeft + newMonWidth * offsetXRel
newTop = newMonTop + newMonHeight * offsetYRel
newWidth = newMonWidth * relWidth
newHeight = newMonHeight * relHeight
'MsgBox "newWnd: " & newWidth & "x" & newHeight & " @ " & newLeft & "," & newTop
End If
End If
End If
wnd.ShowState = SHOWSTATE_NORMAL
wnd.Left = newLeft
wnd.Top = newTop
wnd.Width = newWidth
wnd.Height = newHeight
wnd.ApplyChanges 0
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment