Jump to content

_MoveWindow()


Yashied
 Share

Recommended Posts

This is a simple function to align windows from one of my project. _MoveWindow() supports working space and Aero (see screenshots), and does not uses any UDFs. Enjoy!

 

Window1.png

Window2.png

Window3.png

$hParent = GUICreate('Parent', 300, 300)
$hForm = GUICreate('Form', 200, 200)

GUISetState(@SW_SHOW, $hParent)
GUISetState(@SW_SHOW, $hForm)

Sleep(1000)

_MoveWindow($hForm, $hParent, 0, 0)

Do
Until GUIGetMsg() = -3

Func _MoveWindow($hWnd, $hParent = 0, $iX = Default, $iY = Default, $iW = -1, $iH = -1, $fClient = 1, $fResize = 0)

    Local $Area[4], $Wx[2], $Cx[2], $tRect, $pRect, $Pos, $Ret

    $Ret = DllCall('user32.dll', 'long', 'GetWindowLongW', 'hwnd', $hWnd, 'int', -16)
    If (@Error) Or (BitAND($Ret[0], 0x21000000)) Then
        Return 0
    EndIf
    $Pos = WinGetPos($hWnd)
    If @Error Then
        Return 0
    EndIf
    If $iW = -1 Then
        $iW = $Pos[2]
    EndIf
    If $iH = -1 Then
        $iH = $Pos[3]
    EndIf
    $tRect = DllStructCreate('int;int;int;int')
    $pRect = DllStructGetPtr($tRect)
    $Ret = DllCall('dwmapi.dll', 'uint', 'DwmGetWindowAttribute', 'hwnd', $hWnd, 'dword', 9, 'ptr', $pRect, 'dword', 16)
    If (Not @Error) And (Not $Ret[0]) Then
        For $i = 0 To 1
            $Wx[$i] = DllStructGetData($tRect, $i + 3) - DllStructGetData($tRect, $i + 1) - $Pos[$i + 2]
        Next
    Else
        For $i = 0 To 1
            $Wx[$i] = 0
        Next
    EndIf
    $iW += $Wx[0]
    $iH += $Wx[1]
    $Ret = DllCall('user32.dll', 'int', 'SystemParametersInfoW', 'uint', 0x0030, 'uint', 0, 'ptr', $pRect, 'uint', 0)
    If (@Error) Or (Not $Ret[0]) Then
        $Area[0] = 0
        $Area[1] = 0
        $Area[2] = @DesktopWidth
        $Area[3] = @DesktopHeight
    Else
        For $i = 0 To 3
            $Area[$i] = DllStructGetData($tRect, $i + 1)
        Next
    EndIf
    Do
        If $hParent Then
            $Ret = DllCall('user32.dll', 'long', 'GetWindowLongW', 'hwnd', $hParent, 'int', -16)
            If @Error Then
                Return 0
            EndIf
            If Not BitAND($Ret[0], 0x20000000) Then
                If $fClient Then
                    $Ret = DllCall('user32.dll', 'int', 'GetClientRect', 'hwnd', $hParent, 'ptr', $pRect)
                    If (@Error) Or (Not $Ret[0]) Then
                        Return 0
                    EndIf
                    $Ret = DllCall('user32.dll', 'int', 'ClientToScreen', 'hwnd', $hParent, 'ptr', $pRect)
                    If (@Error) Or (Not $Ret[0]) Then
                        Return 0
                    EndIf
                    If IsKeyword($iX) Then
                        $iX = Int((DllStructGetData($tRect, 3) - $iW) / 2)
                    EndIf
                    If IsKeyword($iY) Then
                        $iY = Int((DllStructGetData($tRect, 4) - $iH) / 2)
                    EndIf
                    $iX += DllStructGetData($tRect, 1)
                    $iY += DllStructGetData($tRect, 2)
                Else
                    $Pos = WinGetPos($hParent)
                    If @Error Then
                        Return 0
                    EndIf
                    $Ret = DllCall('dwmapi.dll', 'uint', 'DwmGetWindowAttribute', 'hwnd', $hParent, 'dword', 9, 'ptr', $pRect, 'dword', 16)
                    If (Not @Error) And (Not $Ret[0]) Then
                        For $i = 0 To 1
                            $Cx[$i] = DllStructGetData($tRect, $i + 3) - DllStructGetData($tRect, $i + 1) - $Pos[$i + 2]
                        Next
                    Else
                        For $i = 0 To 1
                            $Cx[$i] = 0
                        Next
                    EndIf
                    If IsKeyword($iX) Then
                        $iX = Int(($Pos[2] + $Cx[0] - $iW) / 2)
                    EndIf
                    If IsKeyword($iY) Then
                        $iY = Int(($Pos[3] + $Cx[1] - $iH) / 2)
                    EndIf
                    $iX += $Pos[0] - $Cx[0] / 2
                    $iY += $Pos[1] - $Cx[1] / 2
                EndIf
                ExitLoop
            EndIf
        EndIf
        If IsKeyword($iX) Then
            $iX = Int(($Area[2] - $iW) / 2)
        EndIf
        If IsKeyword($iY) Then
            $iY = Int(($Area[3] - $iH) / 2)
        EndIf
        $iX += $Area[0]
        $iY += $Area[1]
    Until 1
    $iX += $Wx[0] / 2
    $iY += $Wx[1] / 2
    If ($fResize) And ($iW > $Area[2] - $Area[0]) Then
        $iW = $Area[2] - $Area[0] - $Wx[0]
    EndIf
    If ($fResize) And ($iH > $Area[3] - $Area[1]) Then
        $iH = $Area[3] - $Area[1] - $Wx[1]
    EndIf
    If $iX > $Area[2] - $iW Then
        $iX = $Area[2] - $iW + $Wx[0] / 2
    EndIf
    If $iX < $Area[0] Then
        $iX = $Area[0] + $Wx[0] / 2
    EndIf
    If $iY > $Area[3] - $iH Then
        $iY = $Area[3] - $iH + $Wx[1] / 2
    EndIf
    If $iY < $Area[1] Then
        $iY = $Area[1] + $Wx[1] / 2
    EndIf
    If WinMove($hWnd, '', $iX, $iY, $iW - $Wx[0], $iH - $Wx[1]) Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>_MoveWindow
