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.Logging.Configuration;
using Microsoft.Extensions.Options;
using Notepad.Extensions.Logging;
using System;
namespace Microsoft.Extensions.Logging

View file

@ -94,14 +94,11 @@ namespace Notepad.Extensions.Logging
switch (info.Kind)
{
case WindowKind.Notepad:
SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message);
SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message);
break;
case WindowKind.NotepadPlusPlus:
{
WriteToNotepadPlusPlus(info.Handle, message);
break;
}
}
}

View file

@ -42,6 +42,10 @@ namespace Notepad.Extensions.Logging
private void ReloadLoggerOptions(NotepadLoggerOptions 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 WindowKind WindowKind { get; private set; }
public string WindowName { get; internal set; }
public void Reset()
{
Handle = default;
WindowKind = default;
WindowName = default;
sb.Clear();
}
@ -47,26 +49,50 @@ namespace Notepad.Extensions.Logging
return true;
}
static Regex notepadPlusPlusRegex = new Regex(@"^new \d+ - Notepad\+\+$", RegexOptions.Compiled);
static Regex notepadPlusPlusRegex = new Regex(@" - Notepad\+\+$", RegexOptions.Compiled);
bool IsKnownNotepadWindow(string titleText)
{
switch (titleText)
if (!string.IsNullOrWhiteSpace(WindowName))
{
case "Untitled - Notepad":
WindowKind = WindowKind.Notepad;
Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null);
return true;
if (WindowName.Equals(titleText, StringComparison.Ordinal))
{
WindowKind = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad;
}
}
else
{
switch (titleText)
{
case "Untitled - Notepad":
WindowKind = WindowKind.Notepad;
break;
default:
if (notepadPlusPlusRegex.IsMatch(titleText))
{
WindowKind = WindowKind.NotepadPlusPlus;
}
break;
}
}
if (notepadPlusPlusRegex.IsMatch(titleText))
{
WindowKind = WindowKind.NotepadPlusPlus;
Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null);
return true;
}
Handle = FindInnerWindow(WindowKind);
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;
public string WindowName { get; internal set; }
public WindowInfo FindNotepadWindow()
{
var stateObject = statePool.Get();
try
{
stateObject.WindowName = WindowName;
NativeMethods.EnumWindows(enumWindowsDelegate, stateObject);
return new WindowInfo(stateObject.WindowKind, stateObject.Handle);
}