Beege

WinSnap - Automatic Window Alignment UDF

20 posts in this topic

#1 ·  Posted (edited)

Here is a UDF to easily apply that "snappy edge" effect to your GUI that you have probably seen on many programs. "Snappy" meaning anytime the boarder of your gui gets close to another windows boarder, or the desktop edges, your gui will automatically snap and align itself to that boarder. It is a very nice feature when it comes to aligning and resizeing your GUIs side by side.

One problem that I still have to hammer out is what Style you use for your GUI. If you choose to make the GUI non sizeable, it will still work but you'll see the edges will overlap a little bit. There is some invisible boarder there that isnt acounted for when I get the gui width and height. I need to look at Getsystemmetrics. Im pretty sure the answer is there.

Anyway plz take a look and tell me what you think. I love feed back or any new ideas. Thanks ;)

Updated 9/17/11: Automatic alignment to ALL visible windows now. Not just other Autoit windows. :huh2: Also removed _WinSnap_RegisterMsgs() as it is not nessasary. Windows Msgs are automatically registered on startup. See UDF header for more details.

Updated 5/31/11: Added new function _WinSnap_SetStyle() to configure what the window snaps to and to be able to enable/disable then snaps. Needed if you want to lock a window to another.

WinSnap.html

Previous Download:122

Note: The .html link is just to help me keep track of downloads. Open the .html file and a download dialog will display.

Examples:

Example 1:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "WinSnap.au3"
 
Global $style = BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_SIZEBOX)
 
Global $GUI1 = GUICreate("GUI1", 200, 150, 100, 200, $style)
GUISetState(@SW_SHOW)
Global $GUI2 = GUICreate("GUI2", 200, 150, 370, 200, $style)
GUISetState(@SW_SHOW)
Global $GUI3 = GUICreate("GUI3", 200, 150, 100, 420, $style)
GUISetState(@SW_SHOW)
Global $GUI4 = GUICreate("GUI4", 200, 150, 370, 420, $style)
GUISetState(@SW_SHOW)
 
_WinSnap_Set($GUI1)
_WinSnap_Set($GUI2)
_WinSnap_Set($GUI3)
_WinSnap_Set($GUI4)
 
Do
Until GUIGetMsg() = -3

Example 2:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "WinSnap.au3"
Global $style = BitOR($WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_SIZEBOX)
Global $GUI1 = GUICreate("GUI1", 200, 150, 100, 200, $style)
Global $button1 = GUICtrlCreateButton('DisableAll', 60, 50, 80, 30)
GUISetState(@SW_SHOW)
Global $GUI2 = GUICreate("GUI2", 200, 150, 350, 200, $style)
Global $button2 = GUICtrlCreateButton('DisableAll', 60, 50, 80, 30)
GUISetState(@SW_SHOW)
_WinSnap_Set($GUI1)
_WinSnap_Set($GUI2)
Global $Msg
While 1
$Msg = GUIGetMsg()
Switch $Msg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $button1
   If GUICtrlRead($button1) = 'EnableAll' Then
    _WinSnap_SetStyle($GUI1, 3)
    GUICtrlSetData($button1, 'DisableAll')
   ElseIf GUICtrlRead($button1) = 'DisableAll' Then
    _WinSnap_SetStyle($GUI1, 0)
    GUICtrlSetData($button1, 'DeskOnly')
   ElseIf GUICtrlRead($button1) = 'DeskOnly' Then
    _WinSnap_SetStyle($GUI1, 1)
    GUICtrlSetData($button1, 'Moveonly')
   ElseIf GUICtrlRead($button1) = 'Moveonly' Then
    _WinSnap_SetStyle($GUI1, 2)
    GUICtrlSetData($button1, 'EnableAll')
   EndIf
  Case $button2
   If GUICtrlRead($button2) = 'EnableAll' Then
    _WinSnap_SetStyle($GUI2, 3)
    GUICtrlSetData($button2, 'DisableAll')
   ElseIf GUICtrlRead($button2) = 'DisableAll' Then
    _WinSnap_SetStyle($GUI2, 0)
    GUICtrlSetData($button2, 'DeskOnly')
   ElseIf GUICtrlRead($button2) = 'DeskOnly' Then
    _WinSnap_SetStyle($GUI2, 1)
    GUICtrlSetData($button2, 'Moveonly')
   ElseIf GUICtrlRead($button2) = 'Moveonly' Then
    _WinSnap_SetStyle($GUI2, 2)
    GUICtrlSetData($button2, 'EnableAll')
   EndIf
EndSwitch
WEnd

UDF:

#region Header
; #INDEX# =======================================================================================================================
; Title .........: WinSnap
; AutoIt Version : 3.3.6.1
; Language ......: English
; Description ...: Functions for making windows have "snap to" edges
; Author(s) .....: Beege
; Remarks........:  This UDF registers windows msgs WM_MOVING, WM_ENTERSIZEMOVE, WS_WM_SIZING. If any of these msgs
;     are registered in your script, your function must pass the msgs on to this UDF. Below are examples of the calls
; you will need to add if this is the case:
;
; Func WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
; WS_WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
; ;USER CODE;
; EndFunc   ;==>WM_SIZING;
;
; Func WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
; WS_WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
; ;USER CODE;
; EndFunc   ;==>WM_SIZING;
;
; Func WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
; WS_WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
; ;USER CODE;
; EndFunc   ;==>WM_SIZING;
;
; ===============================================================================================================================
 
 
; #CURRENT# =====================================================================================================================
; _WinSnap_Set
; _WinSnap_SetStyle
; ===============================================================================================================================
 
#include-once
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>
#endregion Header
#region Global Varialbes
;~ Opt('MustDeclareVars', 1)
 
Global Enum $g_iHwnd, $g_iStyle
Global $g_aSnapGUIs[1][2] = [[0, 0]]
#cs
$g_aSnapGUIs[$i][$g_iHwnd]  = Handle to window
$g_aSnapGUIs[$i][$g_iStyle] = Style Flag
#ce
 
Global $g_iMouseDistancetoX, $g_iMouseDistancetoY
Global $g_iDesktopHeight
Global $g_bMovingStarted = False
Global $g_iMonitorTotal
 
Global Const $WS_WM_MOVING = 0x0216
Global Const $WS_WM_ENTERSIZEMOVE = 0x0231
 
GUIRegisterMsg($WS_WM_MOVING, "WS_WM_MOVING")
GUIRegisterMsg($WS_WM_ENTERSIZEMOVE, 'WS_WM_ENTERSIZEMOVE')
GUIRegisterMsg($WM_SIZING, 'WS_WM_SIZING')
 
#endregion Global Varialbes
 
#region Public functions
; #FUNCTION# ====================================================================================================
; Name...........: _WinSnap_Set
; Description....: Sets window to have "snap to" edges
; Syntax.........: _WinSnap_Set($hGUI)
; Parameters.....: $hGUI - handle to window
; Return values..: Success -  1
; Failure -  0 and sets @error
; Author.........: Beege
; Remarks........: None
; ===============================================================================================================
Func _WinSnap_Set($hGUI)
 
$g_aSnapGUIs[0][0] += 1
ReDim $g_aSnapGUIs[$g_aSnapGUIs[0][0] + 1][2]
$g_aSnapGUIs[$g_aSnapGUIs[0][0]][$g_iHwnd] = $hGUI
$g_aSnapGUIs[$g_aSnapGUIs[0][0]][$g_iStyle] = 3
 
Local $tRectMoving = DllStructCreate($tagRECT)
_WinAPI_SystemParametersInfo(0x0030, 0, DllStructGetPtr($tRectMoving), 0);SPI_GETWORKAREA
If @error Then Return SetError(1, 0, 0)
$g_iDesktopHeight = DllStructGetData($tRectMoving, 'Bottom')
 
$g_iMonitorTotal = _WinAPI_GetSystemMetrics(80);SM_CMONITORS
If @error Then Return SetError(1, 0, 0)
 
Return 1
 
EndFunc   ;==>_WinSnap_Set
 
; #FUNCTION# ====================================================================================================
; Name...........: _WinSnap_SetStyle
; Description....: Sets what the window will snap to. Desktop edges or Window edges
; Syntax.........: _WinSnap_SetStyle($hGUI, $iFlag)
; Parameters.....: $hGUI - Handle to window to set style
; $iFlag - Style Value:
; | 0 = No Snap at all
; | 1 = Snap to Desktop Edges
; | 2 = Snap to Other Autoit Windows
; | 3 = Snap to all (Defualt)
; Return values..: Success - Returns the value of the previous setting
; Failure - 0 and set @error to 1
; Author.........: Beege
; ===============================================================================================================
Func _WinSnap_SetStyle($hGUI, $iFlag)
 
Local $iIndex = _GetIndex($hGUI)
If Not $iIndex Then Return SetError(1, 0, 0)
 
Local $iLast = $g_aSnapGUIs[$iIndex][$g_iStyle]
$g_aSnapGUIs[$iIndex][$g_iStyle] = $iFlag
 
Return $iLast
 
EndFunc   ;==>_WinSnap_SetStyle
 
#endregion Public functions
 
#region internel functions
; #FUNCTION# ====================================================================================================
; Name...........: _GetIndex
; Description....: returns array index for handle
; Syntax.........: _GetIndex($hGUI)
; Parameters.....: $hGUI - handle to window
; Return values..: Success - array index for gui handle
; Failure - 0
; Author.........: Beege
; ===============================================================================================================
Func _GetIndex($hGUI)
For $i = 1 To $g_aSnapGUIs[0][0]
If $hGUI = $g_aSnapGUIs[$i][$g_iHwnd] Then Return $i
Next
Return 0
EndFunc   ;==>_GetIndex
 
; #FUNCTION# ====================================================================================================
; Name...........: WS_WM_ENTERSIZEMOVE
; Description....:  Called when window begins to move.
; Syntax.........: WS_WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
; Return values..: None
; Author.........: Beege
; ===============================================================================================================
Func WS_WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam, $lParam
 
Local $pGUI1 = WinGetPos($hWndGUI)
Local $aMousePos = MouseGetPos()
 
$g_bMovingStarted = False
$g_iMouseDistancetoX = $aMousePos[0] - $pGUI1[0]
$g_iMouseDistancetoY = $pGUI1[1] - $aMousePos[1]
 
EndFunc   ;==>WS_WM_ENTERSIZEMOVE
 
; #FUNCTION# ====================================================================================================
; Name...........: WS_WM_MOVING
; Description....: Called when a window is being moved
; Syntax.........: WS_WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
; Return values..: None
; Author.........: Beege
; ===============================================================================================================
Func WS_WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam, $lParam
 
Static $iStartW, $iStartH
 
