PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/scripts/MinimizeToTrayMenu.htm

http://autohotkey-chinese.googlecode.com/
HTML | 266 lines | 232 code | 34 blank | 0 comment | 0 complexity | 4f1fe240544f67626b06849c613ea2eb MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Minimize Window to Tray Menu</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <link rel="alternate" type="application/rss+xml" title="AutoHotkey Forum RSS" href="/forum/rss.php">
  8. <link href="/docs/css/default.css" rel="stylesheet" type="text/css">
  9. <link href="/docs/css/print.css" rel="stylesheet" type="text/css" media="print">
  10. </head>
  11. <body>
  12. <h6>Minimize Window to Tray Menu</h6>
  13. <p> This script assigns a hotkey of your choice to hide any window so that
  14. it becomes an entry at the bottom of the script's tray menu. Hidden
  15. windows can then be unhidden individually or all at once by selecting
  16. the corresponding item on the menu. If the script exits for any reason,
  17. all the windows that it hid will be unhidden automatically.
  18. </p>
  19. <p><a href="MinimizeToTrayMenu.ahk">Download This Script</a> &nbsp;| &nbsp;<a href="index.htm">Other Sample Scripts</a> &nbsp;| &nbsp;<a href="http://www.autohotkey.com">Home</a></p>
  20. <pre class="NoIndent"><em>; CHANGES:</em>
  21. <em>; July 22, 2005 (changes provided by egilmour):</em>
  22. <em>; - Added new hotkey to unhide the last hidden window (Win+U)</em>
  23. <em>;</em>
  24. <em>; November 3, 2004 (changes provided by trogdor):</em>
  25. <em>; - Program manager is prevented from being hidden.</em>
  26. <em>; - If there is no active window, the minimize-to-tray hotkey will have</em>
  27. <em>; no effect rather than waiting indefinitely.</em>
  28. <em>;</em>
  29. <em>; October 23, 2004:</em>
  30. <em>; - The taskbar is prevented from being hidden.</em>
  31. <em>; - Some possible problems with long window titles have been fixed.</em>
  32. <em>; - Windows without a title can be hidden without causing problems.</em>
  33. <em>; - If the script is running under AHK v1.0.22 or greater, the</em>
  34. <em>; maximum length of each menu item is increased from 100 to 260.</em>
  35. <em>; CONFIGURATION SECTION: Change the below values as desired.</em>
  36. <em>; This is the maximum number of windows to allow to be hidden (having a</em>
  37. <em>; limit helps performance):</em>
  38. mwt_MaxWindows = 50
  39. <em>; This is the hotkey used to hide the active window:</em>
  40. mwt_Hotkey = #h <em>; Win+H</em>
  41. <em>; This is the hotkey used to unhide the last hidden window:</em>
  42. mwt_UnHotkey = #u <em>; Win+U</em>
  43. <em>; If you prefer to have the tray menu empty of all the standard items,</em>
  44. <em>; such as Help and Pause, use N. Otherwise, use Y:</em>
  45. mwt_StandardMenu = N
  46. <em>; These next few performance settings help to keep the action within the</em>
  47. <em>; #HotkeyModifierTimeout period, and thus avoid the need to release and</em>
  48. <em>; press down the hotkey's modifier if you want to hide more than one</em>
  49. <em>; window in a row. These settings are not needed you choose to have the</em>
  50. <em>; script use the keyboard hook via #InstallKeybdHook or other means:</em>
  51. #HotkeyModifierTimeout 100
  52. SetWinDelay 10
  53. SetKeyDelay 0
  54. #SingleInstance <em>; Allow only one instance of this script to be running.</em>
  55. <em>; END OF CONFIGURATION SECTION (do not make changes below this point</em>
  56. <em>; unless you want to change the basic functionality of the script).</em>
  57. Hotkey, %mwt_Hotkey%, mwt_Minimize
  58. Hotkey, %mwt_UnHotkey%, mwt_UnMinimize
  59. <em>; If the user terminates the script by any means, unhide all the</em>
  60. <em>; windows first:</em>
  61. OnExit, mwt_RestoreAllThenExit
  62. if mwt_StandardMenu = Y
  63. Menu, Tray, Add
  64. else
  65. {
  66. Menu, Tray, NoStandard
  67. Menu, Tray, Add, E&amp;xit and Unhide All, mwt_RestoreAllThenExit
  68. }
  69. Menu, Tray, Add, &amp;Unhide All Hidden Windows, mwt_RestoreAll
  70. Menu, Tray, Add <em>; Another separator line to make the above more special.</em>
  71. if a_AhkVersion = <em>; Since it's blank, version is older than 1.0.22.</em>
  72. mwt_MaxLength = 100
  73. else
  74. mwt_MaxLength = 260 <em>; Reduce this to restrict the width of the menu.</em>
  75. return <em>; End of auto-execute section.</em>
  76. mwt_Minimize:
  77. if mwt_WindowCount &gt;= %mwt_MaxWindows%
  78. {
  79. MsgBox No more than %mwt_MaxWindows% may be hidden simultaneously.
  80. return
  81. }
  82. <em>; Set the &quot;last found window&quot; to simplify and help performance.</em>
  83. <em>; Since in certain cases it is possible for there to be no active window,</em>
  84. <em>; a timeout has been added:</em>
  85. WinWait, A,, 2
  86. if ErrorLevel &lt;&gt; 0 <em>; It timed out, so do nothing.</em>
  87. return
  88. <em>; Otherwise, the &quot;last found window&quot; has been set and can now be used:</em>
  89. WinGet, mwt_ActiveID, ID
  90. WinGetTitle, mwt_ActiveTitle
  91. WinGetClass, mwt_ActiveClass
  92. if mwt_ActiveClass in Shell_TrayWnd,Progman
  93. {
  94. MsgBox The desktop and taskbar cannot be hidden.
  95. return
  96. }
  97. <em>; Because hiding the window won't deactivate it, activate the window</em>
  98. <em>; beneath this one (if any). I tried other ways, but they wound up</em>
  99. <em>; activating the task bar. This way sends the active window (which is</em>
  100. <em>; about to be hidden) to the back of the stack, which seems best:</em>
  101. Send, !{esc}
  102. <em>; Hide it only now that WinGetTitle/WinGetClass above have been run (since</em>
  103. <em>; by default, those commands cannot detect hidden windows):</em>
  104. WinHide
  105. <em>; If the title is blank, use the class instead. This serves two purposes:</em>
  106. <em>; 1) A more meaningful name is used as the menu name.</em>
  107. <em>; 2) Allows the menu item to be created (otherwise, blank items wouldn't</em>
  108. <em>; be handled correctly by the various routines below).</em>
  109. if mwt_ActiveTitle =
  110. mwt_ActiveTitle = ahk_class %mwt_ActiveClass%
  111. <em>; Ensure the title is short enough to fit. mwt_ActiveTitle also serves to</em>
  112. <em>; uniquely identify this particular menu item.</em>
  113. StringLeft, mwt_ActiveTitle, mwt_ActiveTitle, %mwt_MaxLength%
  114. <em>; In addition to the tray menu requiring that each menu item name be</em>
  115. <em>; unique, it must also be unique so that we can reliably look it up in</em>
  116. <em>; the array when the window is later unhidden. So make it unique if it</em>
  117. <em>; isn't already:</em>
  118. Loop, %mwt_MaxWindows%
  119. {
  120. if mwt_WindowTitle%a_index% = %mwt_ActiveTitle%
  121. {
  122. <em>; Match found, so it's not unique.</em>
  123. <em>; First remove the 0x from the hex number to conserve menu space:</em>
  124. StringTrimLeft, mwt_ActiveIDShort, mwt_ActiveID, 2
  125. StringLen, mwt_ActiveIDShortLength, mwt_ActiveIDShort
  126. StringLen, mwt_ActiveTitleLength, mwt_ActiveTitle
  127. mwt_ActiveTitleLength += %mwt_ActiveIDShortLength%
  128. mwt_ActiveTitleLength += 1 <em>; +1 the 1 space between title &amp; ID.</em>
  129. if mwt_ActiveTitleLength &gt; %mwt_MaxLength%
  130. {
  131. <em>; Since menu item names are limted in length, trim the title</em>
  132. <em>; down to allow just enough room for the Window's Short ID at</em>
  133. <em>; the end of its name:</em>
  134. TrimCount = %mwt_ActiveTitleLength%
  135. TrimCount -= %mwt_MaxLength%
  136. StringTrimRight, mwt_ActiveTitle, mwt_ActiveTitle, %TrimCount%
  137. }
  138. <em>; Build unique title:</em>
  139. mwt_ActiveTitle = %mwt_ActiveTitle% %mwt_ActiveIDShort%
  140. break
  141. }
  142. }
  143. <em>; First, ensure that this ID doesn't already exist in the list, which can</em>
  144. <em>; happen if a particular window was externally unhidden (or its app unhid</em>
  145. <em>; it) and now it's about to be re-hidden:</em>
  146. mwt_AlreadyExists = n
  147. Loop, %mwt_MaxWindows%
  148. {
  149. if mwt_WindowID%a_index% = %mwt_ActiveID%
  150. {
  151. mwt_AlreadyExists = y
  152. break
  153. }
  154. }
  155. <em>; Add the item to the array and to the menu:</em>
  156. if mwt_AlreadyExists = n
  157. {
  158. Menu, Tray, add, %mwt_ActiveTitle%, RestoreFromTrayMenu
  159. mwt_WindowCount += 1
  160. Loop, %mwt_MaxWindows% <em>; Search for a free slot.</em>
  161. {
  162. <em>; It should always find a free slot if things are designed right.</em>
  163. if mwt_WindowID%a_index% = <em>; An empty slot was found.</em>
  164. {
  165. mwt_WindowID%a_index% = %mwt_ActiveID%
  166. mwt_WindowTitle%a_index% = %mwt_ActiveTitle%
  167. break
  168. }
  169. }
  170. }
  171. return
  172. RestoreFromTrayMenu:
  173. Menu, Tray, delete, %A_ThisMenuItem%
  174. <em>; Find window based on its unique title stored as the menu item name:</em>
  175. Loop, %mwt_MaxWindows%
  176. {
  177. if mwt_WindowTitle%a_index% = %A_ThisMenuItem% <em>; Match found.</em>
  178. {
  179. StringTrimRight, IDToRestore, mwt_WindowID%a_index%, 0
  180. WinShow, ahk_id %IDToRestore%
  181. WinActivate ahk_id %IDToRestore% <em>; Sometimes needed.</em>
  182. mwt_WindowID%a_index% = <em>; Make it blank to free up a slot.</em>
  183. mwt_WindowTitle%a_index% =
  184. mwt_WindowCount -= 1
  185. break
  186. }
  187. }
  188. return
  189. <em>;; This will pop the last minimized window off the stack and unhide it.</em>
  190. mwt_UnMinimize:
  191. <em>;; Make sure there's something to unhide.</em>
  192. if mwt_WindowCount &gt; 0
  193. {
  194. <em>;; Get the id of the last window minimized and unhide it</em>
  195. StringTrimRight, IDToRestore, mwt_WindowID%mwt_WindowCount%, 0
  196. WinShow, ahk_id %IDToRestore%
  197. WinActivate ahk_id %IDToRestore%
  198. <em>;; Get the menu name of the last window minimized and remove it</em>
  199. StringTrimRight, MenuToRemove, mwt_WindowTitle%mwt_WindowCount%, 0
  200. Menu, Tray, delete, %MenuToRemove%
  201. <em>;; clean up our 'arrays' and decrement the window count</em>
  202. mwt_WindowID%mwt_WindowCount% =
  203. mwt_WindowTitle%mwt_WindowCount% =
  204. mwt_WindowCount -= 1
  205. }
  206. return
  207. mwt_RestoreAllThenExit:
  208. Gosub, mwt_RestoreAll
  209. ExitApp <em>; Do a true exit.</em>
  210. mwt_RestoreAll:
  211. Loop, %mwt_MaxWindows%
  212. {
  213. if mwt_WindowID%a_index% &lt;&gt;
  214. {
  215. StringTrimRight, IDToRestore, mwt_WindowID%a_index%, 0
  216. WinShow, ahk_id %IDToRestore%
  217. WinActivate ahk_id %IDToRestore% <em>; Sometimes needed.</em>
  218. <em>; Do it this way vs. DeleteAll so that the sep. line and first</em>
  219. <em>; item are retained:</em>
  220. StringTrimRight, MenuToRemove, mwt_WindowTitle%a_index%, 0
  221. Menu, Tray, delete, %MenuToRemove%
  222. mwt_WindowID%a_index% = <em>; Make it blank to free up a slot.</em>
  223. mwt_WindowTitle%a_index% =
  224. mwt_WindowCount -= 1
  225. }
  226. if mwt_WindowCount = 0
  227. break
  228. }
  229. return
  230. </pre>
  231. </body>
  232. </html>