public static GetClassName ( |
||
hWnd | ||
return | string |
// This runs on the Excel main thread (usually, not always) - get off quickly void HandleWinEvent(IntPtr hWinEventHook, WinEvent eventType, IntPtr hWnd, WinEventObjectId idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { // Debug.Print($"++++++++++++++ WinEvent Received: {eventType} on thread {Thread.CurrentThread.ManagedThreadId} from thread {dwEventThread} +++++++++++++++++++++++++++"); try { if (_hWndFilterOrZero != IntPtr.Zero && hWnd != _hWndFilterOrZero) { return; } if (!IsSupportedWinEvent(eventType) || idObject == WinEventObjectId.OBJID_CURSOR || hWnd == IntPtr.Zero) { return; } // Moving the GetClassName call here where the main thread is running. var windowClassName = Win32Helper.GetClassName(hWnd); if (string.IsNullOrEmpty(windowClassName)) { return; } // CONSIDER: We might add some filtering here... maybe only interested in some of the window / event combinations _syncContextAuto.Post(OnWinEventReceived, new WinEventArgs(eventType, hWnd, windowClassName, idObject, idChild, dwEventThread, dwmsEventTime)); } catch (Exception ex) { Logger.WinEvents.Warn($"HandleWinEvent Exception {ex}"); } }
// Runs on the Automation thread (before syncContextAuto starts pumping) public void TryInitialize() { Debug.Print("### WindowWatcher TryInitialize on thread: " + Thread.CurrentThread.ManagedThreadId); var focusedWindowHandle = Win32Helper.GetFocusedWindowHandle(); string className = null; if (focusedWindowHandle != IntPtr.Zero) { className = Win32Helper.GetClassName(focusedWindowHandle); } UpdateFocus(focusedWindowHandle, className); }
void _windowLocationChangeHook_WinEventReceived(object sender, WinEventHook.WinEventArgs winEventArgs) { #if DEBUG Logger.WinEvents.Verbose($"{winEventArgs.EventType} - Window {winEventArgs.WindowHandle:X} ({Win32Helper.GetClassName(winEventArgs.WindowHandle)} - Object/Child {winEventArgs.ObjectId} / {winEventArgs.ChildId} - Thread {winEventArgs.EventThreadId} at {winEventArgs.EventTimeMs}"); #endif LocationChanged?.Invoke(this, EventArgs.Empty); }
// Runs on our Automation thread (via SyncContext passed into the constructor) // CONSIDER: Performance impact of logging (including GetClassName) here void OnWinEventReceived(object winEventArgsObj) { var winEventArgs = (WinEventArgs)winEventArgsObj; #if DEBUG if (winEventArgs.ObjectId != WinEventObjectId.OBJID_CURSOR) { Logger.WinEvents.Verbose($"{winEventArgs.EventType} - Window {winEventArgs.WindowHandle:X} ({Win32Helper.GetClassName(winEventArgs.WindowHandle)} - Object/Child {winEventArgs.ObjectId} / {winEventArgs.ChildId} - Thread {winEventArgs.EventThreadId} at {winEventArgs.EventTimeMs}"); } #endif WinEventReceived?.Invoke(this, winEventArgs); }
void WindowStateChange(IntPtr hWinEventHook, WinEventHook.WinEvent eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { // This runs on the main application thread. Debug.Print("### Thread receiving WindowStateChange: " + Thread.CurrentThread.ManagedThreadId); switch (Win32Helper.GetClassName(hWnd)) { case _classMain: //if (eventType == WinEventHook.WinEvent.EVENT_OBJECT_FOCUS || // eventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW) { Debug.Print("MainWindow update: " + hWnd.ToString("X") + ", EventType: " + eventType); UpdateMainWindow(hWnd); } if (eventType == WinEventHook.WinEvent.EVENT_OBJECT_DESTROY) { } break; case _classPopupList: if (PopupListWindow == IntPtr.Zero && (eventType == WinEventHook.WinEvent.EVENT_OBJECT_CREATE || eventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW)) { PopupListWindow = hWnd; PopupListWindowChanged(this, EventArgs.Empty); } else { Debug.Assert(PopupListWindow == hWnd); } break; case _classInCellEdit: Debug.Print("InCell Window update: " + hWnd.ToString("X") + ", EventType: " + eventType + ", idChild: " + idChild); if (eventType == WinEventHook.WinEvent.EVENT_OBJECT_CREATE || eventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW) { InCellEditWindow = hWnd; InCellEditWindowChanged(this, EventArgs.Empty); } else if (eventType == WinEventHook.WinEvent.EVENT_OBJECT_HIDE) { InCellEditWindow = IntPtr.Zero; InCellEditWindowChanged(this, EventArgs.Empty); } else if (eventType == WinEventHook.WinEvent.EVENT_OBJECT_FOCUS) { InCellEditFocused(this, EventArgs.Empty); } break; case _classFormulaBar: Debug.Print("FormulaBar Window update: " + hWnd.ToString("X") + ", EventType: " + eventType + ", idChild: " + idChild); if (eventType == WinEventHook.WinEvent.EVENT_OBJECT_CREATE || eventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW) { FormulaBarWindow = hWnd; FormulaBarWindowChanged(this, EventArgs.Empty); } else if (eventType == WinEventHook.WinEvent.EVENT_OBJECT_FOCUS) { FormulaBarFocused(this, EventArgs.Empty); } break; default: //InCellEditWindowChanged(this, EventArgs.Empty); break; } }
// This runs on the Automation thread, via SyncContextAuto passed in to WinEventHook when we created this WindowWatcher // CONSIDER: We would be able to run all the watcher updates from WinEvents, including Location and Selection changes, // but since WinEvents have no hwnd filter, UIAutomation events might be more efficient. // CONSIDER: Performance optimisation would keep a list of window handles we know about, preventing the class name check every time void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEventArgs e) { var className = Win32Helper.GetClassName(e.WindowHandle); if (e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_FOCUS) { // Might raise change event for Unfocus if (!UpdateFocus(e.WindowHandle, className)) { // We already have the right focus return; } } // Debug.Print("### Thread receiving WindowStateChange: " + Thread.CurrentThread.ManagedThreadId); switch (className) { //case _sheetWindowClass: // if (e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW) // { // // Maybe a new workbook is on top... // // Note that there is also an EVENT_OBJECT_PARENTCHANGE (which we are not subscribing to at the moment // } // break; case _popupListClass: PopupListWindowChanged?.Invoke(this, new WindowChangedEventArgs(e.WindowHandle, e.EventType, e.ObjectId)); break; case _inCellEditClass: InCellEditWindowChanged?.Invoke(this, new WindowChangedEventArgs(e.WindowHandle, e.EventType, e.ObjectId)); break; case _formulaBarClass: FormulaBarWindowChanged?.Invoke(this, new WindowChangedEventArgs(e.WindowHandle, e.EventType, e.ObjectId)); break; case _excelToolTipClass: ExcelToolTipWindowChanged?.Invoke(this, new WindowChangedEventArgs(e.WindowHandle, e.EventType, e.ObjectId)); break; //case _nuiDialogClass: // // Debug.Print($"SelectDataSource {_selectDataSourceClass} Window update: {e.WindowHandle:X}, EventType: {e.EventType}, idChild: {e.ChildId}"); // if (e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_CREATE) // { // // Get the name of this window - maybe ours or some other NUIDialog // var windowTitle = Win32Helper.GetText(e.WindowHandle); // if (windowTitle.Equals(_selectDataSourceTitle, StringComparison.OrdinalIgnoreCase)) // { // SelectDataSourceWindow = e.WindowHandle; // SelectDataSourceWindowChanged?.Invoke(this, // new WindowChangedEventArgs { Type = WindowChangedEventArgs.ChangeType.Create }); // } // } // else if (SelectDataSourceWindow == e.WindowHandle && e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_SHOW) // { // IsSelectDataSourceWindowVisible = true; // SelectDataSourceWindowChanged?.Invoke(this, // new WindowChangedEventArgs { Type = WindowChangedEventArgs.ChangeType.Create }); // } // else if (SelectDataSourceWindow == e.WindowHandle && e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_HIDE) // { // IsSelectDataSourceWindowVisible = false; // SelectDataSourceWindowChanged?.Invoke(this, new WindowChangedEventArgs { Type = WindowChangedEventArgs.ChangeType.Hide }); // } // else if (SelectDataSourceWindow == e.WindowHandle && e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_DESTROY) // { // IsSelectDataSourceWindowVisible = false; // SelectDataSourceWindow = IntPtr.Zero; // SelectDataSourceWindowChanged?.Invoke(this, new WindowChangedEventArgs { Type = WindowChangedEventArgs.ChangeType.Destroy }); // } // break; default: //InCellEditWindowChanged(this, EventArgs.Empty); break; } }
void _windowLocationChangeHook_WinEventReceived(object sender, WinEventHook.WinEventArgs winEventArgs) { #if DEBUG //Logger.WinEvents.Verbose(string.Format("{winEventArgs.EventType} - Window {winEventArgs.WindowHandle:X} ({Win32Helper.GetClassName(winEventArgs.WindowHandle)} - Object/Child {winEventArgs.ObjectId} / {winEventArgs.ChildId} - Thread {winEventArgs.EventThreadId} at {winEventArgs.EventTimeMs}"); Logger.WinEvents.Verbose(string.Format("{0} - Window {1:X} ({2} - Object/Child {3} / {4} - Thread {5} at {6}", winEventArgs.EventType, winEventArgs.WindowHandle, Win32Helper.GetClassName(winEventArgs.WindowHandle), winEventArgs.ObjectId, winEventArgs.ChildId, winEventArgs.EventThreadId, winEventArgs.EventTimeMs)); #endif LocationChanged.Invoke(this, EventArgs.Empty); }
// Runs on our Automation thread (via SyncContext passed into the constructor) // CONSIDER: Performance impact of logging (including GetClassName) here void OnWinEventReceived(object winEventArgsObj) { var winEventArgs = (WinEventArgs)winEventArgsObj; #if DEBUG if (winEventArgs.ObjectId != WinEventObjectId.OBJID_CURSOR) { //Logger.WinEvents.Verbose(string.Format("{winEventArgs.EventType} - Window {winEventArgs.WindowHandle:X} ({Win32Helper.GetClassName(winEventArgs.WindowHandle)} - Object/Child {winEventArgs.ObjectId} / {winEventArgs.ChildId} - Thread {winEventArgs.EventThreadId} at {winEventArgs.EventTimeMs}"); Logger.WinEvents.Verbose(string.Format("{0} - Window {1:X} ({2} - Object/Child {3} / {4} - Thread {5} at {6}", winEventArgs.EventType, winEventArgs.WindowHandle, Win32Helper.GetClassName(winEventArgs.WindowHandle), winEventArgs.ObjectId, winEventArgs.ChildId, winEventArgs.EventThreadId, winEventArgs.EventTimeMs)); } #endif WinEventReceived.Invoke(this, winEventArgs); }
// Runs on our Automation thread (via SyncContext passed into the constructor) // CONSIDER: Performance impact of logging (including GetClassName) here void OnWinEventReceived(object winEventArgsObj) { var winEventArgs = (WinEventArgs)winEventArgsObj; if (winEventArgs.ObjectId == WinEventObjectId.OBJID_CURSOR) { return; } #if DEBUG Logger.WinEvents.Verbose($"{winEventArgs.EventType} - Window {winEventArgs.WindowHandle:X} {(winEventArgs.WindowHandle != IntPtr.Zero ? Win32Helper.GetClassName(winEventArgs.WindowHandle) : "")} - Object/Child {winEventArgs.ObjectId} / {winEventArgs.ChildId} - Thread {winEventArgs.EventThreadId} at {winEventArgs.EventTimeMs}"); #endif WinEventReceived?.Invoke(this, winEventArgs); }