Advertisement
Guest User

AltDrag for Windows

a guest
Jul 18th, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Easy Window Dragging -- KDE style (requires XP/2k/NT) -- by Jonny
  2. ; http://www.autohotkey.com
  3. ; This script makes it much easier to move or resize a window: 1) Hold down
  4. ; the ALT key and LEFT-click anywhere inside a window to drag it to a new
  5. ; location; 2) Hold down ALT and RIGHT-click-drag anywhere inside a window
  6. ; to easily resize it; 3) Press ALT twice, but before releasing it the second
  7. ; time, left-click to minimize the window under the mouse cursor, right-click
  8. ; to maximize it, or middle-click to close it.
  9.  
  10. ; This script was inspired by and built on many like it
  11. ; in the forum. Thanks go out to ck, thinkstorm, Chris,
  12. ; and aurelian for a job well done.
  13.  
  14. ; Change history:
  15. ; November 07, 2006: Optimized resizing code in !RButton, courtesy of bluedawn.
  16. ; February 05, 2006: Fixed double-alt (the ~Alt hotkey) to work with latest versions of AHK.
  17.  
  18. ; The Double-Alt modifier is activated by pressing
  19. ; Alt twice, much like a double-click. Hold the second
  20. ; press down until you click.
  21. ;
  22. ; The shortcuts:
  23. ;  Alt + Left Button  : Drag to move a window.
  24. ;  Alt + Right Button : Drag to resize a window.
  25. ;  Double-Alt + Left Button   : Minimize a window.
  26. ;  Double-Alt + Right Button  : Maximize/Restore a window.
  27. ;  Double-Alt + Middle Button : Close a window.
  28. ;
  29. ; You can optionally release Alt after the first
  30. ; click rather than holding it down the whole time.
  31.  
  32. If (A_AhkVersion < "1.0.39.00")
  33. {
  34.     MsgBox,20,,This script may not work properly with your version of AutoHotkey. Continue?
  35.     IfMsgBox,No
  36.     ExitApp
  37. }
  38.  
  39.  
  40. ; This is the setting that runs smoothest on my
  41. ; system. Depending on your video card and cpu
  42. ; power, you may want to raise or lower this value.
  43. SetWinDelay,2
  44.  
  45. CoordMode,Mouse
  46. return
  47.  
  48. !LButton::
  49. If DoubleAlt
  50. {
  51.     MouseGetPos,,,KDE_id
  52.     ; This message is mostly equivalent to WinMinimize,
  53.     ; but it avoids a bug with PSPad.
  54.     PostMessage,0x112,0xf020,,,ahk_id %KDE_id%
  55.     DoubleAlt := false
  56.     return
  57. }
  58. ; Get the initial mouse position and window id, and
  59. ; abort if the window is maximized.
  60. MouseGetPos,KDE_X1,KDE_Y1,KDE_id
  61. WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
  62. If KDE_Win
  63.     return
  64. ; Get the initial window position.
  65. WinGetPos,KDE_WinX1,KDE_WinY1,,,ahk_id %KDE_id%
  66. Loop
  67. {
  68.     GetKeyState,KDE_Button,LButton,P ; Break if button has been released.
  69.     If KDE_Button = U
  70.         break
  71.     MouseGetPos,KDE_X2,KDE_Y2 ; Get the current mouse position.
  72.     KDE_X2 -= KDE_X1 ; Obtain an offset from the initial mouse position.
  73.     KDE_Y2 -= KDE_Y1
  74.     KDE_WinX2 := (KDE_WinX1 + KDE_X2) ; Apply this offset to the window position.
  75.     KDE_WinY2 := (KDE_WinY1 + KDE_Y2)
  76.     WinMove,ahk_id %KDE_id%,,%KDE_WinX2%,%KDE_WinY2% ; Move the window to the new position.
  77. }
  78. return
  79.  
  80. !RButton::
  81. If DoubleAlt
  82. {
  83.     MouseGetPos,,,KDE_id
  84.     ; Toggle between maximized and restored state.
  85.     WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
  86.     If KDE_Win
  87.         WinRestore,ahk_id %KDE_id%
  88.     Else
  89.         WinMaximize,ahk_id %KDE_id%
  90.     DoubleAlt := false
  91.     return
  92. }
  93. ; Get the initial mouse position and window id, and
  94. ; abort if the window is maximized.
  95. MouseGetPos,KDE_X1,KDE_Y1,KDE_id
  96. WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
  97. If KDE_Win
  98.     return
  99. ; Get the initial window position and size.
  100. WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
  101. ; Define the window region the mouse is currently in.
  102. ; The four regions are Up and Left, Up and Right, Down and Left, Down and Right.
  103. If (KDE_X1 < KDE_WinX1 + KDE_WinW / 2)
  104.     KDE_WinLeft := 1
  105. Else
  106.     KDE_WinLeft := -1
  107. If (KDE_Y1 < KDE_WinY1 + KDE_WinH / 2)
  108.     KDE_WinUp := 1
  109. Else
  110.     KDE_WinUp := -1
  111. Loop
  112. {
  113.     GetKeyState,KDE_Button,RButton,P ; Break if button has been released.
  114.     If KDE_Button = U
  115.         break
  116.     MouseGetPos,KDE_X2,KDE_Y2 ; Get the current mouse position.
  117.     ; Get the current window position and size.
  118.     WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
  119.     KDE_X2 -= KDE_X1 ; Obtain an offset from the initial mouse position.
  120.     KDE_Y2 -= KDE_Y1
  121.     ; Then, act according to the defined region.
  122.     WinMove,ahk_id %KDE_id%,, KDE_WinX1 + (KDE_WinLeft+1)/2*KDE_X2  ; X of resized window
  123.                             , KDE_WinY1 +   (KDE_WinUp+1)/2*KDE_Y2  ; Y of resized window
  124.                             , KDE_WinW  -     KDE_WinLeft  *KDE_X2  ; W of resized window
  125.                             , KDE_WinH  -       KDE_WinUp  *KDE_Y2  ; H of resized window
  126.     KDE_X1 := (KDE_X2 + KDE_X1) ; Reset the initial position for the next iteration.
  127.     KDE_Y1 := (KDE_Y2 + KDE_Y1)
  128. }
  129. return
  130.  
  131. ; "Alt + MButton" may be simpler, but I
  132. ; like an extra measure of security for
  133. ; an operation like this.
  134. !MButton::
  135. If DoubleAlt
  136. {
  137.     MouseGetPos,,,KDE_id
  138.     WinClose,ahk_id %KDE_id%
  139.     DoubleAlt := false
  140.     return
  141. }
  142. return
  143.  
  144. ; This detects "double-clicks" of the alt key.
  145. ~Alt::
  146. DoubleAlt := A_PriorHotkey = "~Alt" AND A_TimeSincePriorHotkey < 400
  147. Sleep 0
  148. KeyWait Alt  ; This prevents the keyboard's auto-repeat feature from interfering.
  149. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement