Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Alt+LButton window dragging


  • Please log in to reply
19 replies to this topic
lasmori
  • Members
  • 4 posts
  • Last active: Jan 13 2008 07:54 AM
  • Joined: 13 Jan 2008
I was not entirely happy with the existing scripts for doing this, so I ended up writing my own.

The script itself is a bit simpler then the others for a couple reasons:
1) I did not write or include any other functionality besides window dragging
2) The logic for moving the window is a bit simpler over the other scripts I've seen
3) IMHO, the script is commented and formatted a bit better.


Anyhow, nothing great. :)

EDIT: Updated to be even simpler thanks to a couple tips posted by Lexikos

;-------------------------------------------------
; Window dragging via alt+lbutton                -
; Author: Lasmori (email AT lasmori D0T com)     -
;-------------------------------------------------
!LButton::

CoordMode, Mouse, Relative
MouseGetPos, cur_win_x, cur_win_y, window_id
WinGet, window_minmax, MinMax, ahk_id %window_id%

; Return if the window is maximized or minimized
if window_minmax <> 0
{
  return
}

CoordMode, Mouse, Screen
SetWinDelay, 0

loop
{
  ; exit the loop if the left mouse button was released
  if !GetKeyState("LButton", "P")
  {
    break
  }

  ; move the window based on cursor position
  MouseGetPos, cur_x, cur_y
  WinMove, ahk_id %window_id%,, (cur_x - cur_win_x), (cur_y - cur_win_y)
}

return
;-------------------------------------------------


AHKisNice
  • Members
  • 35 posts
  • Last active: May 22 2010 01:57 PM
  • Joined: 11 Jan 2008
Hello, thanks for the script. I alt+left clicked on a window's titlebar and nothing happened. What is the script supposed to do? How do i use it?

lasmori
  • Members
  • 4 posts
  • Last active: Jan 13 2008 07:54 AM
  • Joined: 13 Jan 2008
it lets you move windows. I am using it because my taskbar is on the top and windows tend to get stuck with their titlebars hidden.

It wont work on maximized windows, though it should work great on normal windows.

Its also meant for using on the inside or body or the application, not the title bar... as that already works :)

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007

Hello, thanks for the script. I alt+left clicked on a window's titlebar and nothing happened. What is the script supposed to do? How do i use it?

The idea is you can "grab" a window by any part (not just the title bar) if you hold down alt.

AHKisNice
  • Members
  • 35 posts
  • Last active: May 22 2010 01:57 PM
  • Joined: 11 Jan 2008
lol oh thanks, nice script lasmori.

I am using it because my taskbar is on the top and windows tend to get stuck with their titlebars hidden.

Why did you move the taskbar to the top? I am going to try that to see if I like it.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
I like how explicitly your variables are named. I'm usually much too lazy. :lol:

I have a few suggestions:
[*:33lb0q1b]A_WinDelay (not A_Win_Delay!) and similar settings need not be reset manually if the thread is going to end.

SetWinDelay:
Every newly launched thread (such as a hotkey, custom menu item, or timed subroutine) starts off fresh with the default setting for this command. That default may be changed by using this command in the auto-execute section (top part of the script).

Threads:
When resumed, a thread's settings for things such as ErrorLevel and SendMode are automatically restored to what they were just prior to its interruption;

[*:33lb0q1b]GetKeyState is available as a function, in case you want to avoid an unnecessary variable.
if ! GetKeyState("LButton", "P")
; or
if GetKeyState("LButton", "P") = 0
; instead of
GetKeyState, lbutton_state, LButton, P
if  lbutton_state = U
[*:33lb0q1b]All purely numeric args (excluding MsgBox) also accept expressions.
WinMove, ahk_id %window_id%,, cur_x - cur_win_x, cur_y - cur_win_y
; instead of
window_x := cur_x - cur_win_x
window_y := cur_y - cur_win_y
WinMove, ahk_id %window_id%,, %window_x%, %window_y%


superdsp
  • Members
  • 15 posts
  • Last active: Oct 25 2010 02:49 PM
  • Joined: 18 Jan 2008

I was not entirely happy with the existing scripts for doing this, so I ended up writing my own.

The script itself is a bit simpler then the others for a couple reasons:
1) I did not write or include any other functionality besides window dragging
2) The logic for moving the window is a bit simpler over the other scripts I've seen
3) IMHO, the script is commented and formatted a bit better.


Anyhow, nothing great. :)

;-------------------------------------------------
; Window dragging via alt+lbutton                -
; Author: Lasmori (email AT lasmori D0T com)     -
;-------------------------------------------------
!LButton::
original_win_delay := A_Win_Delay

CoordMode, Mouse, Relative
MouseGetPos, cur_win_x, cur_win_y, window_id
WinGet, window_minmax, MinMax, ahk_id %window_id%

; Return if the window is maximized or minimized
if window_minmax <> 0 
{
  return
}

CoordMode, Mouse, Screen
SetWinDelay, 0

loop
{
  ; exit the loop if the left mouse button was released
  GetKeyState, lbutton_state, LButton, P
  if lbutton_state = U
  {
    break
  }

  MouseGetPos, cur_x, cur_y
  window_x := cur_x - cur_win_x
  window_y := cur_y - cur_win_y
  WinMove, ahk_id %window_id%,, %window_x%, %window_y%
}

SetWinDelay, %original_win_delay%

return
;-------------------------------------------------


Very nice!

Razlin
  • Members
  • 454 posts
  • Last active: Feb 05 2014 06:21 PM
  • Joined: 05 Nov 2007
Didnt test the script but many people dont know that if you

ALT+Space ona window

then press M = move

You can use the arrrow keys to move a window then press enter to "execute" the move

this is part of windows not ahk.

Anyhow.. my 2 copper

Da Rossa
  • Members
  • 401 posts
  • Last active: Mar 26 2016 10:20 AM
  • Joined: 06 Dec 2007
Nice script brother! i'm using it! Are you planning to write a clean code for the Window resize function too? :)
AHK is perfect.

lasmori
  • Members
  • 4 posts
  • Last active: Jan 13 2008 07:54 AM
  • Joined: 13 Jan 2008

Nice script brother! i'm using it! Are you planning to write a clean code for the Window resize function too? :)


Sister, not brother :)

Hmm... I had no plans to do so but could if someone wants it bad enough. I obviously haven't been paying attention to the forum for a while. (Actually just had surgery...)


I also wanted to thank the above poster on the tips.

lasmori
  • Members
  • 4 posts
  • Last active: Jan 13 2008 07:54 AM
  • Joined: 13 Jan 2008

lol oh thanks, nice script lasmori.

I am using it because my taskbar is on the top and windows tend to get stuck with their titlebars hidden.

Why did you move the taskbar to the top? I am going to try that to see if I like it.


I dunno... I am a mac user so its just more familiar :)

Da Rossa
  • Members
  • 401 posts
  • Last active: Mar 26 2016 10:20 AM
  • Joined: 06 Dec 2007

Nice script brother! i'm using it! Are you planning to write a clean code for the Window resize function too? :)


Sister, not brother :)

Hmm... I had no plans to do so but could if someone wants it bad enough. I obviously haven't been paying attention to the forum for a while. (Actually just had surgery...)


I also wanted to thank the above poster on the tips.


AHK is perfect.

liquidos
  • Members
  • 1 posts
  • Last active: Apr 23 2008 12:10 PM
  • Joined: 18 Apr 2008
Thanks, this script really works nicely! I had one problem with it though. When I dragged a window it stayed beneath other windows. I added this line to make it be the top window:

SetWinDelay, 0 ; After this
WinSet, Top,, ahk_id %window_id%
The problem might be because some third party program or the TweakUI xmouse feature, I don't know, but it probably doesn't add much overhead.. :)

Pacman
  • Guests
  • Last active:
  • Joined: --
I found that the background windows would not move properly because the mouse position was calculated relative to the active window. This version of the code corrects the movement of background windows.

;--------------------------------------------------
; Window dragging via alt+lbutton                 -
; Author: Lasmori (email AT lasmori D0T com)      -
; http://www.autohotkey.com/forum/topic27487.html -
;--------------------------------------------------
!LButton::

; Fixed to move background windows properly
CoordMode, Mouse, Screen

MouseGetPos, , , id ; get ID of window under cursor
WinGetTitle, title, ahk_id %id% ; get title of window under cursor
SetTitleMatchMode 3 ; match window that has the exact name as %title%
WinGetPos, win_x, win_y, , , %title% ; get upper left corner of window
MouseGetPos, current_x, current_y, window_id ; get cursor position on the screen (not relative to window)
cur_win_x := current_x - win_x ; calculate relative cursor position
cur_win_y := current_y - win_y
WinGet, window_minmax, MinMax, ahk_id %window_id%

; Return if the window is maximized or minimized
if window_minmax <> 0
{
  return
}

CoordMode, Mouse, Screen
SetWinDelay, 0

loop
{
  ; exit the loop if the left mouse button was released
  if !GetKeyState("LButton", "P")
  {
    break
  }

  ; move the window based on cursor position
  MouseGetPos, cur_x, cur_y
  WinMove, ahk_id %window_id%,, (cur_x - cur_win_x), (cur_y - cur_win_y)
}

return


Pacman
  • Guests
  • Last active:
  • Joined: --
I cleaned up my code a touch by using the window ID instead of the window title when retrieving the upper left corner coordinates. The result is 2 fewer lines.

;--------------------------------------------------
; Window dragging via alt+lbutton                 -
; Author: Lasmori (email AT lasmori D0T com)      -
; http://www.autohotkey.com/forum/topic27487.html -
;--------------------------------------------------
!LButton::

; Fixed to move background windows properly
CoordMode, Mouse, Screen

MouseGetPos, , , id ; get ID of window under cursor
WinGetPos, win_x, win_y, , , ahk_id %id% ; get upper left corner of window
MouseGetPos, current_x, current_y, window_id ; get cursor position on the screen (not relative to window)
cur_win_x := current_x - win_x ; calculate relative cursor position
cur_win_y := current_y - win_y
WinGet, window_minmax, MinMax, ahk_id %window_id%

; Return if the window is maximized or minimized
if window_minmax <> 0
{
  return
}

CoordMode, Mouse, Screen
SetWinDelay, 0

loop
{
  ; exit the loop if the left mouse button was released
  if !GetKeyState("LButton", "P")
  {
    break
  }

  ; move the window based on cursor position
  MouseGetPos, cur_x, cur_y
  WinMove, ahk_id %window_id%,, (cur_x - cur_win_x), (cur_y - cur_win_y)
}

return