Skip to content

Commit

Permalink
优化 截图窗口大小显示
Browse files Browse the repository at this point in the history
优化 情景执行错误提示
优化 截图窗口识别
优化 截图窗口操作
  • Loading branch information
MakesYT committed Jan 5, 2025
1 parent 41b1095 commit 1106f56
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 23 deletions.
61 changes: 43 additions & 18 deletions Core.Window/ScreenCapture/ScreenCaptureByWGC.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Graphics;
Expand Down Expand Up @@ -55,6 +56,9 @@ public List<ScreenCaptureInfo> GetAllScreenInfo()
}
public const uint WS_POPUP = 0x80000000; // 弹出窗口样式
public const uint WS_CHILD = 0x40000000; // 子窗口样式
private const int WS_EX_TOOLWINDOW = 0x00000080; // 工具窗口
private const int WS_EX_APPWINDOW = 0x00040000; // 应用窗口
const int WS_EX_NOREDIRECTIONBITMAP = 0x00200000;
public List<WindowInfo> GetAllWindowInfo()
{
var screenCaptureInfos = new List<WindowInfo>();
Expand All @@ -79,8 +83,13 @@ public List<WindowInfo> GetAllWindowInfo()
{
continue;
}
int style = User32.GetWindowLong(currentHwnd,User32.WindowLongFlags.GWL_STYLE);
if ( (style & WS_CHILD) != 0)
int style1= User32.GetWindowLong(currentHwnd,User32.WindowLongFlags.GWL_STYLE);
if ( (style1 & (uint) User32.WindowStyles.WS_POPUP) != 0)
{
continue;
}
int style2 = User32.GetWindowLong(currentHwnd,User32.WindowLongFlags.GWL_EXSTYLE);
if ( (style2 & (int) User32.WindowStylesEx.WS_EX_NOACTIVATE) != 0||(style2 & (int) User32.WindowStylesEx.WS_EX_TOOLWINDOW) != 0)
{
continue;
}
Expand All @@ -89,6 +98,17 @@ public List<WindowInfo> GetAllWindowInfo()
continue;
}

User32.GetWindowDisplayAffinity(currentHwnd, out var affinity);
if (affinity!= User32.WindowDisplayAffinity.WDA_NONE)
{
continue;
}
User32.GetWindowThreadProcessId(currentHwnd,out var id);

var s = Process.GetProcessById((int)id).ProcessName;



