mirror of
https://github.com/yaakov-h/Notepad.Extensions.Logging.git
synced 2024-11-22 11:14:50 +00:00
Added support of window name from options in WindowEnumerationState class.
This commit is contained in:
parent
6926e2a858
commit
d2e66538e0
5 changed files with 48 additions and 17 deletions
|
@ -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
|
||||
|
|
|
@ -96,14 +96,11 @@ namespace Notepad.Extensions.Logging
|
|||
case WindowKind.Notepad:
|
||||
SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message);
|
||||
break;
|
||||
|
||||
case WindowKind.NotepadPlusPlus:
|
||||
{
|
||||
WriteToNotepadPlusPlus(info.Handle, message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe static void WriteToNotepadPlusPlus(IntPtr hwnd, string message)
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(WindowName))
|
||||
{
|
||||
if (WindowName.Equals(titleText, StringComparison.Ordinal))
|
||||
{
|
||||
WindowKind = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (titleText)
|
||||
{
|
||||
case "Untitled - Notepad":
|
||||
WindowKind = WindowKind.Notepad;
|
||||
Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null);
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
if (notepadPlusPlusRegex.IsMatch(titleText))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue