@@ -123,10 +123,10 @@ private static IntPtr FindNotepadEditorHandle(IntPtr notepadWindowHandle)
123
123
}
124
124
125
125
// Issue #59 - Alternate way of finding the RichEditD2DPT class:
126
- if ( FindEditorHandleThroughChildWindows ( notepadWindowHandle ) is var childRichEditHandle
127
- && childRichEditHandle != IntPtr . Zero )
126
+ if ( FindEditorHandleThroughChildWindows ( notepadWindowHandle ) is var richEditHandleFromChildren
127
+ && richEditHandleFromChildren != IntPtr . Zero )
128
128
{
129
- return childRichEditHandle ;
129
+ return richEditHandleFromChildren ;
130
130
}
131
131
132
132
return User32 . FindWindowEx ( notepadWindowHandle , IntPtr . Zero , "Edit" , null ) ;
@@ -140,6 +140,13 @@ private void EnsureNotDisposed()
140
140
}
141
141
}
142
142
143
+ private static string GetClassNameFromWindow ( IntPtr handle )
144
+ {
145
+ StringBuilder sb = new StringBuilder ( 256 ) ;
146
+ var ret = User32 . GetClassName ( handle , sb , sb . Capacity ) ;
147
+ return ret != 0 ? sb . ToString ( ) : string . Empty ;
148
+ }
149
+
143
150
private static bool EnumWindow ( IntPtr handle , IntPtr pointer )
144
151
{
145
152
GCHandle gch = GCHandle . FromIntPtr ( pointer ) ;
@@ -149,18 +156,20 @@ private static bool EnumWindow(IntPtr handle, IntPtr pointer)
149
156
throw new InvalidCastException ( "GCHandle Target could not be cast as List<IntPtr>" ) ;
150
157
}
151
158
152
- // We only want windows of class RichEditD2DPT.
153
- if ( User32 . FindWindowEx ( handle , IntPtr . Zero , "RichEditD2DPT" , null ) != IntPtr . Zero )
159
+ if ( string . Equals ( GetClassNameFromWindow ( handle ) , "RichEditD2DPT" , StringComparison . OrdinalIgnoreCase ) )
154
160
{
155
161
list . Add ( handle ) ;
162
+
163
+ // Stop enumerating - we found the one.
164
+ return false ;
156
165
}
157
166
158
167
return true ;
159
168
}
160
169
161
170
private static IntPtr FindEditorHandleThroughChildWindows ( IntPtr notepadWindowHandle )
162
171
{
163
- List < IntPtr > result = new List < IntPtr > ( ) ;
172
+ List < IntPtr > result = new List < IntPtr > ( 1 ) ;
164
173
GCHandle listHandle = GCHandle . Alloc ( result ) ;
165
174
try
166
175
{
0 commit comments