Yashied Posted May 11, 2011 Share Posted May 11, 2011 (edited) 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! expandcollapse popup$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 December 22, 2013 by Yashied My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
guinness Posted May 11, 2011 Share Posted May 11, 2011 Nice Function! I'm sure I will find a use for it 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
KaFu Posted May 12, 2011 Share Posted May 12, 2011 Nice function again m8 ... 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 . expandcollapse popup$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 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now