Local $iIndex = _GetIndex($hWndGUI)
If Not $iIndex Then Return $GUI_RUNDEFMSG
 
Local $tRectMoving = DllStructCreate($tagRECT, $lParam)
 
If Not $g_bMovingStarted Then
$g_bMovingStarted = True
$iStartW = DllStructGetData($tRectMoving, 'Right') - DllStructGetData($tRectMoving, 'Left')
$iStartH = DllStructGetData($tRectMoving, 'Bottom') - DllStructGetData($tRectMoving, 'Top')
EndIf
 
;If window is snaped we need to monitor how much the mouse has moved
;from is original drag point position on the title bar.
Local $aMousePos = MouseGetPos()
Local $iChangeinX = Abs($aMousePos[0] - (DllStructGetData($tRectMoving, 'Left') + $g_iMouseDistancetoX))
Local $iChangeinY = Abs((DllStructGetData($tRectMoving, 'Top') - $aMousePos[1]) - $g_iMouseDistancetoY)
 
Local $iTopMoving = DllStructGetData($tRectMoving, 'Top'), $iBottomMoving = DllStructGetData($tRectMoving, 'Bottom')
Local $iLeftMoving = DllStructGetData($tRectMoving, 'Left'), $iRightMoving = DllStructGetData($tRectMoving, 'Right')
 
#region Check if near desktop edges
;Check if style includes snaping to desktop edges
If BitAND($g_aSnapGUIs[$iIndex][$g_iStyle], 1) Then
For $i = 1 To $g_iMonitorTotal
If _IsClose($iRightMoving, (@DesktopWidth * $i)) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * $i))
DllStructSetData($tRectMoving, 'Left', DllStructGetData($tRectMoving, 'Right') - $iStartW)
EndIf
Next
 
For $i = 0 To $g_iMonitorTotal - 1
If _IsClose($iLeftMoving, (@DesktopWidth * $i)) Then
DllStructSetData($tRectMoving, 'Left', (@DesktopWidth * $i))
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf
Next
 
If _IsClose($iTopMoving, 0) Then
DllStructSetData($tRectMoving, 'Top', 0)
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf
 
If _IsClose($iBottomMoving, $g_iDesktopHeight) Then
DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight)
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf
 
If _IsClose($iBottomMoving, @DesktopHeight) Then
DllStructSetData($tRectMoving, 'Bottom', @DesktopHeight)
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf
 
;Here we check if the mouse has moved from original drag point. if it has we unsnap the window
If $iChangeinX > 20 Then
DllStructSetData($tRectMoving, 'Left', $aMousePos[0] - $g_iMouseDistancetoX)
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf
If $iChangeinY > 15 Then
DllStructSetData($tRectMoving, 'Top', $aMousePos[1] + $g_iMouseDistancetoY)
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf
 
EndIf
#endregion Check if near desktop edges
 
#region Check if window in near other Autoit Windows
Local $aWinlist = WinList()
;Check if style of window includes other Autoit Windows
If BitAND($g_aSnapGUIs[$iIndex][$g_iStyle], 2) Then
 
For $i = 1 To $aWinlist[0][0]
 
If $aWinlist[$i][0] = '' Or $aWinlist[$i][1] = $hWndGUI Or Not _IsVisible($aWinlist[$i][1]) Then ContinueLoop
 
Local $tRectNonMoving = _WinAPI_GetWindowRect($aWinlist[$i][1])
Local $iTopNonMoving = DllStructGetData($tRectNonMoving, 'Top'), $iBottomNonMoving = DllStructGetData($tRectNonMoving, 'Bottom')
Local $iLeftNonMoving = DllStructGetData($tRectNonMoving, 'Left'), $iRightNonMoving = DllStructGetData($tRectNonMoving, 'Right')
 
If $iTopMoving <= ($iBottomNonMoving + 5) And ($iBottomMoving + 5) >= $iTopNonMoving Then
;Gui1Right to Gui2Right
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then
DllStructSetData($tRectMoving, 'Right', $iRightNonMoving)
DllStructSetData($tRectMoving, 'Left', DllStructGetData($tRectMoving, 'Right') - $iStartW)
EndIf
 
;Gui1Left to Gui2left
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then
DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving)
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf
 
;Gui1Left to Gui2Right
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then
DllStructSetData($tRectMoving, 'Left', $iRightNonMoving)
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf
 
;Gui1Right to Gui2Left
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then
DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving)
DllStructSetData($tRectMoving, 'Left', DllStructGetData($tRectMoving, 'Right') - $iStartW)
EndIf
EndIf
 
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
;gui1top to gui2top
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then
DllStructSetData($tRectMoving, 'Top', $iTopNonMoving)
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf
 
;~  gui1top to gui2bottom
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then
DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving)
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf
 
;gui1bottom to gui2bottom
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then
DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving)
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf
 
;gui1bottom to gui2top
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then
DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving)
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf
EndIf
 
;Here we check if the mouse has moved from original drag point. if it has we unsnap the window
If $iChangeinX > 20 Then
DllStructSetData($tRectMoving, 'Left', $aMousePos[0] - $g_iMouseDistancetoX)
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf
 
If $iChangeinY > 15 Then
DllStructSetData($tRectMoving, 'Top', $aMousePos[1] + $g_iMouseDistancetoY)
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf
Next
EndIf
#endregion Check if window in near other Autoit Windows
 
Return $GUI_RUNDEFMSG
 
EndFunc   ;==>WS_WM_MOVING
 
; #FUNCTION# ====================================================================================================
; Name...........: WS_WM_SIZING
; Description....: Called when a window is being sized
; Syntax.........: WS_WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
; Return values..: None
; Author.........: Beege
; ===============================================================================================================
Func WS_WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam, $lParam
 
If Not _GetIndex($hWndGUI) Then Return $GUI_RUNDEFMSG
 
Local $tRectMoving = DllStructCreate($tagRECT, $lParam)
Local $iLeftMoving = DllStructGetData($tRectMoving, 'Left'), $iRightMoving = DllStructGetData($tRectMoving, 'Right')
Local $iTopMoving = DllStructGetData($tRectMoving, 'Top'), $iBottomMoving = DllStructGetData($tRectMoving, 'Bottom')
 
#region check if resizing near desktop edge
Switch $wParam
Case 6; bottom edge
If _IsClose($iBottomMoving, $g_iDesktopHeight) Then DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight)
Case 7; bottom Left corner
If _IsClose($iBottomMoving, $g_iDesktopHeight) Then DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight)
If _IsClose($iLeftMoving, @DesktopWidth) Then DllStructSetData($tRectMoving, 'Left', @DesktopWidth)
If _IsClose($iLeftMoving, 0) Then DllStructSetData($tRectMoving, 'Left', 0)
Case 1; left
If _IsClose($iLeftMoving, @DesktopWidth) Then DllStructSetData($tRectMoving, 'Left', @DesktopWidth)
If _IsClose($iLeftMoving, 0) Then DllStructSetData($tRectMoving, 'Left', 0)
Case 2; right
If _IsClose($iRightMoving, @DesktopWidth) Then
DllStructSetData($tRectMoving, 'Right', @DesktopWidth)
ElseIf _IsClose($iRightMoving, @DesktopWidth * 2) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * 2))
EndIf
Case 8; bottom right
If _IsClose($iBottomMoving, $g_iDesktopHeight) Then DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight)
If _IsClose($iRightMoving, @DesktopWidth) Then
DllStructSetData($tRectMoving, 'Right', @DesktopWidth)
ElseIf _IsClose($iRightMoving, @DesktopWidth * 2) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * 2))
EndIf
Case 3; top
If _IsClose($iTopMoving, 0) Then DllStructSetData($tRectMoving, 'Top', 0)
Case 4; top left
If _IsClose($iTopMoving, 0) Then DllStructSetData($tRectMoving, 'Top', 0)
If _IsClose($iLeftMoving, @DesktopWidth) Then DllStructSetData($tRectMoving, 'Left', @DesktopWidth)
Case 5; top right
If _IsClose($iRightMoving, @DesktopWidth) Then
DllStructSetData($tRectMoving, 'Right', @DesktopWidth)
ElseIf _IsClose($iRightMoving, @DesktopWidth * 2) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * 2))
EndIf
If _IsClose($iTopMoving, 0) Then DllStructSetData($tRectMoving, 'Top', 0)
EndSwitch
#endregion check if resizing near desktop edge
 
#region Check if resizing near other windows
Local $aWinlist = WinList()
 
For $i = 1 To $aWinlist[0][0]
 
If $aWinlist[$i][0] = '' Or $aWinlist[$i][1] = $hWndGUI Or Not _IsVisible($aWinlist[$i][1]) Then ContinueLoop
 
Local $tRectNonMoving = _WinAPI_GetWindowRect($aWinlist[$i][1])
Local $iLeftNonMoving = DllStructGetData($tRectNonMoving, 'Left'), $iRightNonMoving = DllStructGetData($tRectNonMoving, 'Right')
Local $iTopNonMoving = DllStructGetData($tRectNonMoving, 'Top'), $iBottomNonMoving = DllStructGetData($tRectNonMoving, 'Bottom')
 
Switch $wParam
Case 6; bottom edge
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving)
EndIf
Case 7; bottom Left corner
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving)
EndIf
Case 1; left
If $iTopMoving <= ($iBottomNonMoving + 5) And ($iBottomMoving + 5) >= $iTopNonMoving Then
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iRightNonMoving)
EndIf
Case 2; right
If $iTopMoving <= ($iBottomNonMoving + 5) And ($iBottomMoving + 5) >= $iTopNonMoving Then
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving)
EndIf
Case 8; bottom right
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving)
EndIf
Case 3; top
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iTopNonMoving)
EndIf
Case 4; top left
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iTopNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iRightNonMoving)
If _IsClose($iLeftMoving, 0) Then DllStructSetData($tRectMoving, 'Left', 0)
EndIf
Case 5; top right
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iTopNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving)
EndIf
EndSwitch
Next
#endregion Check if resizing near other windows
 
Return $GUI_RUNDEFMSG
 
EndFunc   ;==>WS_WM_SIZING
 
; #FUNCTION# ====================================================================================================
; Name...........: _IsClose
; Description....: Tests if Position a is near Postion b
; Syntax.........: _IsClose($a, $b)
; Parameters.....: $a - Position 1
; $b - Position 2
; Return values..: True/False
; Author.........: Beege
; ===============================================================================================================
Func _IsClose($a, $b)
Return (Abs($a - $b) < 15)
EndFunc   ;==>_IsClose
#endregion internel functions
 
Func _IsVisible($handle)
Return (BitAND(WinGetState($handle), 2) = 2)
EndFunc   ;==>_IsVisible
Edited by Beege
1 person likes this

Share this post


Link to post
Share on other sites



Updated Posted Image

Share this post


Link to post
Share on other sites

Nice UDF :huh2:


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

Cool work!

When snapping corners the distance is greater than when snapping sides. Is that intentional?

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Nice UDF :huh2:

Thankyou!!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Cool work!

When snapping corners the distance is greater than when snapping sides. Is that intentional?

Br,

UEZ

Thankyou very much for the feedback and No it defiantly shouldn't. When you say corner and sides do you mean the desktop corners and sides or when your resizing a window using one of its corners or sides?

Edited by Beege

Share this post


Link to post
Share on other sites

Nice work

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Thankyou very much for the feedback and No it defiantly shouldn't. When you say corner and sides do you mean the desktop corners and sides or when your resizing a window using one of its corners or sides?

If I start the Example.au3 and position the left upper corner of the 2nd window to the right bottom corner of the 1st window the distance of the edges between the two windows are too big regarding to the other snap behavior.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

If I start the Example.au3 and position the left upper corner of the 2nd window to the right bottom corner of the 1st window the distance of the edges between the two windows are too big regarding to the other snap behavior.

Br,

UEZ

Ok I see what you mean. I'm pretty sure the windows are aligned with the correct distance, it just looks further because the windows show they have rounded corners, when in reality they have square corners that we cant see. I took a small snap shot to better explain.

Posted Image

If you look close at the green lines I drew you should see the horizontal borders line up perfectly, as well as the vertical ones do. If we could actually see the square corners they would be touching just right but we cant.. Hopefully were talking about the same thing. If not let me know. Thanks again for the feed back again. :huh2: Edited by Beege

Share this post


Link to post
Share on other sites

Yes, you are right, I didn't think about rounded corners of the windows. :huh2:

It would look nicer if the corner distance would be smaller...

Btw, happy birthday!

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Yes, you are right, I didn't think about rounded corners of the windows. :huh2:

It would look nicer if the corner distance would be smaller...

Btw, happy birthday!

Br,

UEZ

Ha thanks UEZ! First one this year. Your Awesome! Posted Image

Share this post


Link to post
Share on other sites

Happy birthday, Beege!! :huh2:


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

Happy birthday, Beege!! :huh2:

Thanks Maffe811!!! Posted Image

Share this post


Link to post
Share on other sites

Updated. Automatic alignment to ALL visible windows now. Not just other Autoit windows :graduated:

Share this post


Link to post
Share on other sites

Absolutly exelent! Gonna excessively use it!

Share this post


Link to post
Share on other sites

Nice! Thankyou :graduated:

Share this post


Link to post
Share on other sites

I have Aero Style with WindowBlinds and it give this : :graduated:

Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I have Aero Style with WindowBlinds and it give this : :graduated:

This is something to do with that "one problem" I mentioned in post 1. The same thing happens when I change the style of the gui to be non resizable. Im guessing WindowsBlinds is changing the style of the window. Its snapping the window to the edge of the inner window and not the outer edge. Ill take another look at it. Maybe I can figure it out this time. Edited by Beege

Share this post


Link to post
Share on other sites

#19 ·  Posted


>>>> Directory Enquiries Challenge <<<<
NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-08-11
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Hi,

I had the same problems with some guis that don't fit. So I modded the UDF with an offset.

#region Header
; #INDEX# =======================================================================================================================
; Title .........: WinSnap
; AutoIt Version : 3.3.6.1
; Language ......: English
; Description ...: Functions for making windows have "snap to" edges
; Author(s) .....: Beege
; Remarks........:  This UDF registers windows msgs WM_MOVING, WM_ENTERSIZEMOVE, WS_WM_SIZING. If any of these msgs
;     are registered in your script, your function must pass the msgs on to this UDF. Below are examples of the calls
; you will need to add if this is the case:
;
; Func WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
; WS_WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
; ;USER CODE;
; EndFunc   ;==>WM_SIZING;
;
; Func WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
; WS_WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
; ;USER CODE;
; EndFunc   ;==>WM_SIZING;
;
; Func WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
; WS_WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
; ;USER CODE;
; EndFunc   ;==>WM_SIZING;
;
; ===============================================================================================================================


; #CURRENT# =====================================================================================================================
; _WinSnap_Set
; _WinSnap_SetStyle
; ===============================================================================================================================

#include-once
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>
#endregion Header
#region Global Varialbes
;~ Opt('MustDeclareVars', 1)

Global Enum $g_iHwnd, $g_iStyle
Global $g_aSnapGUIs[1][2] = [[0, 0]]
#cs
$g_aSnapGUIs[$i][$g_iHwnd]  = Handle to window
$g_aSnapGUIs[$i][$g_iStyle] = Style Flag
#ce

Global $g_iMouseDistancetoX, $g_iMouseDistancetoY
Global $g_iDesktopHeight
Global $g_bMovingStarted = False
Global $g_iMonitorTotal
Global $g_iGuiOffset ; mod by cox because of some GUIs that don't fit

Global Const $WS_WM_MOVING = 0x0216
Global Const $WS_WM_ENTERSIZEMOVE = 0x0231

GUIRegisterMsg($WS_WM_MOVING, "WS_WM_MOVING")
GUIRegisterMsg($WS_WM_ENTERSIZEMOVE, 'WS_WM_ENTERSIZEMOVE')
GUIRegisterMsg($WM_SIZING, 'WS_WM_SIZING')

