Jump to content

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

Moving a window via mouse without dragging its title bar


  • Please log in to reply
5 replies to this topic
Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
The below example shows how to drag a spot anywhere inside a window and make the whole window follow the mouse. Although it uses a GUI window, it could be adapted to work on any particular window, or even on all windows.
gui, -caption +0x40000  ; 0x40000 is WS_THICKFRAME (needed for AlwaysOnTop). 

gui, add, text,, Text to display

gui, show

WinGet, GuiID, ID, A

WinSet, AlwaysOnTop, On, A

return



~LButton::

CoordMode, Mouse

MouseGetPos, MouseStartX, MouseStartY, MouseWin

if MouseWin <> %GuiID%

	return

; Otherwise, track the mouse as the user drags it:

SetTimer, WatchMouse, 10

return



WatchMouse:

GetKeyState, LButtonState, LButton, P

if LButtonState = U  ; Button has been released, so drag is complete.

{

	SetTimer, WatchMouse, off

	return

}

; Otherwise, reposition the window to match the change in mouse coordinates

; caused by the user having dragged the mouse:

CoordMode, Mouse

MouseGetPos, MouseX, MouseY

DeltaX = %MouseX%

DeltaX -= %MouseStartX%

DeltaY = %MouseY%

DeltaY -= %MouseStartY%

MouseStartX = %MouseX%  ; Update for the next timer call to this subroutine.

MouseStartY = %MouseY%

WinGetPos, GuiX, GuiY,,, ahk_id %GuiID%

GuiX += %DeltaX%

GuiY += %DeltaY%

SetWinDelay, -1   ; Makes the below move faster/smoother.

WinMove, ahk_id %GuiID%,, %GuiX%, %GuiY%

return


BoBo
  • Guests
  • Last active:
  • Joined: --
Available in the scripts showcase section ? Yes, or ?
Yes, yes, yes (the "bit" would answer @ Tron)

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
It seems a little too obscure for that. But it's here in case anyone ever searches for such a thing.

  • Guests
  • Last active:
  • Joined: --
I'm not a very expereienced AHK user, so my understanding of this script is superficial to say the least. I get the jist of it, though. Would it be possible to apply the concept to all windows, with a hotkey like ALT+Lbutton, instead of just the gui AHK creates? (Linux style)

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Here is a revised version that works on any window. Since there is more interest than I expected, I've put it here: http://www.autohotke... ... owDrag.htm

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Chris, did I ever tell you you're my hero? It took all of 30 seconds to get one of my fave *nix features ported. :shock: