mirror of
https://github.com/yaakov-h/Notepad.Extensions.Logging.git
synced 2024-11-22 11:14:50 +00:00
Move FindWindow to new file
This commit is contained in:
parent
70531ac442
commit
75db9915a5
3 changed files with 46 additions and 34 deletions
19
Notepad.Extensions.Logging/NativeMethods.cs
Normal file
19
Notepad.Extensions.Logging/NativeMethods.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
26
Notepad.Extensions.Logging/WindowFinder.cs
Normal file
26
Notepad.Extensions.Logging/WindowFinder.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue