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

@ -94,14 +94,11 @@ namespace Notepad.Extensions.Logging
switch (info.Kind) switch (info.Kind)
{ {
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;
}
} }
} }

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)
{ {
switch (titleText) if (!string.IsNullOrWhiteSpace(WindowName))
{ {
case "Untitled - Notepad": if (WindowName.Equals(titleText, StringComparison.Ordinal))
WindowKind = WindowKind.Notepad; {
Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null); WindowKind = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad;
return true; }
}
else
{
switch (titleText)
{
case "Untitled - Notepad":
WindowKind = WindowKind.Notepad;
break;
default:
if (notepadPlusPlusRegex.IsMatch(titleText))
{
WindowKind = WindowKind.NotepadPlusPlus;
}
break;
}
} }
if (notepadPlusPlusRegex.IsMatch(titleText)) Handle = FindInnerWindow(WindowKind);
{
WindowKind = WindowKind.NotepadPlusPlus;
Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null);
return true;
}
return false; 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);
} }