Jump to content

LImit Area user can drag window


Bilgus
 Share

Recommended Posts

Example of Intercepting WM_MOVING to restrict area the window can be moved to

;Autoit v3.3.14.5 Limit Form Move Example-- Bilgus 2018
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPISysWin.au3> ;_WinAPI_DefWindowProc()

Global $g_hGUI, $g_idLimit

Global $g_aWindowLimits = [15, 10, @DesktopWidth / 2, @DesktopHeight / 2] ; Min X, Min Y, Max X, Max Y

GUIRegisterMsg($WM_MOVING, WM_MOVING) ;Register a callback function for $WM_MOVING

Example()

Func Example()
    ; Create a GUI with various controls.
    $g_hGUI = GUICreate("Example")
    $g_idLimit = GUICtrlCreateCheckbox("Limit Drag", 10, 370, 85, 25)
    Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)


    ; Display the GUI.
    GUISetState(@SW_SHOW, $g_hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idOK
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($g_hGUI)
EndFunc   ;==>Example

Func WM_MOVING($hWnd, $iMsg, $wParam, $lParam)

    If $hWnd = $g_hGUI Then
        Local $bLimited = False
        
        ;Current rect is passed in lParam
        Local $tRect = DllStructCreate($tagRect, $lParam) ; (IS NOT X, Y, W, H) => (IS X, Y, X+W, Y+H)
        Local $iLeft = DllStructGetData($tRect, 1)
        Local $iTop = DllStructGetData($tRect, 2)
        Local $iRight = DllStructGetData($tRect, 3)
        Local $iBottom = DllStructGetData($tRect, 4)
        Local $iWidth, $iHeight

        ;Check left and right of window against imposed limits
        If $iLeft < $g_aWindowLimits[0] Then
            $iWidth = $iRight - $iLeft ;Calculate Original Width
            $iLeft = $g_aWindowLimits[0]
            $iRight = $iLeft + $iWidth ;Have to keep our same Width
            $bLimited = True
        ElseIf $iRight > $g_aWindowLimits[2] Then
            $iWidth = $iRight - $iLeft ;Calculate Original Width
            $iRight = $g_aWindowLimits[2]
            $iLeft = $iRight - $iWidth ;Have to keep our same Width
            $bLimited = True
        EndIf

        ;Check top and bottom of window against imposed limits
        If $iTop < $g_aWindowLimits[1] Then
            $iHeight = $iBottom - $iTop ;Calculate Original Height
            $iTop = $g_aWindowLimits[1]
            $iBottom = $iTop + $iHeight ;Have to keep our same Height
            $bLimited = True
        ElseIf $iBottom > $g_aWindowLimits[3] Then
            $iHeight = $iBottom - $iTop ;Calculate Original Height
            $iBottom = $g_aWindowLimits[3]
            $iTop = $iBottom - $iHeight ;Have to keep our same Height
            $bLimited = True
        EndIf

        If $bLimited And BitAND(GUICtrlRead($g_idLimit), $GUI_CHECKED) Then ;Limit happened -- Pass new Values Back to WndProc
            DllStructSetData($tRect, 1, $iLeft)
            DllStructSetData($tRect, 2, $iTop)
            DllStructSetData($tRect, 3, $iRight)
            DllStructSetData($tRect, 4, $iBottom)
            _WinAPI_DefWindowProc($hWnd, $iMsg, $wParam, $lParam);Pass new Rect on to default window procedure
            Return 1 ; True
        EndIf

    EndIf
    Return $GUI_RUNDEFMSG ;Default Handler
EndFunc   ;==>WM_MOVING

 

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...