Jump to content
Sign in to follow this  
WideBoyDixon

Window Positioning

Recommended Posts

WideBoyDixon

Hi all,

Other commitments have taken me away from this forum but today I had an itch that I needed to scratch and AutoIt came to my rescue. I wanted a quick script to arrange windows on my screen so I wrote the one here. It's probably flawed in many ways but I'll throw it here anyways ... :D

The idea of the script is that you can use [Win]+[Ctrl]+[Numeric Keypad] to position the active window on your screen. Should be self explanatory as the window is positioned in the same place as the numeric keypad key that you use.

Opt("MustDeclareVars", 1)

Global $hDesktop = WinGetHandle("Program Manager")
Global $hDLL = DllOpen("user32.dll")
Global $gMonitorInfo = DllStructCreate("int;int[4];int[4];int;char[32]")
DllStructSetData($gMonitorInfo, 1, DllStructGetSize($gMonitorInfo))

_SetHotKeys()

While 1
    Sleep(100)
WEnd

Func _SetHotKeys()
    Local $i
    For $i = 1 To 9
        HotKeySet("#^{NUMPAD" & $i & "}", "_MoveWindow")
    Next
    HotKeySet("#^{NUMPAD0}", "_Quit")
EndFunc   ;==>_SetHotKeys

Func _MoveWindow()
    Local $hWnd = WinGetHandle("[ACTIVE]")
    If $hWnd = $hDesktop Then Return
    If WinGetTitle($hWnd) = "" Then Return
    Local $winState = WinGetState($hWnd)
    If BitAND($winState, 7) <> 7 Then Return
    If BitAND($winState, 48) <> 0 Then Return
    Local $rWindow = WinGetPos($hWnd)
    Local $hMonitor = _GetMonitorFromPoint($rWindow[0], $rWindow[1])
    If $hMonitor = 0 Then Return
    Local $rMonitor = _GetMonitorRect($hMonitor)
    Local $sKeyPressed = StringMid(@HotKeyPressed, 10, 1)
    If StringInStr("134679", $sKeyPressed) > 0 Then $rMonitor[2] /= 2
    If StringInStr("123789", $sKeyPressed) > 0 Then $rMonitor[3] /= 2
    If StringInStr("369", $sKeyPressed) > 0 Then $rMonitor[0] += $rMonitor[2]
    If StringInStr("123", $sKeyPressed) > 0 Then $rMonitor[1] += $rMonitor[3]
    WinMove($hWnd, "", $rMonitor[0], $rMonitor[1], $rMonitor[2], $rMonitor[3])
EndFunc   ;==>_MoveWindow

Func _GetMonitorFromPoint($iX, $iY)
    Local $aRet = DllCall($hDLL, "hwnd", "MonitorFromPoint", "int", $iX, "int", $iY, "int", 0)
    Return $aRet[0]
EndFunc   ;==>_GetMonitorFromPoint

Func _GetMonitorRect($hMonitor)
    Local $aRet = DllCall($hDLL, "int", "GetMonitorInfo", "hwnd", $hMonitor, "ptr", DllStructGetPtr($gMonitorInfo))
    Local $aRect[4]
    For $i = 1 To 4
        $aRect[$i - 1] = DllStructGetData($gMonitorInfo, 3, $i)
    Next
    $aRect[2] -= $aRect[0]
    $aRect[3] -= $aRect[1]
    Return $aRect
EndFunc   ;==>_GetMonitorRect

Func _Quit()
    DllClose($hDLL)
    Exit
EndFunc   ;==>_Quit

Have fun.

WBD

Share this post


Link to post
Share on other sites
bluechipps

yes this most likely has been done elsewhere but i would not have stumbled on it as i have on yours :D very useful and i can definitely use this, thanks a bunch

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  

×