#endregion Global Varialbes

#region Public functions
; #FUNCTION# ====================================================================================================
; Name...........: _WinSnap_Set
; Description....: Sets window to have "snap to" edges
; Syntax.........: _WinSnap_Set($hGUI)
; Parameters.....: $hGUI - handle to window
; Return values..: Success -  1
; Failure -  0 and sets @error
; Author.........: Beege
; Remarks........: None
; ===============================================================================================================
Func _WinSnap_Set($hGUI, $iGuiOffset = 0) ; mod by cox
$g_iGuiOffset = $iGuiOffset ; mod by cox
$g_aSnapGUIs[0][0] += 1
ReDim $g_aSnapGUIs[$g_aSnapGUIs[0][0] + 1][2]
$g_aSnapGUIs[$g_aSnapGUIs[0][0]][$g_iHwnd] = $hGUI
$g_aSnapGUIs[$g_aSnapGUIs[0][0]][$g_iStyle] = 3

Local $tRectMoving = DllStructCreate($tagRECT)
_WinAPI_SystemParametersInfo(0x0030, 0, DllStructGetPtr($tRectMoving), 0);SPI_GETWORKAREA
If @error Then Return SetError(1, 0, 0)
$g_iDesktopHeight = DllStructGetData($tRectMoving, 'Bottom')

$g_iMonitorTotal = _WinAPI_GetSystemMetrics(80);SM_CMONITORS
If @error Then Return SetError(1, 0, 0)

Return 1

EndFunc   ;==>_WinSnap_Set

; #FUNCTION# ====================================================================================================
; Name...........: _WinSnap_SetStyle
; Description....: Sets what the window will snap to. Desktop edges or Window edges
; Syntax.........: _WinSnap_SetStyle($hGUI, $iFlag)
; Parameters.....: $hGUI - Handle to window to set style
; $iFlag - Style Value:
; | 0 = No Snap at all
; | 1 = Snap to Desktop Edges
; | 2 = Snap to Other Autoit Windows
; | 3 = Snap to all (Defualt)
; Return values..: Success - Returns the value of the previous setting
; Failure - 0 and set @error to 1
; Author.........: Beege
; ===============================================================================================================
Func _WinSnap_SetStyle($hGUI, $iFlag)

Local $iIndex = _GetIndex($hGUI)
If Not $iIndex Then Return SetError(1, 0, 0)

Local $iLast = $g_aSnapGUIs[$iIndex][$g_iStyle]
$g_aSnapGUIs[$iIndex][$g_iStyle] = $iFlag

Return $iLast

EndFunc   ;==>_WinSnap_SetStyle

#endregion Public functions

#region internel functions
; #FUNCTION# ====================================================================================================
; Name...........: _GetIndex
; Description....: returns array index for handle
; Syntax.........: _GetIndex($hGUI)
; Parameters.....: $hGUI - handle to window
; Return values..: Success - array index for gui handle
; Failure - 0
; Author.........: Beege
; ===============================================================================================================
Func _GetIndex($hGUI)
For $i = 1 To $g_aSnapGUIs[0][0]
If $hGUI = $g_aSnapGUIs[$i][$g_iHwnd] Then Return $i
Next
Return 0
EndFunc   ;==>_GetIndex

