Share

I was playing around in AutoIt because I was doing some R&D on a tool that could position and resize a window give the proper disposition values and window title.  WinMove() to the rescue!  I dunno how I missed this lil gem.  I can only imagine that I overlooked it because of the name.  I was looking for something like “WinSize” or “WinResize” or “WinSetSize” or something of that nature.  I thought WinMove simply moved a window, and it can!  But it can also be used to resize and/or position a window using a window handle or title (see the documentation).  The tool I intended to build needed to be able to look for a window who’s title contained a specified string, then position and size that window using values I provide.

Below is a rough example of how to resize a notepad window:

#include<Array.au3>
 
;Window state constants
Const $WINSTATE_WINDOWEXISTS = 1
Const $WINSTATE_VISIBLE = 2
Const $WINSTATE_ENABLED = 4
Const $WINSTATE_ACTIVE = 8
Const $WINSTATE_MINIMIZED = 16
Const $WINSTATE_MAXIMIZED = 32
 
;Position
Dim $nPosX = Default
Dim $nPosY = Default
 
;Size
Dim $nWidth = 300
Dim $nHeight = 200
 
Dim $sWindowTitle = "Notepad"
Dim $i = 0
Dim $varState
Dim $arrWindows = WinList()
_ArrayDisplay($arrWindows)	;Just to show a list of all windows and handle IDs.
 
;Look for any window title with "Notepad" in it, get the window state by HWND and then move/size by HWND.
If IsArray($arrWindows) And $arrWindows[0][0] > 0 Then
	For $i = 1 To $arrWindows[0][0]
		If StringInStr($arrWindows[$i][0], $sWindowTitle) > 0 Then
			$varState = WinGetState($arrWindows[$i][1])
			If BitAND($varState, $WINSTATE_WINDOWEXISTS) And BitAND($varState, $WINSTATE_VISIBLE) Then
				WinMove($arrWindows[$i][1], "", $nPosX, $nPosY, $nWidth, $nHeight)
			EndIf
		EndIf
	Next
EndIf
 
Exit

Ultimately, I wrapped all this up in a UDF that I could use in my project. It is important to know that this won’t work on minimized windows, BUT…. it WILL work on hidden windows.

Note: The “Default” keyword can be used instead of integer values when defining position, which will cause the position to be placed at the Windows default.