/ahk_scripts/EasyWindowDrag_(KDE).ahk

http://github.com/jixiuf/my_autohotkey_scripts · AutoHotKey · 153 lines · 93 code · 13 blank · 47 comment · 0 complexity · 18f5b3d568349253ebd26966d6b7902a MD5 · raw file

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