Move and resize windows with mouse?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ozi
Posts: 10
Joined: 12 Jan 2021, 04:29

Move and resize windows with mouse?

08 May 2021, 02:45

Dear AHK Experts,

could you help me with next challenge, pls.

I created s script (some parts was copied and modified from existing projects) to use CapsLock with mouse for moving and resizing window dragging for any place on window. Now I want to modify it do the same but with mouse only. Scenario is next:
1. LButton+RButton - drag
2. RButton+Lbutton - resize
Both actions work but not perfect. E.g. Lbutton does a click action and selecting/modifying text in window in action 1. RButton fired context menu after finishing action 2.
I tried different approaches (took from google and this forum) but they didn't work for me.
So, any help on this will be much appreciated.
Probably script can be refactored in better way, in this case you are welcome to correct me. Thank you!

Code: Select all

; Drag windows anywhere
Capslock & LButton::
CoordMode, Mouse
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
mv_mode = mv
SetTimer, EWD_WatchMouse, 10
return

; Resize windows
Capslock & RButton::
    CoordMode, Mouse
    MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
    WinGetPos, ,, EWD_OriginalPosX, EWD_OriginalPosY, ahk_id %EWD_MouseWin%
    mv_mode = sz
    SetTimer, EWD_WatchMouse, 10
return
;---------------------------------------------------
; Drag windows anywhere
~LButton & RButton::
While GetKeyState("RButton", "P") {
    CoordMode, Mouse
    MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
    WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
    mv_mode = mv
    SetTimer, EWD_WatchMouse, 10
}
Send {Escape}
return

; Resize windows
~RButton & LButton::
While GetKeyState("LButton", "P") {
    CoordMode, Mouse
    MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
    WinGetPos, ,, EWD_OriginalPosX, EWD_OriginalPosY, ahk_id %EWD_MouseWin%
    mv_mode = sz
    SetTimer, EWD_WatchMouse, 10
}
Send {Escape}
return

;---------------------------------------------------
; Function for move and resize windows
EWD_WatchMouse:
    if mv_mode = mv
    {
        GetKeyState, EWD_MButtonState, LButton, P
        WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
    }
    if mv_mode = sz
    {
        GetKeyState, EWD_MButtonState, RButton, P
        WinGetPos, ,, EWD_WinX, EWD_WinY, ahk_id %EWD_MouseWin%
    }
    if EWD_MButtonState = U
    {
        SetTimer, EWD_WatchMouse, off
        return
    }
    CoordMode, Mouse
    MouseGetPos, EWD_MouseX, EWD_MouseY
    SetWinDelay, -1   ; Makes the below move faster/smoother.
    if mv_mode = mv
        WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
    if mv_mode = sz
        WinMove, ahk_id %EWD_MouseWin%,,,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
    EWD_MouseStartX := EWD_MouseX
    EWD_MouseStartY := EWD_MouseY
    WinActivate ahk_id %EWD_MouseWin%
return
Last edited by ozi on 08 May 2021, 06:08, edited 1 time in total.
ozi
Posts: 10
Joined: 12 Jan 2021, 04:29

Re: Move and resize windows with mouse?

08 May 2021, 06:13

Thank you, Mike
I checked this variant but as to me, my approach is more convenient. You may drag window using any window area, not only with caption.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Move and resize windows with mouse?

08 May 2021, 10:05

Code: Select all

/* Easy Window Dragging (requires XP/2k/NT) ------------------------------------------------------------------------
https://www.autohotkey.com/
Normally, a window can only be dragged by clicking on its title bar. This script extends that so that any point
inside a window can be dragged. To activate this mode, hold down CapsLock or the middle mouse button while clicking,
then drag the window to a new position. Note: You can optionally release CapsLock or the middle mouse button after
pressing down the mouse button rather than holding it down the whole time. This script requires v1.0.25+.
--------------------------------------------------------------------------------------------------------------------
*/
CapsLock & LButton::
Capslock & RButton::
CoordMode, Mouse
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, % wTitle := "ahk_id " EWD_MouseWin
WinGet, EWD_WinState, MinMax, %wTitle%
If !EWD_WinState
 SetTimer, EWD_WatchMouse, 50
