/MessageHooks.ahk

http://7plus.googlecode.com/ · AutoHotKey · 270 lines · 234 code · 7 blank · 29 comment · 79 complexity · bc1be231a4baa6ee5afd0d8bd5215a55 MD5 · raw file

  1. ; see http://msdn.microsoft.com/en-us/library/dd318066(VS.85).aspxs
  2. HookProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime ){
  3. global ResizeWindow, SlideWindows, WindowList
  4. ListLines, Off
  5. hwnd += 0
  6. ;On dialog popup, check if its an explorer confirmation dialog
  7. if(event = 0x00008002) ;EVENT_OBJECT_SHOW
  8. {
  9. if(IsObject(Settings) && Settings.Explorer.AutoCheckApplyToAllFiles && WinVer >= WIN_Vista)
  10. FixExplorerConfirmationDialogs()
  11. return
  12. }
  13. if idObject or idChild ;Doesn't each much time, skip for profiling
  14. return
  15. WinGet, style, Style, ahk_id %hwnd%
  16. if (style & 0x40000000) ;return if hwnd is child window, for some reason idChild may be 0 for some children ?!?! ( I hate ms )
  17. return
  18. if(event = 0x0016) ;EVENT_SYSTEM_MINIMIZEEND
  19. {
  20. Trigger := new CWindowStateChangeTrigger()
  21. Trigger.Window := hwnd
  22. Trigger.Event := "Window minimized"
  23. EventSystem.OnTrigger(Trigger)
  24. }
  25. else if(event = 0x8001 && IsObject(Settings) && Settings.Explorer.Tabs.UseTabs) ;EVENT_OBJECT_DESTROY
  26. {
  27. ; DecToHex(hwnd)
  28. ; if(TabContainerList.ContainsHWND(hwnd))
  29. }
  30. else if(event = 0x800B) ;EVENT_OBJECT_LOCATIONCHANGE
  31. {
  32. WinGet, state, minmax, ahk_id %hwnd%
  33. if(state = 1)
  34. {
  35. Trigger := new CWindowStateChangeTrigger()
  36. Trigger.Window := hwnd
  37. Trigger.Event := "Window maximized"
  38. EventSystem.OnTrigger(Trigger)
  39. }
  40. if(InStr("CabinetWClass,ExploreWClass", WinGetClass("ahk_id " hwnd)))
  41. ExplorerMoved(hwnd)
  42. if(IsObject(SlideWindows))
  43. SlideWindows.CheckResizeReleaseCondition(hwnd)
  44. if(state != -1)
  45. {
  46. WindowList.MovedWindow := hwnd
  47. SetTimer, UpdateWindowPosition, -1000
  48. }
  49. }
  50. else if(event = 0x000A && Settings.Windows.ShowResizeTooltip)
  51. {
  52. ResizeWindow := hwnd
  53. SetTimer, ResizeWindowTooltip, 50
  54. SlideWindows.CheckResizeReleaseCondition(hwnd)
  55. }
  56. else if(event = 0x000B)
  57. {
  58. ShowTip({Min : 4, Max : 7}, 0.1)
  59. if(Settings.Windows.ShowResizeTooltip)
  60. {
  61. ResizeWindow := ""
  62. SetTimer, ResizeWindowTooltip, Off
  63. ResizeWindowTooltip(true)
  64. Tooltip
  65. }
  66. }
  67. ListLines, On
  68. }
  69. ResizeWindowTooltip:
  70. ResizeWindowTooltip()
  71. return
  72. ResizeWindowTooltip(reset = false)
  73. {
  74. global ResizeWindow
  75. static w,h
  76. if(reset)
  77. {
  78. w:=0
  79. h:=0
  80. return
  81. }
  82. WinGetPos, , , wn, hn, ahk_id %ResizeWindow%
  83. if(w && h && (w != wn || h != hn))
  84. Tooltip %w%/%h%
  85. w := wn
  86. h := hn
  87. }
  88. ;See http://msdn.microsoft.com/en-us/library/ms644991(VS.85).aspx
  89. ShellMessage( wParam, lParam, Msg)
  90. {
  91. WasCritical := A_IsCritical
  92. Critical
  93. ListLines, Off
  94. global BlinkingWindows, WindowList, Accessor, RecentCreateCloseEvents, ToolWindows, ExplorerWindows, LastWindow, LastWindowClass, SlideWindows, CurrentWindow, PreviousWindow, ExplorerHistory
  95. Trigger := new COnMessageTrigger()
  96. Trigger.Message := wParam
  97. Trigger.lParam := lParam
  98. Trigger.Msg := Msg
  99. EventSystem.OnTrigger(Trigger)
  100. if(wParam = 1 || wParam = 2) ;Window Created/Closed
  101. {
  102. lParam += 0
  103. ;Keep a list of recently received create/close messages, because they can be sent multiple times and we only want one.
  104. if(!IsObject(RecentCreateCloseEvents))
  105. RecentCreateCloseEvents := Array()
  106. SetTimer, ClearRecentCreateCloseEvents, -300
  107. if(!RecentCreateCloseEvents.HasKey(lParam))
  108. {
  109. RecentCreateCloseEvents[lParam] := 1
  110. Trigger := wParam = 1 ? new CWindowCreatedTrigger() : new CWindowClosedTrigger()
  111. class:= wParam = 1 ? WinGetClass("ahk_Id " lParam) : (IsObject(WindowList) && IsObject(WindowList[lParam]) ? WindowList[lParam].class : "INVALID WINDOW CLASS")
  112. Trigger.Window := lParam
  113. EventSystem.OnTrigger(Trigger)
  114. ;Keep a list of windows and their required info stored. This allows to identify windows which were closed recently.
  115. WinGet, hwnds, list,,, Program Manager
  116. Loop, %hwnds%
  117. {
  118. hwnd := hwnds%A_Index%+0
  119. WinGetTitle, title, ahk_id %hwnd%
  120. if(IsObject(WindowList[hwnd]))
  121. WindowList[hwnd].title := title
  122. else
  123. {
  124. WinGetClass, class, ahk_id %hwnd%
  125. WinGet, exe, ProcessName, ahk_id %hwnd%
  126. WinGet, Path, ProcessPath, ahk_id %hwnd%
  127. WindowList[hwnd] := Object("class", class, "title", title, "Executable", exe, "Path", Path)
  128. }
  129. }
  130. }
  131. if(wParam = 2)
  132. {
  133. if(IsObject(WindowList[lParam]) && InStr("CabinetWClass,ExploreWClass", WindowList[lParam].class))
  134. GoSub WaitForClose
  135. else ;Code below is also executed in WaitForClose for separate Explorer handling (why can't explorer send close messages properly like a normal window??)
  136. {
  137. if(IsObject(ToolWindows))
  138. {
  139. Loop % ToolWindows.MaxIndex()
  140. {
  141. if(ToolWindows[A_Index].hParent = lParam && ToolWindows[A_Index].AutoClose)
  142. {
  143. WinClose % "ahk_id " ToolWindows[A_Index].hGui
  144. ToolWindows.Remove(A_Index)
  145. break
  146. }
  147. }
  148. }
  149. SlideWindows.WindowClosed(lParam)
  150. }
  151. }
  152. if(wParam = 1)
  153. {
  154. if(IsObject(SlideWindows))
  155. SlideWindows.WindowCreated(lParam)
  156. AutoCloseWindowsUpdate(lParam)
  157. ;~ SlideWindows.CreatedWindow := lParam
  158. ;~ SetTimer, SlideWindows_WindowCreated, -100
  159. } ; SlideWindows.WindowCreated(lParam)
  160. }
  161. ;Blinking windows detection, add new blinking windows
  162. else if(wParam = 32774)
  163. {
  164. lParam += 0
  165. if(!BlinkingWindows.indexOf(lParam))
  166. {
  167. BlinkingWindows.Insert(lParam)
  168. ShowTip(12)
  169. }
  170. }
  171. ;Window Activation
  172. else if(wParam = 4 || wParam = 32772) ;HSHELL_WINDOWACTIVATED||HSHELL_RUDEAPPACTIVATED
  173. {
  174. ShowTip(13, 0.05)
  175. if(IsAltTabWindow(lParam))
  176. {
  177. PreviousWindow := CurrentWindow
  178. CurrentWindow := lParam
  179. }
  180. lParam += 0
  181. Trigger := new CWindowActivatedTrigger()
  182. EventSystem.OnTrigger(Trigger)
  183. ;Blinking windows detection, remove activated windows
  184. if(x := BlinkingWindows.indexOf(lParam))
  185. BlinkingWindows.Delete(x)
  186. if(IsObject(CAccessor.Instance.GUI) && CAccessor.Instance.Settings.CloseWhenDeactivated && WinExist("A") != CAccessor.Instance.GUI.hwnd)
  187. CAccessor.Instance.Close()
  188. ;If we change from another program to explorer/desktop/dialog
  189. if((IsExplorer := WinActive("ahk_group ExplorerGroup"))||WinActive("ahk_group DesktopGroup")||IsDialog())
  190. {
  191. if(!WinActive("ahk_group DesktopGroup")) ;By doing this, recall explorer path works also when double clicking desktop to launch explorer
  192. Settings.Explorer.CurrentPath := Navigation.GetPath()
  193. ;Paste text/image as file file creation
  194. CreateFileFromClipboard()
  195. if((IsExplorer && ExplorerWindows.GetItemWithValue("hwnd", IsExplorer).Path != Settings.Explorer.CurrentPath) || !IsExplorer)
  196. {
  197. Entry := RichObject()
  198. Name := Entry.Path := Settings.Explorer.CurrentPath
  199. SplitPath, Name, Name
  200. Entry.Name := IsExplorer ? Navigation.GetDisplayName(lParam) : Name
  201. Entry.Usage := 0
  202. Entry := ExplorerHistory.Push(Entry) ;This can return a different entry that already exists in the list!
  203. Entry.Usage++
  204. }
  205. }
  206. if(LastWindowClass && InStr("CabinetWClass,ExploreWClass", LastWindowClass) && !ExplorerWindows.TabContainerList.TabCreationInProgress && !ExplorerWindows.TabContainerList.TabActivationInProgress)
  207. ExplorerDeactivated(LastWindow)
  208. LastWindow := lParam
  209. LastWindowClass := WinGetClass("ahk_id " lParam)
  210. if(InStr("CabinetWClass,ExploreWClass", LastWindowClass) && LastWindowClass && !ExplorerWindows.TabContainerList.TabCreationInProgress && !ExplorerWindows.TabContainerList.TabActivationInProgress)
  211. ExplorerActivated(LastWindow)
  212. if(IsObject(SlideWindows))
  213. SlideWindows.WindowActivated()
  214. }
  215. ;Redraw is fired on Explorer path change
  216. else if(wParam = 6)
  217. {
  218. lParam += 0
  219. ;Detect changed path
  220. if(InStr("CabinetWClass,ExploreWClass", WinGetClass("ahk_id " lParam)))
  221. {
  222. ExplorerPathChanged(ExplorerWindows.GetItemWithValue("hwnd", lParam))
  223. ; newpath := Navigation.GetPath()
  224. ; if(newpath && newpath != Settings.Explorer.CurrentPath)
  225. ; {
  226. ; outputdebug Explorer path changed from %ExplorerPath% to %newpath%
  227. ; ExplorerPathChanged(Settings.Explorer.CurrentPath, newpath)
  228. ; Settings.Explorer.PreviousPath := Settings.Explorer.CurrentPath
  229. ; Settings.Explorer.CurrentPath := newpath
  230. ; Trigger := new CExplorerPathChangedTrigger()
  231. ; EventSystem.OnTrigger(Trigger)
  232. ; if(Settings.Explorer.Tabs.UseTabs && !SuppressTabEvents && hwnd:=WinActive("ahk_group ExplorerGroup"))
  233. ; UpdateTabs()
  234. ; }
  235. }
  236. }
  237. ListLines, On
  238. if(!WasCritical)
  239. Critical, Off
  240. }
  241. ;Timer for clearing the list of recently received create/close events
  242. ClearRecentCreateCloseEvents:
  243. RecentCreateCloseEvents := Array()
  244. return
  245. UpdateWindowPosition:
  246. UpdateWindowPosition()
  247. return
  248. UpdateWindowPosition()
  249. {
  250. global WindowList
  251. WinGetPos, x, y, w, h, % "ahk_id " WindowList.MovedWindow
  252. if(!IsObject(WindowList[WindowList.MovedWindow]))
  253. return
  254. WindowList[WindowList.MovedWindow].x := x
  255. WindowList[WindowList.MovedWindow].y := y
  256. WindowList[WindowList.MovedWindow].w := w
  257. WindowList[WindowList.MovedWindow].h := h
  258. }
  259. WM_POWERBROADCAST(wParam, lParam, msg)
  260. {
  261. if (wParam = 18)
  262. AutoCloseWindowsUpdate(WinExist("Windows Update ahk_class #32770"))
  263. }