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 Enumerate() { using var current = Process.GetCurrentProcess(); var processIds = new HashSet(); 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(); PInvoke.EnumWindows((hwnd, _) => { uint pid; PInvoke.GetWindowThreadProcessId(hwnd, &pid); if (!processIds.Contains((int)pid)) return true; Span className = stackalloc char[256]; Span 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 InspectNativeWindows() => WechatNativeWindow.Enumerate().Select(WechatNativeWindow.Sanitize).ToArray(); public static IReadOnlyList InspectMainWindows() => WechatNativeWindow.Enumerate().Where(window => WechatLocators.IsNativeMainWindow(window.ClassName, window.Title)) .Select(WechatNativeWindow.Sanitize).ToArray(); }