Skip to content

Commit

Permalink
PR rebase & minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoproiete committed Jun 4, 2023
1 parent 6db95e1 commit 7ab2698
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions sample/ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ private static void Main(string[] args)

try
{
//Console.WriteLine("Open a `notepad.exe` instance and press <enter> to continue...");
//Console.ReadLine();
Console.WriteLine("Open a `notepad.exe` instance and press <enter> to continue...");
Console.ReadLine();

Console.WriteLine("Writing messages to the most recent Notepad you opened...");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public override void Flush()
}

// Get how many characters are in the Notepad editor already
var textLength = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
var textLengthBefore = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);

// Set the caret position to the end of the text
User32.SendMessage(_currentNotepadEditorHandle, User32.EM_SETSEL, (IntPtr)textLength, (IntPtr)textLength);
User32.SendMessage(_currentNotepadEditorHandle, User32.EM_SETSEL, (IntPtr)textLengthBefore, (IntPtr)textLengthBefore);

var buffer = base.GetStringBuilder();
var message = buffer.ToString();
Expand All @@ -88,13 +88,17 @@ public override void Flush()
// Get how many characters are in the Notepad editor after putting in new text
var textLengthAfter = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);

// If no change in textLength, reset editor handle to try to find it again.
if (textLengthAfter == textLength)
// If no change in the text length, reset editor handle to try to find it again.
if (textLengthAfter == textLengthBefore)
{
_currentNotepadEditorHandle = IntPtr.Zero;

// Otherwise, we clear the buffer
}
else
{
// Otherwise, we clear the buffer

buffer.Clear();
}
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -161,9 +165,10 @@ private static string GetClassNameFromWindow(IntPtr handle)

private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
var gch = GCHandle.FromIntPtr(pointer);
var list = gch.Target as List<IntPtr>;

if (list is null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
Expand All @@ -181,8 +186,9 @@ private static bool EnumWindow(IntPtr handle, IntPtr pointer)

private static IntPtr FindEditorHandleThroughChildWindows(IntPtr notepadWindowHandle)
{
List<IntPtr> result = new List<IntPtr>(1);
GCHandle listHandle = GCHandle.Alloc(result);
var result = new List<IntPtr>(1);
var listHandle = GCHandle.Alloc(result);

try
{
User32.Win32Callback childProc = new User32.Win32Callback(EnumWindow);
Expand All @@ -191,8 +197,11 @@ private static IntPtr FindEditorHandleThroughChildWindows(IntPtr notepadWindowHa
finally
{
if (listHandle.IsAllocated)
{
listHandle.Free();
}
}

return result.FirstOrDefault();
}
}
Expand Down

0 comments on commit 7ab2698

Please sign in to comment.