From 75db9915a53bd2d22a6e54b74e90c01ca2e26772 Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 17:39:57 +1000 Subject: [PATCH] Move FindWindow to new file --- Notepad.Extensions.Logging/NativeMethods.cs | 19 +++++++++++ Notepad.Extensions.Logging/NotepadLogger.cs | 35 +-------------------- Notepad.Extensions.Logging/WindowFinder.cs | 26 +++++++++++++++ 3 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 Notepad.Extensions.Logging/NativeMethods.cs create mode 100644 Notepad.Extensions.Logging/WindowFinder.cs diff --git a/Notepad.Extensions.Logging/NativeMethods.cs b/Notepad.Extensions.Logging/NativeMethods.cs new file mode 100644 index 0000000..6de089f --- /dev/null +++ b/Notepad.Extensions.Logging/NativeMethods.cs @@ -0,0 +1,19 @@ +using System; +using System.Runtime.InteropServices; + +namespace Microsoft.Extensions.Logging +{ + static class NativeMethods + { + [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); + + [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow); + + public const int EM_REPLACESEL = 0x00C2; + + [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); + } +} diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index 2636b13..c4e37b1 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -84,42 +84,9 @@ namespace Microsoft.Extensions.Logging static void WriteToNotepad(string message) { - IntPtr hwnd = FindNotepadWindow(); + IntPtr hwnd = WindowFinder.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 - { - [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); - - [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow); - - public const int EM_REPLACESEL = 0x00C2; - - [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); } } diff --git a/Notepad.Extensions.Logging/WindowFinder.cs b/Notepad.Extensions.Logging/WindowFinder.cs new file mode 100644 index 0000000..0027600 --- /dev/null +++ b/Notepad.Extensions.Logging/WindowFinder.cs @@ -0,0 +1,26 @@ +using System; + +namespace Microsoft.Extensions.Logging +{ + static class WindowFinder + { + public 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; + } + } +}