"'Non Titlebar-limited/From anywhere-within' Window Mover" Script Improvement Help

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
OrangeCat
Posts: 24
Joined: 14 Jun 2022, 00:47

"'Non Titlebar-limited/From anywhere-within' Window Mover" Script Improvement Help

25 Apr 2024, 06:34

Hello...

I found & tweaked a script that does exactly as post's title description, although I'd like to learn/to come with a different approach for arriving to the same result. As long as the cursor is anywhere within selected-window's area, I can move the entire window where-ever (The same action that would normally be done from a window's Title Bar).

As I'm still learning while/loop with combination of keystate and etc. thus avoided said keystate combination via an infinite loop usage where I separately use a "LButton Up" to break-out from infinite loop.

Here is the script,
I'd greatly appreciate if someone could please quickly fix it so that I can avoid using LButton Up

Code: Select all

;                   .-----------.
; ××××××××××××××××××| VARIABLES |××××××××××××××××××
;                   '==========='

    WindowDragHotkey := 0

;             .-----------------------.
; ××××××××××××| SCRIPT VIA SUBROUTINE |××××××××××××
;             '======================='

        WindowDragOn:
            MouseGetPos, MX1, MY1, WinID
            WinExist("ahk_id " . WinID)  
            WinGetPos, WX, WY  
            While WindowDragHotkey = 1
                {
                MouseGetPos, MX2, MY2
                If (MX1 <> MX2) || (MY1 <> MY2) 
                    {
                    WX += MX2 - MX1
                    WY += MY2 - MY1
                    WinMove, WX, WY  
                    MX1 := MX2
                    MY1 := MY2
                    }
                }
            Return
         RETURN

;                    .---------.
; ×××××××××××××××××××| HOTKEYS |×××××××××××××××××××
;                    '========='

        ~LButton & MButton::
            WindowDragHotkey = 1
            Gosub WindowDragOn
        RETURN

        ~LButton Up::
            WindowDragHotkey = 0
        RETURN

So...
- LButton & MButton activates it;
- Then I release MButton (but still keep LButton held down)
I'm now into an infinite loop and able to move the window to where-ever/from where-ever within
Releasing said held LButton then breaks out of this infinite loop and ceases "cursor-glued window movement".

But again,
I'd greatly appreciate help on achieving this exact same executed-behavior, but without the use of "LButton Up::"
If someone could please...
User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: "'Non Titlebar-limited/From anywhere-within' Window Mover" Script Improvement Help

25 Apr 2024, 07:55

Just a comment here: I believe that the script to achieve this goal was already posted previously, so you might want to search the forum for it.
User avatar
OrangeCat
Posts: 24
Joined: 14 Jun 2022, 00:47

Re: "'Non Titlebar-limited/From anywhere-within' Window Mover" Script Improvement Help

25 Apr 2024, 15:45

Thanks,
I found Juho's version and tweaked it a little bit...

For those interested:

Code: Select all


	SetWinDelay, -1
	CoordMode, Mouse, Screen

;                .--------.
; ×××××××××××××××| HOTKEY |×××××××××××××××
;                '========'
	~LButton & MButton::Gosub, WindowDrag

;                .--------.
; ×××××××××××××××| SCRIPT |×××××××××××××××
;                '========'
        WindowDrag:
            MouseGetPos, Mouse_Start_X, Mouse_Start_Y, Selected_Window
            WinGet, Window_State, MinMax, ahk_id %Selected_Window%
            If Window_State = 0
                SetTimer, WinDrag, 1
        RETURN


            WinDrag:
                MouseGetPos, Mouse_Current_X, Mouse_Current_Y
                WinGetPos, Selected_Window_X, Selected_Window_Y, , , ahk_id %Selected_Window%
                WinMove, ahk_id %Selected_Window%, , Selected_Window_X + Mouse_Current_X - Mouse_Start_X, Selected_Window_Y + Mouse_Current_Y - Mouse_Start_Y

                Mouse_Start_X := Mouse_Current_X
                Mouse_Start_Y := Mouse_Current_Y

                GetKeyState, LButton_State, LButton, P
                If LButton_State = U
                {
                    SetTimer, WinDrag, Off
                    Return
                }
            RETURN


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Google [Bot], peter_ahk and 103 guests