From 2d7131997493a3f9a05876364ec80cd663ca37c5 Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 17:38:01 +1000 Subject: [PATCH] Also search for Notepad windows with changes. Fixes #3. --- Notepad.Extensions.Logging/NotepadLogger.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index f365058..2636b13 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -84,10 +84,29 @@ namespace Microsoft.Extensions.Logging static void WriteToNotepad(string message) { - IntPtr hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad"); + IntPtr hwnd = FindNotepadWindow(); IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null); NativeMethods.SendMessage(edit, NativeMethods.EM_REPLACESEL, (IntPtr)1, message); } + + static IntPtr FindNotepadWindow() + { + IntPtr hwnd; + + hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad"); + if (hwnd != IntPtr.Zero) + { + return hwnd; + } + + hwnd = NativeMethods.FindWindow(null, "*Untitled - Notepad"); + if (hwnd != IntPtr.Zero) + { + return hwnd; + } + + return IntPtr.Zero; + } } static class NativeMethods