Skip to content

Commit

Permalink
优化 窗口截图
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed Jan 4, 2025
1 parent 0f573cb commit 5a2a70a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
62 changes: 28 additions & 34 deletions Core.Window/ScreenCapture/ScreenCaptureByWGC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,65 +58,59 @@ public List<WindowInfo> GetAllWindowInfo()
{
var screenCaptureInfos = new List<WindowInfo>();
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;
Expand Down
4 changes: 2 additions & 2 deletions KitopiaAvalonia/Windows/ScreenCaptureWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down

0 comments on commit 5a2a70a

Please sign in to comment.