mirror of
https://github.com/yaakov-h/Notepad.Extensions.Logging.git
synced 2025-04-11 23:14:52 +00:00
38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using System;
|
|
using Microsoft.Extensions.ObjectPool;
|
|
|
|
namespace Notepad.Extensions.Logging
|
|
{
|
|
class WindowFinder : IWindowFinder
|
|
{
|
|
public WindowFinder()
|
|
{
|
|
statePool = new DefaultObjectPool<WindowEnumerationState>(WindowEnumerationStatePoolingPolicy.Instance);
|
|
}
|
|
|
|
readonly ObjectPool<WindowEnumerationState> statePool;
|
|
|
|
public WindowInfo FindNotepadWindow(string windowName)
|
|
{
|
|
var stateObject = statePool.Get();
|
|
try
|
|
{
|
|
stateObject.WindowName = windowName;
|
|
NativeMethods.EnumWindows(enumWindowsDelegate, stateObject);
|
|
return new WindowInfo(stateObject.WindowKind, stateObject.Handle);
|
|
}
|
|
finally
|
|
{
|
|
statePool.Return(stateObject);
|
|
}
|
|
}
|
|
|
|
static NativeMethods.EnumWindowsDelegate enumWindowsDelegate = new NativeMethods.EnumWindowsDelegate(EnumWindowsCallback);
|
|
|
|
static bool EnumWindowsCallback(IntPtr hWnd, object lParam)
|
|
{
|
|
var state = (WindowEnumerationState)lParam;
|
|
return state.ExamineWindow(hWnd);
|
|
}
|
|
}
|
|
}
|