@@ -123,10 +123,10 @@ private static IntPtr FindNotepadEditorHandle(IntPtr notepadWindowHandle)
123123 }
124124
125125 // 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 )
128128 {
129- return childRichEditHandle ;
129+ return richEditHandleFromChildren ;
130130 }
131131
132132 return User32 . FindWindowEx ( notepadWindowHandle , IntPtr . Zero , "Edit" , null ) ;
@@ -140,6 +140,13 @@ private void EnsureNotDisposed()
140140 }
141141 }
142142
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+
143150 private static bool EnumWindow ( IntPtr handle , IntPtr pointer )
144151 {
145152 GCHandle gch = GCHandle . FromIntPtr ( pointer ) ;
@@ -149,18 +156,20 @@ private static bool EnumWindow(IntPtr handle, IntPtr pointer)
149156 throw new InvalidCastException ( "GCHandle Target could not be cast as List<IntPtr>" ) ;
150157 }
151158
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 ) )
154160 {
155161 list . Add ( handle ) ;
162+
163+ // Stop enumerating - we found the one.
164+ return false ;
156165 }
157166
158167 return true ;
159168 }
160169
161170 private static IntPtr FindEditorHandleThroughChildWindows ( IntPtr notepadWindowHandle )
162171 {
163- List < IntPtr > result = new List < IntPtr > ( ) ;
172+ List < IntPtr > result = new List < IntPtr > ( 1 ) ;
164173 GCHandle listHandle = GCHandle . Alloc ( result ) ;
165174 try
166175 {
0 commit comments