@@ -74,10 +74,10 @@ public override void Flush()
74
74
}
75
75
76
76
// Get how many characters are in the Notepad editor already
77
- var textLength = User32 . SendMessage ( _currentNotepadEditorHandle , User32 . WM_GETTEXTLENGTH , IntPtr . Zero , IntPtr . Zero ) ;
77
+ var textLengthBefore = User32 . SendMessage ( _currentNotepadEditorHandle , User32 . WM_GETTEXTLENGTH , IntPtr . Zero , IntPtr . Zero ) ;
78
78
79
79
// Set the caret position to the end of the text
80
- User32 . SendMessage ( _currentNotepadEditorHandle , User32 . EM_SETSEL , ( IntPtr ) textLength , ( IntPtr ) textLength ) ;
80
+ User32 . SendMessage ( _currentNotepadEditorHandle , User32 . EM_SETSEL , ( IntPtr ) textLengthBefore , ( IntPtr ) textLengthBefore ) ;
81
81
82
82
var buffer = base . GetStringBuilder ( ) ;
83
83
var message = buffer . ToString ( ) ;
@@ -88,13 +88,17 @@ public override void Flush()
88
88
// Get how many characters are in the Notepad editor after putting in new text
89
89
var textLengthAfter = User32 . SendMessage ( _currentNotepadEditorHandle , User32 . WM_GETTEXTLENGTH , IntPtr . Zero , IntPtr . Zero ) ;
90
90
91
- // If no change in textLength, reset editor handle to try to find it again.
92
- if ( textLengthAfter == textLength )
91
+ // If no change in the text length, reset editor handle to try to find it again.
92
+ if ( textLengthAfter == textLengthBefore )
93
+ {
93
94
_currentNotepadEditorHandle = IntPtr . Zero ;
94
-
95
- // Otherwise, we clear the buffer
95
+ }
96
96
else
97
+ {
98
+ // Otherwise, we clear the buffer
99
+
97
100
buffer . Clear ( ) ;
101
+ }
98
102
}
99
103
100
104
protected override void Dispose ( bool disposing )
@@ -161,9 +165,10 @@ private static string GetClassNameFromWindow(IntPtr handle)
161
165
162
166
private static bool EnumWindow ( IntPtr handle , IntPtr pointer )
163
167
{
164
- GCHandle gch = GCHandle . FromIntPtr ( pointer ) ;
165
- List < IntPtr > list = gch . Target as List < IntPtr > ;
166
- if ( list == null )
168
+ var gch = GCHandle . FromIntPtr ( pointer ) ;
169
+ var list = gch . Target as List < IntPtr > ;
170
+
171
+ if ( list is null )
167
172
{
168
173
throw new InvalidCastException ( "GCHandle Target could not be cast as List<IntPtr>" ) ;
169
174
}
@@ -181,8 +186,9 @@ private static bool EnumWindow(IntPtr handle, IntPtr pointer)
181
186
182
187
private static IntPtr FindEditorHandleThroughChildWindows ( IntPtr notepadWindowHandle )
183
188
{
184
- List < IntPtr > result = new List < IntPtr > ( 1 ) ;
185
- GCHandle listHandle = GCHandle . Alloc ( result ) ;
189
+ var result = new List < IntPtr > ( 1 ) ;
190
+ var listHandle = GCHandle . Alloc ( result ) ;
191
+
186
192
try
187
193
{
188
194
User32 . Win32Callback childProc = new User32 . Win32Callback ( EnumWindow ) ;
@@ -191,8 +197,11 @@ private static IntPtr FindEditorHandleThroughChildWindows(IntPtr notepadWindowHa
191
197
finally
192
198
{
193
199
if ( listHandle . IsAllocated )
200
+ {
194
201
listHandle . Free ( ) ;
202
+ }
195
203
}
204
+
196
205
return result . FirstOrDefault ( ) ;
197
206
}
198
207
}
0 commit comments