@@ -117,7 +117,11 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv
117117 const string _nuiDialogClass = "NUIDialog" ;
118118 const string _selectDataSourceTitle = "Select Data Source" ; // TODO: How does localization work?
119119
120+ private readonly SynchronizationContext _syncContextAuto ;
121+ private readonly SynchronizationContext _syncContextMain ;
122+
120123 List < WinEventHook > _windowStateChangeHooks = new List < WinEventHook > ( ) ;
124+ private WinEventHook _locationChangeEventHook ;
121125
122126 // These track keyboard focus for Windows in the Excel process
123127 // Used to synthesize the 'Unfocus' change events
@@ -138,6 +142,9 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv
138142
139143 public WindowWatcher ( SynchronizationContext syncContextAuto , SynchronizationContext syncContextMain )
140144 {
145+ _syncContextAuto = syncContextAuto ;
146+ _syncContextMain = syncContextMain ;
147+
141148#pragma warning disable CS0618 // Type or member is obsolete (GetCurrentThreadId) - But for debugging we want to monitor this anyway
142149 // Debug.Print($"### WindowWatcher created on thread: Managed {Thread.CurrentThread.ManagedThreadId}, Native {AppDomain.GetCurrentThreadId()}");
143150#pragma warning restore CS0618 // Type or member is obsolete
@@ -164,6 +171,14 @@ public WindowWatcher(SynchronizationContext syncContextAuto, SynchronizationCont
164171 {
165172 windowStateChangeHook . WinEventReceived += _windowStateChangeHook_WinEventReceived ;
166173 }
174+
175+ SetUpLocationChangeEventListener ( ) ;
176+ }
177+
178+ private void SetUpLocationChangeEventListener ( )
179+ {
180+ _locationChangeEventHook = new WinEventHook ( WinEventHook . WinEvent . EVENT_OBJECT_LOCATIONCHANGE , WinEventHook . WinEvent . EVENT_OBJECT_LOCATIONCHANGE , _syncContextAuto , _syncContextMain , IntPtr . Zero ) ;
181+ _locationChangeEventHook . WinEventReceived += _windowStateChangeHook_WinEventReceived ;
167182 }
168183
169184 // Runs on the Automation thread (before syncContextAuto starts pumping)
@@ -224,6 +239,17 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven
224239 {
225240 Debug . Fail ( "WinEvent with window 0!?" ) ;
226241 }
242+
243+ if ( e . EventType == WinEventHook . WinEvent . EVENT_SYSTEM_MOVESIZESTART )
244+ {
245+ _syncContextMain . Post ( _ => _locationChangeEventHook . Dispose ( ) , null ) ;
246+ }
247+
248+ if ( e . EventType == WinEventHook . WinEvent . EVENT_SYSTEM_MOVESIZEEND )
249+ {
250+ _syncContextMain . Post ( _ => SetUpLocationChangeEventListener ( ) , null ) ;
251+ }
252+
227253 if ( e . EventType == WinEventHook . WinEvent . EVENT_OBJECT_FOCUS )
228254 {
229255 // Might raise change event for Unfocus
@@ -322,6 +348,7 @@ public void Dispose()
322348 }
323349
324350 _windowStateChangeHooks = new List < WinEventHook > ( ) ;
351+ _locationChangeEventHook . Dispose ( ) ;
325352 }
326353 }
327354
0 commit comments