Jump to content
Sign in to follow this  

Order Windows

Recommended Posts



I'm new to auto it, I'm trying to order 6 windows named the same thing (only handle is different). I've sorted out Resizing and moving the first one, however i'd like the second one to get placed next to the first, and so on. And if I close one down I'd like the next one to take its place. 

Im a bit lost , mostly in the ordering part.


Share this post

Link to post
Share on other sites

For this example of Notepad windows to work for you, your window handles will need to be entered into the array, $aWinHwds.
Press Esc key to exit this example when run.

#include <Array.au3>

HotKeySet("{ESC}", "_Terminate") ; Press Esc key to exit.

Global $aWinHwds[6] ; <<<<< Change for the number of windows required. (eg. $aWinHwds[20] will generate 20 windows)
Local $iNumOfCols = 4 ; <<<<< Change for number of columns of windows per row on desktop.

Local $iWinWidth = Int(@DesktopWidth / $iNumOfCols), $iWinHeight = Int(@DesktopHeight / Ceiling(UBound($aWinHwds) / $iNumOfCols))

For $i = 0 To UBound($aWinHwds) - 1
    $iPid = Run("notepad.exe")
    $aWinHwds[$i] = WinGetHandle("", "") ; Add the active window's handle to $aWinHwds Array
    WinMove($aWinHwds[$i], "", Mod($i, $iNumOfCols) * $iWinWidth, Int($i / $iNumOfCols) * $iWinHeight, $iWinWidth + 13, $iWinHeight + 10)

While 1
    For $i = 0 To UBound($aWinHwds) - 1
        If WinExists($aWinHwds[$i], "") = 0 Then ; Check if any windows have been deleted.
            _ArrayDelete($aWinHwds, $i) ;  This re-dimensions the $aWinHwds array.
            ExitLoop ; Exits For-Next loop. Will re-order windows in next For-Next loop with new UBound($aWinHwds) value.
            WinMove($aWinHwds[$i], "", Mod($i, $iNumOfCols) * $iWinWidth, Int($i / $iNumOfCols) * $iWinHeight, $iWinWidth + 13, $iWinHeight + 10)
    If UBound($aWinHwds) = 0 Then ExitLoop ; Exit script if all generated windows have been deleted.  Exits While-Wend loop.

Func _Terminate()
    ; Close all created windows
    For $i = UBound($aWinHwds) - 1 To 0 Step -1
        WinClose($aWinHwds[$i], "")
EndFunc   ;==>_Terminate

Another method is right click on the bottom task bar near the right hand side of the desktop and a context menu will pop up.  To re-arrange the windows on the desktop select either:-
Cascade windows,
Show windows stacked,
Show windows side by side, or,
Show the desktop,

from the menu.


And another shell object method.

; https://msdn.microsoft.com/en-us/library/windows/desktop/bb774094(v=vs.85).aspx

$objShell = ObjCreate("Shell.Application")

$objShell.ToggleDesktop ; Show desktop
$objShell.ToggleDesktop ; Re-instste previous desktop
MsgBox(0, "ToggleDesktop 2nd time", "", 2)

MsgBox(0, "TileHorizontally", "", 2)

MsgBox(0, "TileVertically", "", 2)
MsgBox(0, "CascadeWindows", "", 2)
MsgBox(0, "The End", "Sorry about messing up all your windows", 2)


Edited by Malkey
Added two more methods.
  • Like 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this