Edited by Yashied
Link to comment
Share on other sites

Nice Function! I'm sure I will find a use for it :unsure: Voted!

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Nice function again m8 :unsure: ... how about changing the syntax a little to match the syntax of WinMove(), this way it's easy to implement by a simple search & replace run :>.

$hParent = GUICreate('Parent', 300, 300)
$hForm = GUICreate('Form', 200, 200)

GUISetState(@SW_SHOW, $hParent)
GUISetState(@SW_SHOW, $hForm)

Sleep(1000)
_WinMoveEx($hForm, "", 0, 0, -1, -1, 0, $hParent)
Sleep(1000)
_WinMoveEx($hForm, "", 0, 0, -1, -1, 0, $hParent, 0)

Do
Until GUIGetMsg() = -3

Func _WinMoveEx($hWnd, $sWndText = "", $iX = Default, $iY = Default, $iW = -1, $iH = -1, $iSpeed = 0, $hParent = 0, $fClient = 1, $fResize = 0)

    Local $Area[4], $Wx[2], $Cx[2], $tRect, $pRect, $Pos, $Ret

    If $sWndText Then
        $hWnd = WinGetHandle($hWnd, $sWndText)
    ElseIf Not IsHWnd($hWnd) Then
        $hWnd = WinGetHandle($hWnd)
    EndIf
    If Not IsHWnd($hWnd) Then
        Return 0
    EndIf
    If $iSpeed Then
        $iSpeed = Number($iSpeed)
        If $iSpeed < 0 Or $iSpeed > 100 Then
            $iSpeed = 0
        EndIf
    EndIf

    $Ret = DllCall('user32.dll', 'long', 'GetWindowLongW', 'hwnd', $hWnd, 'int', -16)
    If (@error) Or (BitAND($Ret[0], 0x21000000)) Then
        Return 0
    EndIf
    $Pos = WinGetPos($hWnd)
    If @error Then
        Return 0
    EndIf
    If $iW = -1 Then
        $iW = $Pos[2]
    EndIf
    If $iH = -1 Then
        $iH = $Pos[3]
    EndIf
    $tRect = DllStructCreate('int;int;int;int')
    $pRect = DllStructGetPtr($tRect)
    $Ret = DllCall('dwmapi.dll', 'uint', 'DwmGetWindowAttribute', 'hwnd', $hWnd, 'dword', 9, 'ptr', $pRect, 'dword', 16)
    If (Not @error) And (Not $Ret[0]) Then
        For $i = 0 To 1
            $Wx[$i] = DllStructGetData($tRect, $i + 3) - DllStructGetData($tRect, $i + 1) - $Pos[$i + 2]
        Next
    Else
        For $i = 0 To 1
            $Wx[$i] = 0
        Next
    EndIf
    $iW += $Wx[0]
    $iH += $Wx[1]
    $Ret = DllCall('user32.dll', 'int', 'SystemParametersInfoW', 'uint', 0x0030, 'uint', 0, 'ptr', $pRect, 'uint', 0)
    If (@error) Or (Not $Ret[0]) Then
        $Area[0] = 0
        $Area[1] = 0
        $Area[2] = @DesktopWidth
        $Area[3] = @DesktopHeight
    Else
        For $i = 0 To 3
            $Area[$i] = DllStructGetData($tRect, $i + 1)
        Next
    EndIf
    Do
        If $hParent Then
            $Ret = DllCall('user32.dll', 'long', 'GetWindowLongW', 'hwnd', $hParent, 'int', -16)
            If @error Then
                Return 0
            EndIf
            If Not BitAND($Ret[0], 0x20000000) Then
                If $fClient Then
                    $Ret = DllCall('user32.dll', 'int', 'GetClientRect', 'hwnd', $hParent, 'ptr', $pRect)
                    If (@error) Or (Not $Ret[0]) Then
                        Return 0
                    EndIf
                    $Ret = DllCall('user32.dll', 'int', 'ClientToScreen', 'hwnd', $hParent, 'ptr', $pRect)
                    If (@error) Or (Not $Ret[0]) Then
                        Return 0
                    EndIf
                    If $iX = Default Then
                        $iX = Int((DllStructGetData($tRect, 3) - $iW) / 2)
                    EndIf
                    If $iY = Default Then
                        $iY = Int((DllStructGetData($tRect, 4) - $iH) / 2)
                    EndIf
                    $iX += DllStructGetData($tRect, 1)
                    $iY += DllStructGetData($tRect, 2)
                Else
                    $Pos = WinGetPos($hParent)
                    If @error Then
                        Return 0
                    EndIf
                    $Ret = DllCall('dwmapi.dll', 'uint', 'DwmGetWindowAttribute', 'hwnd', $hParent, 'dword', 9, 'ptr', $pRect, 'dword', 16)
                    If (Not @error) And (Not $Ret[0]) Then
                        For $i = 0 To 1
                            $Cx[$i] = DllStructGetData($tRect, $i + 3) - DllStructGetData($tRect, $i + 1) - $Pos[$i + 2]
                        Next
                    Else
                        For $i = 0 To 1
                            $Cx[$i] = 0
                        Next
                    EndIf
                    If $iX = Default Then
                        $iX = Int(($Pos[2] + $Cx[0] - $iW) / 2)
                    EndIf
                    If $iY = Default Then
                        $iY = Int(($Pos[3] + $Cx[1] - $iH) / 2)
                    EndIf
                    $iX += $Pos[0] - $Cx[0] / 2
                    $iY += $Pos[1] - $Cx[1] / 2
                EndIf
                ExitLoop
            EndIf
        EndIf
        If $iX = Default Then
            $iX = Int(($Area[2] - $iW) / 2)
        EndIf
        If $iY = Default Then
            $iY = Int(($Area[3] - $iH) / 2)
        EndIf
        $iX += $Area[0]
        $iY += $Area[1]
    Until 1
    $iX += $Wx[0] / 2
    $iY += $Wx[1] / 2
    If ($fResize) And ($iW > $Area[2] - $Area[0]) Then
        $iW = $Area[2] - $Area[0] - $Wx[0]
    EndIf
    If ($fResize) And ($iH > $Area[3] - $Area[1]) Then
        $iH = $Area[3] - $Area[1] - $Wx[1]
    EndIf
    If $iX > $Area[2] - $iW Then
        $iX = $Area[2] - $iW + $Wx[0] / 2
    EndIf
    If $iX < $Area[0] Then
        $iX = $Area[0] + $Wx[0] / 2
    EndIf
    If $iY > $Area[3] - $iH Then
        $iY = $Area[3] - $iH + $Wx[1] / 2
    EndIf
    If $iY < $Area[1] Then
        $iY = $Area[1] + $Wx[1] / 2
    EndIf
    If $iSpeed Then
        If WinMove($hWnd, '', $iX, $iY, $iW - $Wx[0], $iH - $Wx[1], $iSpeed) Then
            Return 0
        Else
            Return 1
        EndIf
    Else
        If WinMove($hWnd, '', $iX, $iY, $iW - $Wx[0], $iH - $Wx[1]) Then
            Return 0
        Else
            Return 1
        EndIf
    EndIf
EndFunc   ;==>_WinMoveEx
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...