; #FUNCTION# ====================================================================================================
; Name...........: WS_WM_ENTERSIZEMOVE
; Description....:  Called when window begins to move.
; Syntax.........: WS_WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
; Return values..: None
; Author.........: Beege
; ===============================================================================================================
Func WS_WM_ENTERSIZEMOVE($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam, $lParam

Local $pGUI1 = WinGetPos($hWndGUI)
Local $aMousePos = MouseGetPos()

$g_bMovingStarted = False
$g_iMouseDistancetoX = $aMousePos[0] - $pGUI1[0]
$g_iMouseDistancetoY = $pGUI1[1] - $aMousePos[1]

EndFunc   ;==>WS_WM_ENTERSIZEMOVE

; #FUNCTION# ====================================================================================================
; Name...........: WS_WM_MOVING
; Description....: Called when a window is being moved
; Syntax.........: WS_WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
; Return values..: None
; Author.........: Beege
; ===============================================================================================================
Func WS_WM_MOVING($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam, $lParam

Static $iStartW, $iStartH

Local $iIndex = _GetIndex($hWndGUI)
If Not $iIndex Then Return $GUI_RUNDEFMSG

Local $tRectMoving = DllStructCreate($tagRECT, $lParam)

If Not $g_bMovingStarted Then
$g_bMovingStarted = True
$iStartW = DllStructGetData($tRectMoving, 'Right') - DllStructGetData($tRectMoving, 'Left')
$iStartH = DllStructGetData($tRectMoving, 'Bottom') - DllStructGetData($tRectMoving, 'Top')
EndIf

;If window is snaped we need to monitor how much the mouse has moved
;from is original drag point position on the title bar.
Local $aMousePos = MouseGetPos()
Local $iChangeinX = Abs($aMousePos[0] - (DllStructGetData($tRectMoving, 'Left') + $g_iMouseDistancetoX))
Local $iChangeinY = Abs((DllStructGetData($tRectMoving, 'Top') - $aMousePos[1]) - $g_iMouseDistancetoY)

Local $iTopMoving = DllStructGetData($tRectMoving, 'Top'), $iBottomMoving = DllStructGetData($tRectMoving, 'Bottom')
Local $iLeftMoving = DllStructGetData($tRectMoving, 'Left'), $iRightMoving = DllStructGetData($tRectMoving, 'Right')

#region Check if near desktop edges
;Check if style includes snaping to desktop edges
If BitAND($g_aSnapGUIs[$iIndex][$g_iStyle], 1) Then
For $i = 1 To $g_iMonitorTotal
If _IsClose($iRightMoving, (@DesktopWidth * $i)) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * $i) - $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Left', DllStructGetData($tRectMoving, 'Right') - $iStartW)
EndIf
Next

For $i = 0 To $g_iMonitorTotal - 1
If _IsClose($iLeftMoving, (@DesktopWidth * $i)) Then
DllStructSetData($tRectMoving, 'Left', (@DesktopWidth * $i) + $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf
Next

If _IsClose($iTopMoving, 0) Then
DllStructSetData($tRectMoving, 'Top', 0 + $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf

If _IsClose($iBottomMoving, $g_iDesktopHeight) Then
DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight - $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf

If _IsClose($iBottomMoving, @DesktopHeight) Then
DllStructSetData($tRectMoving, 'Bottom', @DesktopHeight - $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf

;Here we check if the mouse has moved from original drag point. if it has we unsnap the window
If $iChangeinX > 20 Then
DllStructSetData($tRectMoving, 'Left', $aMousePos[0] - $g_iMouseDistancetoX)
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf
If $iChangeinY > 15 Then
DllStructSetData($tRectMoving, 'Top', $aMousePos[1] + $g_iMouseDistancetoY)
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf

EndIf
#endregion Check if near desktop edges

#region Check if window in near other Autoit Windows
Local $aWinlist = WinList()
;Check if style of window includes other Autoit Windows
If BitAND($g_aSnapGUIs[$iIndex][$g_iStyle], 2) Then

For $i = 1 To $aWinlist[0][0]

If $aWinlist[$i][0] = '' Or $aWinlist[$i][1] = $hWndGUI Or Not _IsVisible($aWinlist[$i][1]) Then ContinueLoop

Local $tRectNonMoving = _WinAPI_GetWindowRect($aWinlist[$i][1])
Local $iTopNonMoving = DllStructGetData($tRectNonMoving, 'Top'), $iBottomNonMoving = DllStructGetData($tRectNonMoving, 'Bottom')
Local $iLeftNonMoving = DllStructGetData($tRectNonMoving, 'Left'), $iRightNonMoving = DllStructGetData($tRectNonMoving, 'Right')

If $iTopMoving <= ($iBottomNonMoving + 5) And ($iBottomMoving + 5) >= $iTopNonMoving Then
;Gui1Right to Gui2Right
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then
DllStructSetData($tRectMoving, 'Right', $iRightNonMoving - $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Left', DllStructGetData($tRectMoving, 'Right') - $iStartW)
EndIf

;Gui1Left to Gui2left
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then
DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving + $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf

;Gui1Left to Gui2Right
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then
DllStructSetData($tRectMoving, 'Left', $iRightNonMoving + $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf

;Gui1Right to Gui2Left
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then
DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving - $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Left', DllStructGetData($tRectMoving, 'Right') - $iStartW)
EndIf
EndIf

If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
;gui1top to gui2top
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then
DllStructSetData($tRectMoving, 'Top', $iTopNonMoving + $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf

;~  gui1top to gui2bottom
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then
DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving + $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf

;gui1bottom to gui2bottom
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then
DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving - $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf

;gui1bottom to gui2top
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then
DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving - $g_iGuiOffset) ; mod by cox
DllStructSetData($tRectMoving, 'Top', DllStructGetData($tRectMoving, 'Bottom') - $iStartH)
EndIf
EndIf

;Here we check if the mouse has moved from original drag point. if it has we unsnap the window
If $iChangeinX > 20 Then
DllStructSetData($tRectMoving, 'Left', $aMousePos[0] - $g_iMouseDistancetoX)
DllStructSetData($tRectMoving, 'Right', DllStructGetData($tRectMoving, 'Left') + $iStartW)
EndIf

If $iChangeinY > 15 Then
DllStructSetData($tRectMoving, 'Top', $aMousePos[1] + $g_iMouseDistancetoY)
DllStructSetData($tRectMoving, 'Bottom', DllStructGetData($tRectMoving, 'Top') + $iStartH)
EndIf
Next
EndIf
#endregion Check if window in near other Autoit Windows

Return $GUI_RUNDEFMSG

EndFunc   ;==>WS_WM_MOVING

; #FUNCTION# ====================================================================================================
; Name...........: WS_WM_SIZING
; Description....: Called when a window is being sized
; Syntax.........: WS_WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
; Return values..: None
; Author.........: Beege
; ===============================================================================================================
Func WS_WM_SIZING($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam, $lParam

If Not _GetIndex($hWndGUI) Then Return $GUI_RUNDEFMSG

Local $tRectMoving = DllStructCreate($tagRECT, $lParam)
Local $iLeftMoving = DllStructGetData($tRectMoving, 'Left'), $iRightMoving = DllStructGetData($tRectMoving, 'Right')
Local $iTopMoving = DllStructGetData($tRectMoving, 'Top'), $iBottomMoving = DllStructGetData($tRectMoving, 'Bottom')

#region check if resizing near desktop edge
Switch $wParam
Case 6; bottom edge
If _IsClose($iBottomMoving, $g_iDesktopHeight) Then DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight)
Case 7; bottom Left corner
If _IsClose($iBottomMoving, $g_iDesktopHeight) Then DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight)
If _IsClose($iLeftMoving, @DesktopWidth) Then DllStructSetData($tRectMoving, 'Left', @DesktopWidth)
If _IsClose($iLeftMoving, 0) Then DllStructSetData($tRectMoving, 'Left', 0)
Case 1; left
If _IsClose($iLeftMoving, @DesktopWidth) Then DllStructSetData($tRectMoving, 'Left', @DesktopWidth)
If _IsClose($iLeftMoving, 0) Then DllStructSetData($tRectMoving, 'Left', 0)
Case 2; right
If _IsClose($iRightMoving, @DesktopWidth) Then
DllStructSetData($tRectMoving, 'Right', @DesktopWidth)
ElseIf _IsClose($iRightMoving, @DesktopWidth * 2) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * 2))
EndIf
Case 8; bottom right
If _IsClose($iBottomMoving, $g_iDesktopHeight) Then DllStructSetData($tRectMoving, 'Bottom', $g_iDesktopHeight)
If _IsClose($iRightMoving, @DesktopWidth) Then
DllStructSetData($tRectMoving, 'Right', @DesktopWidth)
ElseIf _IsClose($iRightMoving, @DesktopWidth * 2) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * 2))
EndIf
Case 3; top
If _IsClose($iTopMoving, 0) Then DllStructSetData($tRectMoving, 'Top', 0)
Case 4; top left
If _IsClose($iTopMoving, 0) Then DllStructSetData($tRectMoving, 'Top', 0)
If _IsClose($iLeftMoving, @DesktopWidth) Then DllStructSetData($tRectMoving, 'Left', @DesktopWidth)
Case 5; top right
If _IsClose($iRightMoving, @DesktopWidth) Then
DllStructSetData($tRectMoving, 'Right', @DesktopWidth)
ElseIf _IsClose($iRightMoving, @DesktopWidth * 2) Then
DllStructSetData($tRectMoving, 'Right', (@DesktopWidth * 2))
EndIf
If _IsClose($iTopMoving, 0) Then DllStructSetData($tRectMoving, 'Top', 0)
EndSwitch
#endregion check if resizing near desktop edge

#region Check if resizing near other windows
Local $aWinlist = WinList()

For $i = 1 To $aWinlist[0][0]

If $aWinlist[$i][0] = '' Or $aWinlist[$i][1] = $hWndGUI Or Not _IsVisible($aWinlist[$i][1]) Then ContinueLoop

Local $tRectNonMoving = _WinAPI_GetWindowRect($aWinlist[$i][1])
Local $iLeftNonMoving = DllStructGetData($tRectNonMoving, 'Left'), $iRightNonMoving = DllStructGetData($tRectNonMoving, 'Right')
Local $iTopNonMoving = DllStructGetData($tRectNonMoving, 'Top'), $iBottomNonMoving = DllStructGetData($tRectNonMoving, 'Bottom')

Switch $wParam
Case 6; bottom edge
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving)
EndIf
Case 7; bottom Left corner
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving)
EndIf
Case 1; left
If $iTopMoving <= ($iBottomNonMoving + 5) And ($iBottomMoving + 5) >= $iTopNonMoving Then
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iRightNonMoving)
EndIf
Case 2; right
If $iTopMoving <= ($iBottomNonMoving + 5) And ($iBottomMoving + 5) >= $iTopNonMoving Then
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving)
EndIf
Case 8; bottom right
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Bottom'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Bottom', $iTopNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving)
EndIf
Case 3; top
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iTopNonMoving)
EndIf
Case 4; top left
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iTopNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iLeftNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Left'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Left', $iRightNonMoving)
If _IsClose($iLeftMoving, 0) Then DllStructSetData($tRectMoving, 'Left', 0)
EndIf
Case 5; top right
If ($iLeftMoving <= $iRightNonMoving + 5) And ($iRightMoving + 5 >= $iLeftNonMoving) Then
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iBottomNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iBottomNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Top'), $iTopNonMoving) Then DllStructSetData($tRectMoving, 'Top', $iTopNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iRightNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iRightNonMoving)
If _IsClose(DllStructGetData($tRectMoving, 'Right'), $iLeftNonMoving) Then DllStructSetData($tRectMoving, 'Right', $iLeftNonMoving)
EndIf
EndSwitch
Next
#endregion Check if resizing near other windows

Return $GUI_RUNDEFMSG

EndFunc   ;==>WS_WM_SIZING

; #FUNCTION# ====================================================================================================
; Name...........: _IsClose
; Description....: Tests if Position a is near Postion b
; Syntax.........: _IsClose($a, $b)
; Parameters.....: $a - Position 1
; $b - Position 2
; Return values..: True/False
; Author.........: Beege
; ===============================================================================================================
Func _IsClose($a, $b)
Return (Abs($a - $b) < 15)
EndFunc   ;==>_IsClose
#endregion internel functions

Func _IsVisible($handle)
Return (BitAND(WinGetState($handle), 2) = 2)
EndFunc   ;==>_IsVisible

So now you can script:

_WinSnap_Set($hGUI, $iOffset)

But you can leave $iOffset empty.

My standard offset is 5.

Regards, Conrad

P.S. Modding is marked with '; mod by cox'. So you can find it easily.

Edited by Simpel
P.S.

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