Move FindWindow to new file

This commit is contained in:
Yaakov 2020-05-21 17:39:57 +10:00
parent 70531ac442
commit 75db9915a5
3 changed files with 46 additions and 34 deletions

View file

@ -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);
}
}

View file

@ -84,42 +84,9 @@ namespace Microsoft.Extensions.Logging
static void WriteToNotepad(string message) static void WriteToNotepad(string message)
{ {
IntPtr hwnd = FindNotepadWindow(); IntPtr hwnd = WindowFinder.FindNotepadWindow();
IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null); IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);
NativeMethods.SendMessage(edit, NativeMethods.EM_REPLACESEL, (IntPtr)1, message); 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);
} }
} }

View file

@ -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;
}
}
}