@@ -117,7 +117,11 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv
117
117
const string _nuiDialogClass = "NUIDialog" ;
118
118
const string _selectDataSourceTitle = "Select Data Source" ; // TODO: How does localization work?
119
119
120
+ private readonly SynchronizationContext _syncContextAuto ;
121
+ private readonly SynchronizationContext _syncContextMain ;
122
+
120
123
List < WinEventHook > _windowStateChangeHooks = new List < WinEventHook > ( ) ;
124
+ private WinEventHook _locationChangeEventHook ;
121
125
122
126
// These track keyboard focus for Windows in the Excel process
123
127
// Used to synthesize the 'Unfocus' change events
@@ -138,6 +142,9 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv
138
142
139
143
public WindowWatcher ( SynchronizationContext syncContextAuto , SynchronizationContext syncContextMain )
140
144
{
145
+ _syncContextAuto = syncContextAuto ;
146
+ _syncContextMain = syncContextMain ;
147
+
141
148
#pragma warning disable CS0618 // Type or member is obsolete (GetCurrentThreadId) - But for debugging we want to monitor this anyway
142
149
// Debug.Print($"### WindowWatcher created on thread: Managed {Thread.CurrentThread.ManagedThreadId}, Native {AppDomain.GetCurrentThreadId()}");
143
150
#pragma warning restore CS0618 // Type or member is obsolete
@@ -164,6 +171,14 @@ public WindowWatcher(SynchronizationContext syncContextAuto, SynchronizationCont
164
171
{
165
172
windowStateChangeHook . WinEventReceived += _windowStateChangeHook_WinEventReceived ;
166
173
}
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 ;
167
182
}
168
183
169
184
// Runs on the Automation thread (before syncContextAuto starts pumping)
@@ -224,6 +239,17 @@ void _windowStateChangeHook_WinEventReceived(object sender, WinEventHook.WinEven
224
239
{
225
240
Debug . Fail ( "WinEvent with window 0!?" ) ;
226
241
}
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
+
227
253
if ( e . EventType == WinEventHook . WinEvent . EVENT_OBJECT_FOCUS )
228
254
{
229
255
// Might raise change event for Unfocus
@@ -322,6 +348,7 @@ public void Dispose()
322
348
}
323
349
324
350
_windowStateChangeHooks = new List < WinEventHook > ( ) ;
351
+ _locationChangeEventHook . Dispose ( ) ;
325
352
}
326
353
}
327
354
0 commit comments