Files
wx-win-agent/node-agent/WxAgent.Windows/WechatNativeWindow.cs
T

57 lines
2.4 KiB
C#

using System.Diagnostics;
using Windows.Win32;
using WxAgent.Core;
namespace WxAgent.Windows;
public sealed record WechatWindowDiagnostic(long Handle, int ProcessId, string ClassName, string Title,
bool Visible, bool Minimized, uint Dpi, int Width, int Height);
internal static class WechatNativeWindow
{
internal static unsafe IReadOnlyList<WechatWindowDiagnostic> Enumerate()
{
using var current = Process.GetCurrentProcess();
var processIds = new HashSet<int>();
foreach (var process in Process.GetProcessesByName("Weixin"))
{
using (process)
{
try { if (process.SessionId == current.SessionId) processIds.Add(process.Id); }
catch (InvalidOperationException) { }
}
}
var result = new List<WechatWindowDiagnostic>();
PInvoke.EnumWindows((hwnd, _) =>
{
uint pid;
PInvoke.GetWindowThreadProcessId(hwnd, &pid);
if (!processIds.Contains((int)pid)) return true;
Span<char> className = stackalloc char[256];
Span<char> title = stackalloc char[512];
var classLength = PInvoke.GetClassName(hwnd, className);
var titleLength = PInvoke.GetWindowText(hwnd, title);
PInvoke.GetWindowRect(hwnd, out var rect);
result.Add(new WechatWindowDiagnostic((long)(nint)hwnd.Value, (int)pid,
new string(className[..classLength]), new string(title[..titleLength]),
PInvoke.IsWindowVisible(hwnd), PInvoke.IsIconic(hwnd), PInvoke.GetDpiForWindow(hwnd),
rect.right - rect.left, rect.bottom - rect.top));
return true;
}, default);
return result;
}
internal static WechatWindowDiagnostic Sanitize(WechatWindowDiagnostic window) =>
window with { Title = UiSnapshotSanitizer.SanitizeName(window.Title) };
}
public static partial class WechatChatClient
{
public static IReadOnlyList<WechatWindowDiagnostic> InspectNativeWindows() =>
WechatNativeWindow.Enumerate().Select(WechatNativeWindow.Sanitize).ToArray();
public static IReadOnlyList<WechatWindowDiagnostic> InspectMainWindows() =>
WechatNativeWindow.Enumerate().Where(window => WechatLocators.IsNativeMainWindow(window.ClassName, window.Title))
.Select(WechatNativeWindow.Sanitize).ToArray();
}