Return

EWD_WatchMouse:
If !(GetKeyState("LButton", "P") | GetKeyState("RButton", "P")) {
 SetTimer, EWD_WatchMouse, Off
 Return
} Else If GetKeyState("Escape", "P") {
 SetTimer, EWD_WatchMouse, Off
 WinMove, %wTitle%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
 Return
} Else CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY, width, height, %wTitle%
SetWinDelay, -1
If Instr(A_ThisHotkey, "LB")
 WinMove, %wTitle%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
Else WinMove, %wTitle%,,,, width + EWD_MouseX - EWD_MouseStartX, height + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX, EWD_MouseStartY := EWD_MouseY
Return
ozi
Posts: 10
Joined: 12 Jan 2021, 04:29

Re: Move and resize windows with mouse?

09 May 2021, 01:59

You's script more neat. Thnx
From quick testing my variant works smoother. But I'll review the way to use it.

So, question for now more in proper usage of Right and Left buttons.
ozi
Posts: 10
Joined: 12 Jan 2021, 04:29

Re: Move and resize windows with mouse?

09 May 2021, 02:25

Decreasing of timer value from 50 to 10 made it smooth. So, only implementation of LButton+RButton and their proper treatment left.
ozi
Posts: 10
Joined: 12 Jan 2021, 04:29

Re: Move and resize windows with mouse?

09 May 2021, 03:35

Looks, I made it almost perfect from behavior prospective. The only small blink is after RButton releasing on resize - suppressing context menu firing. Probably it can be refactored a bit to make it nicer. So, the latest variant for now is:

Code: Select all

; Drag window anywhere
Capslock & LButton::
    CoordMode, Mouse
    MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
    WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, % wTitle := "ahk_id " EWD_MouseWin
    mv_mode = mv
    SetTimer, EWD_WatchMouse, 10
return

; Resize window
Capslock & RButton::
    CoordMode, Mouse
    MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
    WinGetPos, ,, EWD_OriginalPosX, EWD_OriginalPosY, % wTitle := "ahk_id " EWD_MouseWin
    mv_mode = sz
    SetTimer, EWD_WatchMouse, 10
return

; Drag window if LButton+RButton
#If GetKeyState("LButton", "P")
RButton::
    CoordMode, Mouse
    MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
    While GetKeyState("RButton", "P") {
        WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, % wTitle := "ahk_id " EWD_MouseWin
        mv_mode = mv
        SetTimer, EWD_WatchMouse, 10
        Send {Esc}
    }
return
#If

; Resize window if RButton+LButton
#If GetKeyState("RButton", "P")
LButton::
    CoordMode, Mouse
    MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
    While GetKeyState("LButton", "P") {
        WinGetPos, ,, EWD_OriginalPosX, EWD_OriginalPosY, % wTitle := "ahk_id " EWD_MouseWin
        mv_mode = sz
        SetTimer, EWD_WatchMouse, 10
        KeyWait, Rbutton ;As soon as RButton is released...
        Send {Esc}  ;... kill the context menu
    }
return
#If

; Function for move and resize windows
EWD_WatchMouse:
    CoordMode, Mouse
    MouseGetPos, EWD_MouseX, EWD_MouseY
    SetWinDelay, -1   ; Makes the below move faster/smoother.
    if mv_mode = mv 
    {
        GetKeyState, EWD_MButtonState, LButton, P
        WinGetPos, EWD_WinX, EWD_WinY,,, %wTitle%
        WinMove, %wTitle%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
    }
    if mv_mode = sz 
    {
        GetKeyState, EWD_MButtonState, RButton, P
        WinGetPos, ,, Width, Height, %wTitle%
        WinMove, %wTitle%,,,, Width + EWD_MouseX - EWD_MouseStartX, Height + EWD_MouseY - EWD_MouseStartY
    }
    if EWD_MButtonState = U 
    {
        SetTimer, EWD_WatchMouse, off
        return
    }
    EWD_MouseStartX := EWD_MouseX
    EWD_MouseStartY := EWD_MouseY
    WinActivate %wTitle%
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 234 guests