From 4c1b10dc0577aaf7ec75d1ac1a21e0e8d2887a63 Mon Sep 17 00:00:00 2001 From: Sardor Shukurov Date: Thu, 6 Jul 2023 12:45:21 +0500 Subject: [PATCH 1/6] #124 Allow processing LOCATIONCHANGE events --- Source/ExcelDna.IntelliSense/UIMonitor/WinEvents.cs | 1 + Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/Source/ExcelDna.IntelliSense/UIMonitor/WinEvents.cs b/Source/ExcelDna.IntelliSense/UIMonitor/WinEvents.cs index 24b1652..bc1b6e6 100644 --- a/Source/ExcelDna.IntelliSense/UIMonitor/WinEvents.cs +++ b/Source/ExcelDna.IntelliSense/UIMonitor/WinEvents.cs @@ -219,6 +219,7 @@ bool IsSupportedWinEvent(WinEvent winEvent) winEvent == WinEvent.EVENT_SYSTEM_MOVESIZESTART || // Only for the on-demand hook winEvent == WinEvent.EVENT_SYSTEM_MOVESIZEEND || // Only for the on-demand hook winEvent == WinEvent.EVENT_OBJECT_SELECTION || // Only for the PopupList + winEvent == WinEvent.EVENT_OBJECT_LOCATIONCHANGE || winEvent == WinEvent.EVENT_SYSTEM_CAPTURESTART; } diff --git a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs index a8bfca7..b3695f7 100644 --- a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs +++ b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs @@ -69,6 +69,10 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv case WinEventHook.WinEvent.EVENT_OBJECT_FOCUS: Type = ChangeType.Focus; break; + case WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE: + Type = ChangeType.LocationChange; + ObjectId = ChangeObjectId.Caret; + break; case WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND: Type = ChangeType.LocationChange; break; From 4ca7b99cfabb986ac2703cd62a7e7c2fbed908f5 Mon Sep 17 00:00:00 2001 From: Sardor Shukurov Date: Thu, 6 Jul 2023 12:46:25 +0500 Subject: [PATCH 2/6] #124 Unhook from LOCATIONCHANGE upon MOVESIZESTART and hook again upon MOVESIZEEND --- .../UIMonitor/WindowWatcher.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs index b3695f7..cdee635 100644 --- a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs +++ b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs @@ -117,7 +117,11 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv const string _nuiDialogClass = "NUIDialog"; const string _selectDataSourceTitle = "Select Data Source"; // TODO: How does localization work? + private readonly SynchronizationContext _syncContextAuto; + private readonly SynchronizationContext _syncContextMain; + List _windowStateChangeHooks = new List(); + private WinEventHook _locationChangeEventHook; // These track keyboard focus for Windows in the Excel process // Used to synthesize the 'Unfocus' change events @@ -138,6 +142,9 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv public WindowWatcher(SynchronizationContext syncContextAuto, SynchronizationContext syncContextMain) { + _syncContextAuto = syncContextAuto; + _syncContextMain = syncContextMain; + #pragma warning disable CS0618 // Type or member is obsolete (GetCurrentThreadId) - But for debugging we want to monitor this anyway // Debug.Print($"### WindowWatcher created on thread: Managed {Thread.CurrentThread.ManagedThreadId}, Native {AppDomain.GetCurrentThreadId()}"); #pragma warning restore CS0618 // Type or member is obsolete @@ -164,6 +171,14 @@ public WindowWatcher(SynchronizationContext syncContextAuto, SynchronizationCont { windowStateChangeHook.WinEventReceived += _windowStateChangeHook_WinEventReceived; } + + SetUpLocationChangeEventListener(); + } + + private void SetUpLocationChangeEventListener() + { + _locationChangeEventHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, _syncContextAuto, _syncContextMain, IntPtr.Zero); + _locationChangeEventHook.WinEventReceived += _windowStateChangeHook_WinEventReceived; } // Runs on the Automation thread (before syncContextAuto starts pumping) @@ -224,6 +239,17 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven { Debug.Fail("WinEvent with window 0!?"); } + + if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZESTART) + { + _syncContextMain.Post(_ => _locationChangeEventHook.Dispose(), null); + } + + if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND) + { + _syncContextMain.Post(_ => SetUpLocationChangeEventListener(), null); + } + if (e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_FOCUS) { // Might raise change event for Unfocus @@ -322,6 +348,7 @@ public void Dispose() } _windowStateChangeHooks = new List(); + _locationChangeEventHook.Dispose(); } } From 2f22197657773a2b4bd75ec8681e2c0aff52e6c9 Mon Sep 17 00:00:00 2001 From: Sardor Shukurov Date: Thu, 6 Jul 2023 12:46:48 +0500 Subject: [PATCH 3/6] #124 Clean up --- .../UIMonitor/WindowWatcher.cs | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs index cdee635..edaa55a 100644 --- a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs +++ b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs @@ -117,11 +117,11 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv const string _nuiDialogClass = "NUIDialog"; const string _selectDataSourceTitle = "Select Data Source"; // TODO: How does localization work? - private readonly SynchronizationContext _syncContextAuto; - private readonly SynchronizationContext _syncContextMain; + readonly SynchronizationContext _syncContextAuto; + readonly SynchronizationContext _syncContextMain; List _windowStateChangeHooks = new List(); - private WinEventHook _locationChangeEventHook; + WinEventHook _locationChangeEventHook; // These track keyboard focus for Windows in the Excel process // Used to synthesize the 'Unfocus' change events @@ -162,8 +162,6 @@ public WindowWatcher(SynchronizationContext syncContextAuto, SynchronizationCont // EVENT_OBJECT_SELECTIONREMOVE // EVENT_OBJECT_SELECTIONWITHIN // EVENT_OBJECT_STATECHANGE (0x800A = 32778) - // NB: Including the next event 'EVENT_OBJECT_LOCATIONCHANGE (0x800B = 32779)' will cause the Excel main window to lag when dragging. - // This drag issue seems to have been introduced with an Office update around November 2022. _windowStateChangeHooks.Add(new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_CREATE, WinEventHook.WinEvent.EVENT_OBJECT_STATECHANGE, syncContextAuto, syncContextMain, IntPtr.Zero)); _windowStateChangeHooks.Add(new WinEventHook(WinEventHook.WinEvent.EVENT_SYSTEM_CAPTURESTART, WinEventHook.WinEvent.EVENT_SYSTEM_CAPTURESTART, syncContextAuto, syncContextMain, IntPtr.Zero)); @@ -175,8 +173,12 @@ public WindowWatcher(SynchronizationContext syncContextAuto, SynchronizationCont SetUpLocationChangeEventListener(); } - private void SetUpLocationChangeEventListener() + void SetUpLocationChangeEventListener() { + // NB: Including the next event 'EVENT_OBJECT_LOCATIONCHANGE (0x800B = 32779)' will cause the Excel main window to lag when dragging. + // This drag issue seems to have been introduced with an Office update around November 2022. + // To workaround this, we unhook from this event upon encountering EVENT_SYSTEM_MOVESIZESTART and then hook again upon encountering + // EVENT_SYSTEM_MOVESIZEEND (see UnhookFromLocationChangeUponDraggingExcelMainWindow). _locationChangeEventHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, _syncContextAuto, _syncContextMain, IntPtr.Zero); _locationChangeEventHook.WinEventReceived += _windowStateChangeHook_WinEventReceived; } @@ -228,6 +230,22 @@ bool UpdateFocus(IntPtr windowHandle, string windowClassName) return true; } + // This allows us to temporarily stop listening to EVENT_OBJECT_LOCATIONCHANGE events when the user is dragging the Excel main window. + // Otherwise we are going to bump into https://github.com/Excel-DNA/IntelliSense/issues/123. The rest of the time we need to stay + // hooked to EVENT_OBJECT_LOCATIONCHANGE for IntelliSense to work correctly (see https://github.com/Excel-DNA/IntelliSense/issues/124). + void UnhookFromLocationChangeUponDraggingExcelMainWindow(WinEventHook.WinEventArgs e) + { + if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZESTART) + { + _syncContextMain.Post(_ => _locationChangeEventHook?.Dispose(), null); + } + + if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND) + { + _syncContextMain.Post(_ => SetUpLocationChangeEventListener(), null); + } + } + // 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. @@ -240,15 +258,7 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven Debug.Fail("WinEvent with window 0!?"); } - if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZESTART) - { - _syncContextMain.Post(_ => _locationChangeEventHook.Dispose(), null); - } - - if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND) - { - _syncContextMain.Post(_ => SetUpLocationChangeEventListener(), null); - } + UnhookFromLocationChangeUponDraggingExcelMainWindow(e); if (e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_FOCUS) { From aff16e4513e2eba18f4f0ca25c94252e4a2d4bdf Mon Sep 17 00:00:00 2001 From: Sardor Shukurov Date: Thu, 6 Jul 2023 12:47:31 +0500 Subject: [PATCH 4/6] #124 Bump IntelliSenseServer version --- Source/ExcelDna.IntelliSense/IntelliSenseServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/ExcelDna.IntelliSense/IntelliSenseServer.cs b/Source/ExcelDna.IntelliSense/IntelliSenseServer.cs index ede2a91..9a15549 100644 --- a/Source/ExcelDna.IntelliSense/IntelliSenseServer.cs +++ b/Source/ExcelDna.IntelliSense/IntelliSenseServer.cs @@ -30,7 +30,7 @@ namespace ExcelDna.IntelliSense // REMEMBER: COM events are not necessarily safe macro contexts. public static class IntelliSenseServer { - const string ServerVersion = "1.7.0"; // TODO: Define and manage this somewhere else + const string ServerVersion = "1.7.1"; // TODO: Define and manage this somewhere else // NOTE: Do not change these constants in custom versions. // They are part of the co-operative safety mechanism allowing different add-ins providing IntelliSense to work together safely. From 78aa1488691e48366369acdfea0f7b8cf56df6b9 Mon Sep 17 00:00:00 2001 From: Sardor Shukurov Date: Thu, 6 Jul 2023 21:53:44 +0500 Subject: [PATCH 5/6] #124 Move location change hook registration to WindowLocationWatcher --- .../UIMonitor/WindowLocationWatcher.cs | 52 ++++++++++++++++--- .../UIMonitor/WindowWatcher.cs | 32 ------------ 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs b/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs index b5b7907..841c052 100644 --- a/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs +++ b/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs @@ -9,7 +9,8 @@ public class WindowLocationWatcher : IDisposable IntPtr _hWnd; SynchronizationContext _syncContextAuto; SynchronizationContext _syncContextMain; - WinEventHook _windowLocationChangeHook; + WinEventHook _windowMoveSizeHook; + WinEventHook _locationChangeEventHook; public event EventHandler LocationChanged; @@ -22,15 +23,46 @@ public WindowLocationWatcher(IntPtr hWnd, SynchronizationContext syncContextAuto _hWnd = hWnd; _syncContextAuto = syncContextAuto; _syncContextMain = syncContextMain; - _windowLocationChangeHook = new WinEventHook(WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZESTART, WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND, _syncContextAuto, syncContextMain, _hWnd); - _windowLocationChangeHook.WinEventReceived += _windowLocationChangeHook_WinEventReceived; + _windowMoveSizeHook = new WinEventHook(WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZESTART, WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND, _syncContextAuto, syncContextMain, _hWnd); + _windowMoveSizeHook.WinEventReceived += _windowMoveSizeHook_WinEventReceived; + + SetUpLocationChangeEventListener(); } - void _windowLocationChangeHook_WinEventReceived(object sender, WinEventHook.WinEventArgs winEventArgs) + void SetUpLocationChangeEventListener() + { + // NB: Including the next event 'EVENT_OBJECT_LOCATIONCHANGE (0x800B = 32779)' will cause the Excel main window to lag when dragging. + // This drag issue seems to have been introduced with an Office update around November 2022. + // To workaround this, we unhook from this event upon encountering EVENT_SYSTEM_MOVESIZESTART and then hook again upon encountering + // EVENT_SYSTEM_MOVESIZEEND (see UnhookFromLocationChangeUponDraggingExcelMainWindow). + _locationChangeEventHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, _syncContextAuto, _syncContextMain, IntPtr.Zero); + _locationChangeEventHook.WinEventReceived += _windowMoveSizeHook_WinEventReceived; + } + + // This allows us to temporarily stop listening to EVENT_OBJECT_LOCATIONCHANGE events when the user is dragging the Excel main window. + // Otherwise we are going to bump into https://github.com/Excel-DNA/IntelliSense/issues/123. The rest of the time we need to stay + // hooked to EVENT_OBJECT_LOCATIONCHANGE for IntelliSense to work correctly (see https://github.com/Excel-DNA/IntelliSense/issues/124). + void UnhookFromLocationChangeUponDraggingExcelMainWindow(WinEventHook.WinEventArgs e) + { + if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZESTART) + { + _syncContextMain.Post(_ => _locationChangeEventHook?.Dispose(), null); + } + + if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND) + { + _syncContextMain.Post(_ => SetUpLocationChangeEventListener(), null); + } + } + + void _windowMoveSizeHook_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 + + UnhookFromLocationChangeUponDraggingExcelMainWindow(winEventArgs); + LocationChanged?.Invoke(this, EventArgs.Empty); } @@ -38,10 +70,16 @@ void _windowLocationChangeHook_WinEventReceived(object sender, WinEventHook.WinE public void Dispose() { Debug.Assert(Thread.CurrentThread.ManagedThreadId == 1); - if (_windowLocationChangeHook != null) + if (_windowMoveSizeHook != null) + { + _windowMoveSizeHook.Dispose(); + _windowMoveSizeHook = null; + } + + if (_locationChangeEventHook != null) { - _windowLocationChangeHook.Dispose(); - _windowLocationChangeHook = null; + _locationChangeEventHook.Dispose(); + _locationChangeEventHook = null; } } } diff --git a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs index edaa55a..8388ba1 100644 --- a/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs +++ b/Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs @@ -121,7 +121,6 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv readonly SynchronizationContext _syncContextMain; List _windowStateChangeHooks = new List(); - WinEventHook _locationChangeEventHook; // These track keyboard focus for Windows in the Excel process // Used to synthesize the 'Unfocus' change events @@ -169,18 +168,6 @@ public WindowWatcher(SynchronizationContext syncContextAuto, SynchronizationCont { windowStateChangeHook.WinEventReceived += _windowStateChangeHook_WinEventReceived; } - - SetUpLocationChangeEventListener(); - } - - void SetUpLocationChangeEventListener() - { - // NB: Including the next event 'EVENT_OBJECT_LOCATIONCHANGE (0x800B = 32779)' will cause the Excel main window to lag when dragging. - // This drag issue seems to have been introduced with an Office update around November 2022. - // To workaround this, we unhook from this event upon encountering EVENT_SYSTEM_MOVESIZESTART and then hook again upon encountering - // EVENT_SYSTEM_MOVESIZEEND (see UnhookFromLocationChangeUponDraggingExcelMainWindow). - _locationChangeEventHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, _syncContextAuto, _syncContextMain, IntPtr.Zero); - _locationChangeEventHook.WinEventReceived += _windowStateChangeHook_WinEventReceived; } // Runs on the Automation thread (before syncContextAuto starts pumping) @@ -230,22 +217,6 @@ bool UpdateFocus(IntPtr windowHandle, string windowClassName) return true; } - // This allows us to temporarily stop listening to EVENT_OBJECT_LOCATIONCHANGE events when the user is dragging the Excel main window. - // Otherwise we are going to bump into https://github.com/Excel-DNA/IntelliSense/issues/123. The rest of the time we need to stay - // hooked to EVENT_OBJECT_LOCATIONCHANGE for IntelliSense to work correctly (see https://github.com/Excel-DNA/IntelliSense/issues/124). - void UnhookFromLocationChangeUponDraggingExcelMainWindow(WinEventHook.WinEventArgs e) - { - if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZESTART) - { - _syncContextMain.Post(_ => _locationChangeEventHook?.Dispose(), null); - } - - if (e.EventType == WinEventHook.WinEvent.EVENT_SYSTEM_MOVESIZEEND) - { - _syncContextMain.Post(_ => SetUpLocationChangeEventListener(), null); - } - } - // 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. @@ -258,8 +229,6 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven Debug.Fail("WinEvent with window 0!?"); } - UnhookFromLocationChangeUponDraggingExcelMainWindow(e); - if (e.EventType == WinEventHook.WinEvent.EVENT_OBJECT_FOCUS) { // Might raise change event for Unfocus @@ -358,7 +327,6 @@ public void Dispose() } _windowStateChangeHooks = new List(); - _locationChangeEventHook.Dispose(); } } From 69dd6f1ddce39d47386ee6d59877336087421bf0 Mon Sep 17 00:00:00 2001 From: Sardor Shukurov Date: Thu, 6 Jul 2023 21:54:05 +0500 Subject: [PATCH 6/6] #124 Update comments --- .../UIMonitor/WindowLocationWatcher.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs b/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs index 841c052..cdd3e3a 100644 --- a/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs +++ b/Source/ExcelDna.IntelliSense/UIMonitor/WindowLocationWatcher.cs @@ -15,9 +15,16 @@ public class WindowLocationWatcher : IDisposable public event EventHandler LocationChanged; // NOTE: An earlier attempt was to monitor LOCATIONCHANGE only between EVENT_SYSTEM_MOVESIZESTART and EVENT_SYSTEM_MOVESIZEEND + // (for the purpose of moving the tooltip to the correct position when the user moves the Excel main window) // This nearly worked, and meant we were watching many fewer events ... // ...but we missed some of the resizing events for the window, leaving our tooltip stranded. - // So until we can find a workaround for that (perhaps a timer would work fine for this), we watch all the LOCATIONCHANGE events. + // We then started to watch all the LOCATIONCHANGE events, but it caused the Excel main window to lag when dragging. + // (This drag issue seems to have been introduced with an Office update around November 2022) + // So until we can find a workaround for that (perhaps a timer would work fine for this), we decided not to bother + // with tracking the tooltip position (we still update it as soon as the Excel main window moving ends). + // We still need to watch the LOCATIONCHANGE events, otherwise the tooltip is not shown at all in some cases. + // To workaround the Excel main window lagging, we unhook from LOCATIONCHANGE upon encountering EVENT_SYSTEM_MOVESIZESTART + // and then hook again upon encountering EVENT_SYSTEM_MOVESIZEEND (see UnhookFromLocationChangeUponDraggingExcelMainWindow). public WindowLocationWatcher(IntPtr hWnd, SynchronizationContext syncContextAuto, SynchronizationContext syncContextMain) { _hWnd = hWnd; @@ -31,10 +38,7 @@ public WindowLocationWatcher(IntPtr hWnd, SynchronizationContext syncContextAuto void SetUpLocationChangeEventListener() { - // NB: Including the next event 'EVENT_OBJECT_LOCATIONCHANGE (0x800B = 32779)' will cause the Excel main window to lag when dragging. - // This drag issue seems to have been introduced with an Office update around November 2022. - // To workaround this, we unhook from this event upon encountering EVENT_SYSTEM_MOVESIZESTART and then hook again upon encountering - // EVENT_SYSTEM_MOVESIZEEND (see UnhookFromLocationChangeUponDraggingExcelMainWindow). + _locationChangeEventHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, _syncContextAuto, _syncContextMain, IntPtr.Zero); _locationChangeEventHook.WinEventReceived += _windowMoveSizeHook_WinEventReceived; }