Jump to content

[SOLVED] Window dragging limits


Recommended Posts

What is the simplest way to limit a GuiWindow from being dragged off the screen(i still want the ability to drag), or limit it to be only dragged in a limited area on the screen.

Here is my sample script.

Local $msg

GUICreate("IT DEPARTMENT - Restart Required", 415, 145, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS), $WS_EX_TOPMOST ) ; Creates GUI Window

    ;GUICreate("My GUI") ; will create a dialog box that when displayed is centered
    GUISetState(@SW_SHOW) ; will display an empty dialog box

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()

Any help would be greatly appreciated.

Thanks,

Rich

Edited by rich2323
Link to comment
Share on other sites

  • Moderators

rich2323,

This is a way to limit the coords within the WM_MOVE message function - but I am sure there must be a better way than using WinMove: :)

There is - see below! :idea:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $iX_Min = 100, $iX_Max = @DesktopWidth - 100, $iY_Min = 100, $iY_Max = @DesktopHeight - 100

$hGUI = GUICreate("Test", 500, 500)

GUISetState()

GUIRegisterMsg($WM_MOVE, "WM_MOVE")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Func WM_MOVE($hWnd, $iMsg, $iwParam, $ilParam)
    if ($hWnd = $hGUI) then
        $aWinPos = WinGetPos($hWnd)

        If $aWinPos[0] < $iX_Min Then WinMove($hGUI, "", $iX_Min, Default)
        If $aWinPos[1] < $iY_Min Then WinMove($hGUI, "", Default, $iY_Min)
        If $aWinPos[0] + $aWinPos[2] > $iX_Max Then WinMove($hGUI, "", $iX_Max - $aWinPos[2], Default)
        If $aWinPos[1] + $aWinPos[3] > $iY_Max Then WinMove($hGUI, "", Default, $iY_Max - $aWinPos[3])

    EndIf
EndFunc

I will keep looking. :)

M23

Edit:

I was looking in the wrong place - we needed to intercept WM_WINDOWPOSCHANGING: :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Global $iX_Min = 100, $iX_Max = @DesktopWidth - 600, $iY_Min = 100, $iY_Max = @DesktopHeight - 600

$hGUI = GUICreate("Test", 500, 500)

GUISetState()

GUIRegisterMsg($WM_WINDOWPOSCHANGING, "WM_WINDOWPOSCHANGING")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Func WM_WINDOWPOSCHANGING($hWnd, $Msg, $wParam, $lParam)

    Local $stWinPos = DllStructCreate("uint;uint;int;int;int;int;uint", $lParam)

    Local $iLeft = DllStructGetData($stWinPos, 3)
    Local $iTop = DllStructGetData($stWinPos, 4)
    Local $iWidth = DllStructGetData($stWinPos, 5)
    Local $iHeight = DllStructGetData($stWinPos, 6)

    If $iLeft < $iX_Min Then DllStructSetData($stWinPos, 3, $iX_Min)
    If $iTop < $iY_Min Then DllStructSetData($stWinPos, 4, $iY_Min)
    If $iLeft > $iX_Max Then DllStructSetData($stWinPos, 3, $iX_Max)
    If $iTop > $iY_Max Then DllStructSetData($stWinPos, 4, $iY_Max)

EndFunc

I hope that suffices! :(

M23

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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