; http://www.autoitscript.com/forum/topic/24342-form-snap/page__view__findpost__p__170144
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global Const $SPI_GETWORKAREA = 0x30

Global $nGap = 20, $nEdge = BitOR(1, 2, 4, 8); Left, Top, Right, Bottom
Global $k = 0, $TrMg = 0

$hGUI = GUICreate("WM_WINDOWPOSCHANGING", 370, 330, -1, -1, $WS_OVERLAPPEDWINDOW)
$condition = GUICtrlCreateLabel('Функция WM_WINDOWPOSCHANGING выполняется в момент перемещения окна, изменения размеров, удерживание мыши на заголовке и отпуске мыши.', 5, 5, 360, 270)
$Checkbox = GUICtrlCreateCheckbox('Прилипать к краям экрана', 10, 275, 170, 17)

GUISetState()
GUIRegisterMsg($WM_WINDOWPOSCHANGING, "WM_WINDOWPOSCHANGING")

While 1
	Switch GUIGetMsg()
		Case $Checkbox
			If GUICtrlRead($Checkbox) = 1 Then
				$TrMg = 1
			Else
				$TrMg = 0
			EndIf
		Case $GUI_EVENT_CLOSE
			Exit
	EndSwitch
WEnd

Func WM_WINDOWPOSCHANGING($hWnd, $msg, $wParam, $lParam)
	Local $stRect = DllStructCreate("int;int;int;int")
	Local $stWinPos = DllStructCreate("uint;uint;int;int;int;int;uint", $lParam)
	DllCall("User32.dll", "int", "SystemParametersInfo", "int", $SPI_GETWORKAREA, "int", 0, "ptr", DllStructGetPtr($stRect), "int", 0)
	Local $nLeft = DllStructGetData($stRect, 1)
	Local $nTop = DllStructGetData($stRect, 2)
	$w = DllStructGetData($stWinPos, 5)
	$h = DllStructGetData($stWinPos, 6)
	$x = DllStructGetData($stWinPos, 3)
	$y = DllStructGetData($stWinPos, 4)
	Local $nRight = DllStructGetData($stRect, 3) - $w
	Local $nBottom = DllStructGetData($stRect, 4) - $h
	If $TrMg = 1 Then
		If Abs($nLeft - $x) <= $nGap Then DllStructSetData($stWinPos, 3, $nLeft)
		If Abs($nTop - $y) <= $nGap Then DllStructSetData($stWinPos, 4, $nTop)
		If Abs($nRight - $x) <= $nGap Then DllStructSetData($stWinPos, 3, $nRight)
		If Abs($nBottom - $y) <= $nGap Then DllStructSetData($stWinPos, 4, $nBottom)
	EndIf
	
	$k += 1
	WinSetTitle($hGUI, '', 'Вызов ' & $k & ' раз, w=' & $w & ', h=' & $h)
	
	GUICtrlSetData($condition, 'Вызов ' & $k & ' раз' & @CRLF & @CRLF & _
		'--- Размеры окна ---' & @CRLF & _
		'w=' & $w & @CRLF & _
		'h=' & $h & @CRLF & @CRLF & _
		'--- Координаты окна ---' & @CRLF & _
		'x=' & $x & @CRLF & _
		'y=' & $y & @CRLF & @CRLF & _
		'Left=' & $nLeft & @CRLF & _
		'Top=' & $nTop & @CRLF & @CRLF & _
		'--- Разница между размером экрана и размером окна ---' & @CRLF & _
		'nRight=' & $nRight & @CRLF & _
		'nBottom=' & $nBottom & @CRLF & @CRLF & _
		'--- Размеры экрана (без ширины панели задач) ---' & @CRLF & _
		'stRect3=' & DllStructGetData($stRect, 3) & @CRLF & _
		'stRect4=' & DllStructGetData($stRect, 4))
	Return 0
EndFunc