Sign in to follow this  
Followers 0
Lazycat

Form Snap

5 posts in this topic

Very simple example, result of playing with new GUIRegisterMsg function. This is fasionable effect - form is snap to screen edges when moving.

#include <GUIConstants.au3>

Global Const $WM_WINDOWPOSCHANGING = 0x0046 
Global Const $SPI_GETWORKAREA = 0x30

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

$hGUI = GUICreate("Snapped window", 300, 200)

GUIRegisterMsg($WM_WINDOWPOSCHANGING, "MY_WM_WINDOWPOSCHANGING")

GUISetState()

While 1
    $GUIMsg = GUIGetMsg()
    
    Switch $GUIMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd


Func MY_WM_WINDOWPOSCHANGING($hWnd, $Msg, $wParam, $lParam)
#cs
    HWND hwnd;
    HWND hwndInsertAfter;
    int x;
    int y;
    int cx;
    int cy;
    UINT flags;
#ce
    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)
    Local $nRight  = DllStructGetData($stRect, 3) - DllStructGetData($stWinPos, 5)
    Local $nBottom = DllStructGetData($stRect, 4) - DllStructGetData($stWinPos, 6)
    If BitAND($nEdge, 1) and Abs($nLeft   - DllStructGetData($stWinPos, 3)) <= $nGap Then DllStructSetData($stWinPos, 3, $nLeft)
    If BitAND($nEdge, 2) and Abs($nTop  - DllStructGetData($stWinPos, 4)) <= $nGap Then DllStructSetData($stWinPos, 4, $nTop)
    If BitAND($nEdge, 4) and Abs($nRight  - DllStructGetData($stWinPos, 3)) <= $nGap Then DllStructSetData($stWinPos, 3, $nRight)
    If BitAND($nEdge, 8) and Abs($nBottom - DllStructGetData($stWinPos, 4)) <= $nGap Then DllStructSetData($stWinPos, 4, $nBottom)
    Return 0
EndFunc

Share this post


Link to post
Share on other sites



Thats nice, I like it. Would it be possible to snap to another window?

Share this post


Link to post
Share on other sites

@Lazycat: Great!

Some weeks ago, I developed a similar function without DLLCall for a script that I will share here, but for any reason I had some problems with the desktop's corners...WINDOWPOSCHANGING do it better...

Thanks.

Share this post


Link to post
Share on other sites

Very nice, thanks!

And it so much less code than if I had tried to do this myself :P

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  
Followers 0