From 5a2a70a22b801848b6d856395f2af3db4834a47e Mon Sep 17 00:00:00 2001 From: MakesYT <2696703792@qq.com> Date: Sat, 4 Jan 2025 12:57:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E6=88=AA=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ScreenCapture/ScreenCaptureByWGC.cs | 62 +++++++++---------- .../Windows/ScreenCaptureWindow.axaml.cs | 4 +- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/Core.Window/ScreenCapture/ScreenCaptureByWGC.cs b/Core.Window/ScreenCapture/ScreenCaptureByWGC.cs index 5628de6..ed08b56 100644 --- a/Core.Window/ScreenCapture/ScreenCaptureByWGC.cs +++ b/Core.Window/ScreenCapture/ScreenCaptureByWGC.cs @@ -58,65 +58,59 @@ public List GetAllWindowInfo() { var screenCaptureInfos = new List(); uint i = 0; + + + + + + // 按 Z-Order 遍历窗口 + + HWND currentHwnd = User32.GetTopWindow(IntPtr.Zero); int zIndex = 0; - User32.EnumWindows((arg1, arg2) => + + while (currentHwnd != IntPtr.Zero) { + + zIndex++; + currentHwnd = User32.GetWindow(currentHwnd, User32.GetWindowCmd.GW_HWNDNEXT); // 忽略有父窗口的和不可见的窗口 - if (!User32.GetParent(arg1).IsNull || !User32.IsWindowVisible(arg1)|| User32.IsIconic(arg1)) + if (!User32.GetParent(currentHwnd).IsNull || !User32.IsWindowVisible(currentHwnd)|| User32.IsIconic(currentHwnd)) { - return true; + continue; } - int style = User32.GetWindowLong(arg1,User32.WindowLongFlags.GWL_STYLE); + int style = User32.GetWindowLong(currentHwnd,User32.WindowLongFlags.GWL_STYLE); if ((style & WS_POPUP) != 0 || (style & WS_CHILD) != 0) { - return true; // 跳过弹出窗口或子窗口 + continue; } - if (!User32.IsWindow(arg1)) + if (!User32.IsWindow(currentHwnd)) { - return true; // 跳过无效窗口 + continue; } // 获取窗口标题 StringBuilder stringBuilder = new StringBuilder(100); - User32.GetWindowText(arg1, stringBuilder, 100); + User32.GetWindowText(currentHwnd, stringBuilder, 100); var title = stringBuilder.ToString(); if (string.IsNullOrWhiteSpace(title)) { - return true; + continue; } // 获取窗口的位置和大小 - User32.GetWindowRect(arg1, out var rect); - - - // 按 Z-Order 遍历窗口 - IntPtr hwnd = arg1.DangerousGetHandle(); - HWND currentHwnd = User32.GetTopWindow(IntPtr.Zero); - int zIndex = 0; - - while (currentHwnd != IntPtr.Zero) - { - if (User32.IsWindowVisible(currentHwnd)) // 只考虑可见窗口 - { - if (currentHwnd == hwnd) - { - break; - } - zIndex++; - } - currentHwnd = User32.GetWindow(currentHwnd, User32.GetWindowCmd.GW_HWNDNEXT); - } - - // 添加到结果列表 + User32.GetWindowRect(currentHwnd, out var rect); screenCaptureInfos.Add(new WindowInfo() { Title = title, - Hwnd = hwnd, + Hwnd = currentHwnd.DangerousGetHandle(), Rect = new Rect(rect.X, rect.Y, rect.Width, rect.Height), ZIndex = zIndex }); + } - return true; - }, IntPtr.Zero); + // 添加到结果列表 + + + return screenCaptureInfos; diff --git a/KitopiaAvalonia/Windows/ScreenCaptureWindow.axaml.cs b/KitopiaAvalonia/Windows/ScreenCaptureWindow.axaml.cs index ff7e09b..299f280 100644 --- a/KitopiaAvalonia/Windows/ScreenCaptureWindow.axaml.cs +++ b/KitopiaAvalonia/Windows/ScreenCaptureWindow.axaml.cs @@ -304,7 +304,7 @@ protected override void OnPointerMoved(PointerEventArgs e) var screenInfoHeight =Bounds.Height/_screenCaptureInfo.ScreenInfo.Height; var positionY = currentPoint.Position.Y/screenInfoWidth; var positionX = currentPoint.Position.X/screenInfoHeight; - var firstOrDefault = _windowInfos.Where(e=>e.Rect.Height!=_screenCaptureInfo.ScreenInfo.Height||e.Rect.Width!=_screenCaptureInfo.ScreenInfo.Width).Where(e => positionX >= e.Rect.X && positionX <= e.Rect.X + e.Rect.Width && + var firstOrDefault = _windowInfos.Where(e => positionX >= e.Rect.X && positionX <= e.Rect.X + e.Rect.Width && positionY >= e.Rect.Y && positionY <= e.Rect.Y + e.Rect.Height).OrderBy(e=>e.ZIndex).ToList(); if (firstOrDefault.Count()==0) { @@ -317,7 +317,7 @@ protected override void OnPointerMoved(PointerEventArgs e) } else { - var windowInfo = firstOrDefault.Last(); + var windowInfo = firstOrDefault.FirstOrDefault(); _startPoint=new Point(windowInfo.Rect.X*screenInfoWidth,windowInfo.Rect.Y*screenInfoHeight); SelectBox._dragTransform.X = _startPoint.X; SelectBox._dragTransform.Y = _startPoint.Y;