MOVE_MINIMIZED_WND = False 'set this to True to move minimized windows as well

'create window object and get all application windows (including dialogs)
Set wnd = CreateObject("UltraMon.Window")
Set wnds = wnd.GetAppWindows(True)

'get the currently active window and store the number of the monitor it is on
monIdActiveApp = 0
Dim wndActive
For Each w In wnds
	If w.Active = True Then
		monIdActiveApp = w.Monitor
		Set wndActive = w
		Exit For
	End If
Next

If monIdActiveApp <> 0 Then
	'there is an active window, move all windows which are on the same monitor
	'as the active window to the next monitor
	For Each w In wnds
		If w.HWnd <> wndActive.HWnd Then
			wndMinimized = False
			If MOVE_MINIMIZED_WND = True And w.ShowState = 1 Then
				'the window is minimized, restore it so that we can get the monitor it is on and possibly move it
				wndMinimized = True
				w.ShowState = 2 'SHOWSTATE_NORMAL
				w.ApplyChanges 0
			End If

			If w.Monitor = monIdActiveApp Then
				w.Monitor = -1 'MOVEMONITOR_NEXT
				w.ApplyChanges 1 + 2 'WNDCHANGE_RESIZE_TO_FIT, WNDCHANGE_CLIP_TO_WORKSPACE
			End If

			If wndMinimized = True Then
				'minimize the window again
				w.ShowState = 1 'SHOWSTATE_MINIMIZED
				w.ApplyChanges 0
			End If
		End If
	Next

	'due to the window moving the active window is no longer active, activate it again
	wndActive.Activate
End If
