Added support of window name from options in WindowEnumerationState class.

This commit is contained in:
Alexander Kislitsyn 2020-05-23 13:00:55 +03:00
parent 6926e2a858
commit d2e66538e0
5 changed files with 48 additions and 17 deletions

View file

@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging.Configuration; using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Notepad.Extensions.Logging;
using System; using System;
namespace Microsoft.Extensions.Logging namespace Microsoft.Extensions.Logging

View file

@ -96,14 +96,11 @@ namespace Notepad.Extensions.Logging
case WindowKind.Notepad: case WindowKind.Notepad:
SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message); SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message);
break; break;
case WindowKind.NotepadPlusPlus: case WindowKind.NotepadPlusPlus:
{
WriteToNotepadPlusPlus(info.Handle, message); WriteToNotepadPlusPlus(info.Handle, message);
break; break;
} }
} }
}
unsafe static void WriteToNotepadPlusPlus(IntPtr hwnd, string message) unsafe static void WriteToNotepadPlusPlus(IntPtr hwnd, string message)
{ {

View file

@ -42,6 +42,10 @@ namespace Notepad.Extensions.Logging
private void ReloadLoggerOptions(NotepadLoggerOptions options) private void ReloadLoggerOptions(NotepadLoggerOptions options)
{ {
this.options = options; this.options = options;
if (windowFinder is WindowFinder finder)
{
finder.WindowName = this.options.WindowName;
}
} }
} }
} }

View file

@ -16,11 +16,13 @@ namespace Notepad.Extensions.Logging
public IntPtr Handle { get; private set; } public IntPtr Handle { get; private set; }
public WindowKind WindowKind { get; private set; } public WindowKind WindowKind { get; private set; }
public string WindowName { get; internal set; }
public void Reset() public void Reset()
{ {
Handle = default; Handle = default;
WindowKind = default; WindowKind = default;
WindowName = default;
sb.Clear(); sb.Clear();
} }
@ -47,26 +49,50 @@ namespace Notepad.Extensions.Logging
return true; return true;
} }
static Regex notepadPlusPlusRegex = new Regex(@"^new \d+ - Notepad\+\+$", RegexOptions.Compiled); static Regex notepadPlusPlusRegex = new Regex(@" - Notepad\+\+$", RegexOptions.Compiled);
bool IsKnownNotepadWindow(string titleText) bool IsKnownNotepadWindow(string titleText)
{
if (!string.IsNullOrWhiteSpace(WindowName))
{
if (WindowName.Equals(titleText, StringComparison.Ordinal))
{
WindowKind = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad;
}
}
else
{ {
switch (titleText) switch (titleText)
{ {
case "Untitled - Notepad": case "Untitled - Notepad":
WindowKind = WindowKind.Notepad; WindowKind = WindowKind.Notepad;
Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null); break;
return true; default:
}
if (notepadPlusPlusRegex.IsMatch(titleText)) if (notepadPlusPlusRegex.IsMatch(titleText))
{ {
WindowKind = WindowKind.NotepadPlusPlus; WindowKind = WindowKind.NotepadPlusPlus;
Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null); }
return true; break;
}
} }
return false; Handle = FindInnerWindow(WindowKind);
return WindowKind != default;
}
IntPtr FindInnerWindow(WindowKind windowKind)
{
switch (windowKind)
{
case WindowKind.Notepad:
return NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null);
case WindowKind.NotepadPlusPlus:
return NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null);
default:
return Handle;
}
} }
} }
} }

View file

@ -12,11 +12,14 @@ namespace Notepad.Extensions.Logging
readonly ObjectPool<WindowEnumerationState> statePool; readonly ObjectPool<WindowEnumerationState> statePool;
public string WindowName { get; internal set; }
public WindowInfo FindNotepadWindow() public WindowInfo FindNotepadWindow()
{ {
var stateObject = statePool.Get(); var stateObject = statePool.Get();
try try
{ {
stateObject.WindowName = WindowName;
NativeMethods.EnumWindows(enumWindowsDelegate, stateObject); NativeMethods.EnumWindows(enumWindowsDelegate, stateObject);
return new WindowInfo(stateObject.WindowKind, stateObject.Handle); return new WindowInfo(stateObject.WindowKind, stateObject.Handle);
} }