Skip to content

Commit 05d88a6

Browse files
committed
Add WinEvents needed to track caret under latest Excel 2016
1 parent b478de3 commit 05d88a6

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

Source/ExcelDna.IntelliSense/Providers/LoaderNotification.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ enum NtStatus : uint
7878
IntPtr _cookie;
7979
LdrNotification _notificationDelegate;
8080

81+
// NOTE: We might try to set an Environment Variable to disable the Managed Debugging Assistant
82+
// https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/diagnosing-errors-with-managed-debugging-assistants
8183
public LoaderNotification()
8284
{
8385
IntPtr context = IntPtr.Zero; // new IntPtr(12345);

Source/ExcelDna.IntelliSense/UIMonitor/FormulaEditWatcher.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ void _windowWatcher_FormulaBarWindowChanged(object sender, WindowWatcher.WindowC
9595
}
9696
else if (e.ObjectId == WindowWatcher.WindowChangedEventArgs.ChangeObjectId.Caret)
9797
{
98-
// We expect this on every text change (and it is our only detection of text changes)
98+
// We expect this on every text change
99+
// NOTE: Not anymore after some Excel / Windows update
99100
UpdateEditStateDelayed();
100101
}
101102
else
@@ -138,8 +139,22 @@ void _windowWatcher_FormulaBarWindowChanged(object sender, WindowWatcher.WindowC
138139
case WindowWatcher.WindowChangedEventArgs.ChangeType.Hide:
139140
Logger.WindowWatcher.Verbose($"FormulaEdit - FormulaBar Hide");
140141
break;
142+
case WindowWatcher.WindowChangedEventArgs.ChangeType.LocationChange:
143+
if (e.ObjectId == WindowWatcher.WindowChangedEventArgs.ChangeObjectId.Caret)
144+
{
145+
// We expect this on every text change in newer Excel versions
146+
Debug.Print($"-#-#-#- Text Changed ... ");
147+
UpdateEditStateDelayed();
148+
}
149+
else
150+
{
151+
Debug.Print($"-#-#-#- Unexpected WindowsChanged object id: {e.ObjectId}");
152+
}
153+
break;
141154
default:
142-
throw new ArgumentOutOfRangeException("Unexpected Window Change Type", "e.Type");
155+
//throw new ArgumentOutOfRangeException("Unexpected Window Change Type", "e.Type");
156+
Logger.WindowWatcher.Verbose($"FormulaEdit - Unexpected Window Change Type: {e.Type}");
157+
break;
143158
}
144159
}
145160

@@ -157,7 +172,9 @@ void _windowWatcher_InCellEditWindowChanged(object sender, WindowWatcher.WindowC
157172
}
158173
else if (e.ObjectId == WindowWatcher.WindowChangedEventArgs.ChangeObjectId.Caret)
159174
{
160-
// We expect this on every text change (and it is our only detection of text changes)
175+
// We expect this on every text change
176+
// NOTE: Not anymore after some Excel / Windows update
177+
Debug.Print($"-#-#-#- Text Changed ... ");
161178
UpdateEditStateDelayed();
162179
}
163180
else
@@ -202,8 +219,22 @@ void _windowWatcher_InCellEditWindowChanged(object sender, WindowWatcher.WindowC
202219
case WindowWatcher.WindowChangedEventArgs.ChangeType.Hide:
203220
Logger.WindowWatcher.Verbose($"FormulaEdit - InCellEdit Hide");
204221
break;
222+
case WindowWatcher.WindowChangedEventArgs.ChangeType.LocationChange:
223+
if (e.ObjectId == WindowWatcher.WindowChangedEventArgs.ChangeObjectId.Caret)
224+
{
225+
// We expect this on every text change in newer Excel versions
226+
Debug.Print($"-#-#-#- Text Changed ... ");
227+
UpdateEditStateDelayed();
228+
}
229+
else
230+
{
231+
Debug.Print($"-#-#-#- Unexpected WindowsChanged object id: {e.ObjectId}");
232+
}
233+
break;
205234
default:
206-
throw new ArgumentOutOfRangeException("Unexpected Window Change Type", "e.Type");
235+
//throw new ArgumentOutOfRangeException("Unexpected Window Change Type", "e.Type");
236+
Logger.WindowWatcher.Verbose($"FormulaEdit - Unexpected Window Change Type: {e.Type}");
237+
break;
207238
}
208239
}
209240

Source/ExcelDna.IntelliSense/UIMonitor/WinEvents.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum SetWinEventHookFlags : uint
5252

5353
public enum WinEvent : uint
5454
{
55+
EVENT_MIN = 0x00000001,
5556
EVENT_OBJECT_CREATE = 0x8000, // hwnd ID idChild is created item
5657
EVENT_OBJECT_DESTROY = 0x8001, // hwnd ID idChild is destroyed item
5758
EVENT_OBJECT_SHOW = 0x8002, // hwnd ID idChild is shown item

Source/ExcelDna.IntelliSense/UIMonitor/WindowWatcher.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,15 @@ internal WindowChangedEventArgs(IntPtr windowHandle, WinEventHook.WinEvent winEv
125125

126126
public WindowWatcher(SynchronizationContext syncContextAuto)
127127
{
128-
#pragma warning disable CS0618 // Type or member is obsolete (GetCurrentThreadId) - But for debugging we want to monitor this anyway
128+
#pragma warning disable CS0618 // Type or member is obsolete (GetCurrentThreadId) - But for debugging we want to monitor this anyway
129129
// Debug.Print($"### WindowWatcher created on thread: Managed {Thread.CurrentThread.ManagedThreadId}, Native {AppDomain.GetCurrentThreadId()}");
130-
#pragma warning restore CS0618 // Type or member is obsolete
130+
#pragma warning restore CS0618 // Type or member is obsolete
131131

132132
// Using WinEvents instead of Automation so that we can watch top-level window changes, but only from the right (current Excel) process.
133133
// TODO: We need to dramatically reduce the number of events we grab here...
134-
_windowStateChangeHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_CREATE, WinEventHook.WinEvent.EVENT_OBJECT_FOCUS, syncContextAuto, IntPtr.Zero);
135-
// _windowStateChangeHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_CREATE, WinEventHook.WinEvent.EVENT_OBJECT_END, syncContextAuto);
134+
_windowStateChangeHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_CREATE, WinEventHook.WinEvent.EVENT_OBJECT_LOCATIONCHANGE, syncContextAuto, IntPtr.Zero);
135+
// _windowStateChangeHook = new WinEventHook(WinEventHook.WinEvent.EVENT_OBJECT_CREATE, WinEventHook.WinEvent.EVENT_OBJECT_END, syncContextAuto, IntPtr.Zero);
136+
// _windowStateChangeHook = new WinEventHook(WinEventHook.WinEvent.EVENT_MIN, WinEventHook.WinEvent.EVENT_AIA_END, syncContextAuto, IntPtr.Zero);
136137

137138
_windowStateChangeHook.WinEventReceived += _windowStateChangeHook_WinEventReceived;
138139
}

0 commit comments

Comments
 (0)