// 获取窗口标题
StringBuilder stringBuilder = new StringBuilder(100);
User32.GetWindowText(currentHwnd, stringBuilder, 100);
Expand All @@ -102,6 +122,7 @@ public List<WindowInfo> GetAllWindowInfo()
screenCaptureInfos.Add(new WindowInfo()
{
Title = title,
ModuleFileName = s,
Hwnd = currentHwnd.DangerousGetHandle(),
Rect = new Rect(rect.X, rect.Y, rect.Width, rect.Height),
ZIndex = zIndex
Expand Down Expand Up @@ -281,23 +302,27 @@ public unsafe ScreenCaptureResult CaptureScreenBytes(ScreenCaptureInfo screenCap
}
case ScreenCaptureType.窗口:
{
if (!User32.IsWindow(screenCaptureInfo.WindowInfo.Hwnd))
var allWindowInfo = GetAllWindowInfo();
if (allWindowInfo.Any(e=>e.Hwnd==screenCaptureInfo.WindowInfo.Hwnd&&e.Title==screenCaptureInfo.WindowInfo.Title&&e.ModuleFileName==screenCaptureInfo.WindowInfo.ModuleFileName))
{
var findWindow = User32.FindWindow(null,screenCaptureInfo.WindowInfo.Title);
if (findWindow.IsNull)
{
throw new Exception("目标窗口不存在");
}

screenCaptureInfo.WindowInfo.Hwnd = findWindow.DangerousGetHandle();

var monitorFromWindow = User32.MonitorFromWindow(screenCaptureInfo.WindowInfo.Hwnd,User32.MonitorFlags.MONITOR_DEFAULTTONEAREST);
screenCaptureInfo.ScreenInfo.hMonitor = monitorFromWindow.DangerousGetHandle();
break;
}

User32.GetWindowRect(screenCaptureInfo.WindowInfo.Hwnd, out var rect);
screenCaptureInfo.WindowInfo.Rect=new Rect(0,0,rect.Width,rect.Height);
var monitorFromWindow = User32.MonitorFromWindow(screenCaptureInfo.WindowInfo.Hwnd,User32.MonitorFlags.MONITOR_DEFAULTTONEAREST);
screenCaptureInfo.ScreenInfo.hMonitor = monitorFromWindow.DangerousGetHandle();
break;
else if (allWindowInfo.Any(e=>e.Title==screenCaptureInfo.WindowInfo.Title&&e.ModuleFileName==screenCaptureInfo.WindowInfo.ModuleFileName))
{
screenCaptureInfo.WindowInfo=allWindowInfo.First(e=>e.Title==screenCaptureInfo.WindowInfo.Title&&e.ModuleFileName==screenCaptureInfo.WindowInfo.ModuleFileName);
var monitorFromWindow = User32.MonitorFromWindow(screenCaptureInfo.WindowInfo.Hwnd,User32.MonitorFlags.MONITOR_DEFAULTTONEAREST);
screenCaptureInfo.ScreenInfo.hMonitor = monitorFromWindow.DangerousGetHandle();
break;
}else if (allWindowInfo.Any(e=>e.ModuleFileName==screenCaptureInfo.WindowInfo.ModuleFileName))
{
screenCaptureInfo.WindowInfo=allWindowInfo.First(e=>e.ModuleFileName==screenCaptureInfo.WindowInfo.ModuleFileName);
var monitorFromWindow = User32.MonitorFromWindow(screenCaptureInfo.WindowInfo.Hwnd,User32.MonitorFlags.MONITOR_DEFAULTTONEAREST);
screenCaptureInfo.ScreenInfo.hMonitor = monitorFromWindow.DangerousGetHandle();
break;
}
throw new Exception("目标窗口不存在");
}

}
Expand Down
9 changes: 7 additions & 2 deletions Core/SDKs/CustomScenario/CustomScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,14 @@ private void ParsePointItem(Dictionary<ScenarioMethodNode, Thread?> threads,
}
catch (Exception e)
{
Log.Debug(e);
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("情景",
Log.Error("错误",e);
if (e.InnerException is not null)
{
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("情景",
$"情景{Name}出现错误\n{e.InnerException?.Message}");
}else ((IToastService)ServiceManager.Services.GetService(typeof(IToastService))!).Show("情景",
$"情景{Name}出现错误\n{e.Message}");

Task.Run(() => { Stop(true); });

valid = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:capture="clr-namespace:KitopiaAvalonia.Controls.Capture">
xmlns:capture="clr-namespace:KitopiaAvalonia.Controls.Capture"
xmlns:screenCaptureWindow="clr-namespace:KitopiaAvalonia.Converter.ScreenCaptureWindow">
<Design.PreviewWith>
<capture:DraggableResizeableControl Width="200" Height="200" />
</Design.PreviewWith>
Expand All @@ -11,8 +12,11 @@
<ControlTemplate>
<Panel>

<Panel.Resources>
<screenCaptureWindow:SizeToIntegerStringConverter x:Key="SizeToIntegerStringConverter" />
</Panel.Resources>
<ContentPresenter Name="Presenter" Content="{TemplateBinding Content}" />
<TextBlock Text="{Binding #Presenter.Bounds.Size}" IsVisible="{TemplateBinding IsSelected}" />
<TextBlock Text="{Binding #Presenter.Bounds.Size,Converter={StaticResource SizeToIntegerStringConverter}}" IsVisible="{TemplateBinding IsSelected}" />
<Border BorderThickness="2" Name="ResizeSizeBoxBorder" IsVisible="{TemplateBinding IsSelected}"
Width="{Binding #Presenter.Bounds.Width}" Height="{Binding #Presenter.Bounds.Height}">
<Border.BorderBrush>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Globalization;

using Avalonia;
using Avalonia.Data.Converters;

namespace KitopiaAvalonia.Converter.ScreenCaptureWindow;

public class SizeToIntegerStringConverter : IValueConverter
{
private static string format="{0:F0}x{1:F0}";
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Size size)
{
return string.Format(format, size.Width, size.Height);
}
return string.Empty;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
7 changes: 7 additions & 0 deletions KitopiaAvalonia/Windows/ScreenCaptureWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ protected override void OnPointerExited(PointerEventArgs e)
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
var position = e.GetPosition(this);
if (Math.Pow(position.Y-_startPoint.Y,2)+ Math.Pow(position.X-_startPoint.X,2)<100)
{
return;
}

if (e.GetCurrentPoint(this)
.Properties.IsLeftButtonPressed && Selecting)
{
Expand Down Expand Up @@ -310,6 +316,7 @@ protected override void OnPointerMoved(PointerEventArgs e)
positionY >= e.Rect.Y && positionY <= e.Rect.Y + e.Rect.Height).OrderBy(e=>e.ZIndex).ToList();
if (firstOrDefault.Count()==0)
{
_currentWindowInfo = new WindowInfo();
_startPoint = new Point(0, 0);
SelectBox._dragTransform.X = 0;
SelectBox._dragTransform.Y = 0;
Expand Down
2 changes: 1 addition & 1 deletion PluginCore
Submodule PluginCore updated 1 files
+1 −0 IScreenCapture.cs

0 comments on commit 1106f56

Please sign in to comment.