private void moveToFront(WindowInfo windowInfo) { windows.Remove(windowInfo); windows.AddFirst(windowInfo); }
private void remove(WindowInfo windowInfo) { windows.Remove(windowInfo); windowsCache.Remove(windowInfo.Handle); }
private void add(WindowInfo windowInfo) { windows.AddFirst(windowInfo); windowsCache.Add(windowInfo.Handle, windowInfo); }
/// <summary> /// WinEventDelegate for the creation & destruction /// </summary> /// <param name="hWinEventHook"></param> /// <param name="eventType"></param> /// <param name="hWnd"></param> /// <param name="idObject"></param> /// <param name="idChild"></param> /// <param name="dwEventThread"></param> /// <param name="dwmsEventTime"></param> private void WinEventDelegate(IntPtr hWinEventHook, WinEvent eventType, IntPtr hWnd, EventObjects idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { if (hWnd == IntPtr.Zero || idObject != EventObjects.OBJID_WINDOW) { if (idObject != EventObjects.OBJID_CARET && idObject != EventObjects.OBJID_CURSOR) { LOG.InfoFormat("Unhandled eventType: {0}, hWnd {1}, idObject {2}, idChild {3}, dwEventThread {4}, dwmsEventTime {5}", eventType, hWnd, idObject, idChild, dwEventThread, dwmsEventTime); } return; } WindowInfo windowInfo = null; IntPtr hWndParent; bool isPreviouslyCreated = windowsCache.TryGetValue(hWnd, out windowInfo); if (windowInfo == null) { hWndParent = User32.GetParent(hWnd); windowInfo = WindowInfo.CreateFor(hWnd, hWndParent); } else { hWndParent = windowInfo.Parent; } if (windowInfo.Classname == "OleMainThreadWndClass") { // Not a peeps, this window is not interresting. return; } LOG.InfoFormat("eventType {0}, hWnd {1}, idObject {2}, idChild {3}, dwEventThread {4}, dwmsEventTime {5}", eventType, hWnd, idObject, idChild, dwEventThread, dwmsEventTime); switch (eventType) { case WinEvent.EVENT_OBJECT_NAMECHANGE: if (windowsCache.TryGetValue(hWnd, out windowInfo)) { // Force update of Text windowInfo.Text = null; LOG.InfoFormat("Rename {0} / {1}", windowInfo.Text, windowInfo.Classname); } else { // Huh? LOG.WarnFormat("No record of a window with hWnd {0}", hWnd); } break; case WinEvent.EVENT_OBJECT_CREATE: if (!isPreviouslyCreated) { if (hWndParent != IntPtr.Zero) { WindowInfo parent; if (!windowsCache.TryGetValue(hWndParent, out parent)) { parent = WindowInfo.CreateFor(hWndParent); add(parent); } parent.Children.Add(windowInfo); LOG.InfoFormat("Added child {0} / {1} to {2} / {3} / {4}", windowInfo.Text, windowInfo.Classname, parent.Handle, parent.Text, parent.Classname); } else { add(windowInfo); LOG.InfoFormat("Added {0} / {1}", windowInfo.Text, windowInfo.Classname); } } else { LOG.InfoFormat("'Activated' parent {0} / {1}", windowInfo.Text, windowInfo.Classname); } break; case WinEvent.EVENT_OBJECT_DESTROY: if (isPreviouslyCreated) { LOG.InfoFormat("Removing {0} / {1}", windowInfo.Text, windowInfo.Classname); remove(windowInfo); } else { if (hWndParent != IntPtr.Zero) { if (windowsCache.TryGetValue(hWnd, out windowInfo)) { LOG.WarnFormat("Unhandled destroy of Child from {0}", windowInfo.Text); // Implement child removal } else { LOG.WarnFormat("No record of a child-window with hWnd {0}", hWnd); } } else { LOG.WarnFormat("No record of a top-window with hWnd {0}", hWnd); } } break; case WinEvent.EVENT_OBJECT_FOCUS: // Move the top-window with the focus to the foreground if (isPreviouslyCreated) { moveToFront(windowInfo); LOG.InfoFormat("Focus: {0} / {1}", windowInfo.Text, windowInfo.Classname); } break; case WinEvent.EVENT_OBJECT_LOCATIONCHANGE: case WinEvent.EVENT_SYSTEM_MOVESIZESTART: case WinEvent.EVENT_SYSTEM_MOVESIZEEND: // Move the top-window with the focus to the foreground if (isPreviouslyCreated) { windowInfo.Bounds = System.Drawing.Rectangle.Empty; LOG.InfoFormat("Move/resize: {2} - {0} / {1}", windowInfo.Text, windowInfo.Classname, windowInfo.Bounds); } break; } }