some test code

This commit is contained in:
huiyadanli 2023-10-07 16:01:10 +08:00
parent 9c4b795d42
commit 5794164546
14 changed files with 216 additions and 47 deletions

View File

@ -52,6 +52,9 @@
<Compile Update="View\Pages\TriggerSettingsPage.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Update="View\PickerWindow.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>

View File

@ -81,7 +81,8 @@ namespace BetterGenshinImpact.GameTask
/// <returns></returns>
public static RECT GetWindowRect(IntPtr hWnd)
{
User32.GetWindowRect(hWnd, out var windowRect);
// User32.GetWindowRect(hWnd, out var windowRect);
DwmApi.DwmGetWindowAttribute<RECT>(hWnd, DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out var windowRect);
return windowRect;
}

View File

@ -1,9 +1,8 @@
<Window x:Class="BetterGenshinImpact.View.Test.CaptureTestWindow"
<Window x:Class="BetterGenshinImpact.View.CaptureTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BetterGenshinImpact.View.Test"
mc:Ignorable="d"
Title="CaptureTestWindow" Height="450" Width="800">
<Grid>

View File

@ -1,12 +1,11 @@
using BetterGenshinImpact.Helpers.Extensions;
using Fischless.WindowCapture;
using System;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using Vanara.PInvoke;
using BetterGenshinImpact.Helpers.Extensions;
using Fischless.WindowCapture;
namespace BetterGenshinImpact.View.Test
namespace BetterGenshinImpact.View
{
/// <summary>
/// CaptureTestWindow.xaml 的交互逻辑
@ -14,9 +13,27 @@ namespace BetterGenshinImpact.View.Test
public partial class CaptureTestWindow : Window
{
private IWindowCapture? _capture;
private long _captureTime;
private long _transferTime;
private long _captureCount;
public CaptureTestWindow()
{
_captureTime = 0;
_transferTime = 0;
_captureCount = 0;
InitializeComponent();
Closed += (sender, args) =>
{
CompositionTarget.Rendering -= Loop;
_capture?.Stop();
Debug.WriteLine("平均截图耗时:" + _captureTime * 1.0 / _captureCount);
Debug.WriteLine("平均转换耗时:" + _transferTime * 1.0 / _captureCount);
Debug.WriteLine("平均总耗时:" + (_captureTime + _transferTime) * 1.0 / _captureCount);
};
}
public void StartCapture(IntPtr hWnd, CaptureModes captureMode)
@ -42,15 +59,22 @@ namespace BetterGenshinImpact.View.Test
var bitmap = _capture?.Capture();
sw.Stop();
Debug.WriteLine("截图耗时:" + sw.ElapsedMilliseconds);
_captureTime += sw.ElapsedMilliseconds;
if (bitmap != null)
{
_captureCount++;
sw.Reset();
sw.Start();
DisplayCaptureResultImage.Source = bitmap.ToBitmapImage();
sw.Stop();
Debug.WriteLine("转换耗时:" + sw.ElapsedMilliseconds);
_transferTime += sw.ElapsedMilliseconds;
}
else
{
Debug.WriteLine("截图失败");
}
}
}
}
}

View File

@ -33,15 +33,14 @@ namespace BetterGenshinImpact.View
static MaskWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MaskWindow), new FrameworkPropertyMetadata(typeof(MaskWindow)));
}
public static MaskWindow Instance(IntPtr? hWnd = null)
{
_maskWindow ??= new MaskWindow();
if (hWnd != null && hWnd!=IntPtr.Zero)
if (hWnd != null && hWnd != IntPtr.Zero)
{
User32.GetWindowRect(hWnd.Value, out var rect);
var rect = SystemControl.GetWindowRect(hWnd.Value);
double scale = DpiHelper.ScaleY;
_maskWindow.Left = (int)Math.Ceiling(rect.X * 1d / scale);
_maskWindow.Top = (int)Math.Ceiling(rect.Y * 1d / scale);

View File

@ -43,6 +43,6 @@
</StackPanel>
<ui:Button x:Name="StartCaptureTest" Margin="0,20,0,0" Content="测试图像捕获" Command="{Binding StartCaptureTestCommand}" />
<ui:Button x:Name="SvtrTest" Margin="0,20,0,0" Content="SVTR测试" Command="{Binding SvtrTestCommand}" />
<!-- <ui:Button x:Name="SvtrTest" Margin="0,20,0,0" Content="SVTR测试" Command="{Binding SvtrTestCommand}" /> -->
</StackPanel>
</Page>

View File

@ -0,0 +1,36 @@
<Window x:Class="BetterGenshinImpact.View.PickerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="选择捕获窗口" Height="450" Width="800">
<Grid Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="0"
Margin="5,0"
Orientation="Horizontal"
Background="White">
<TextBlock FontSize="20" Text="双击选中要捕获的窗口" />
</StackPanel>
<ListBox
x:Name="WindowList"
Grid.Row="1"
Padding="0,10"
BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl MouseDoubleClick="WindowsOnMouseDoubleClick">
<TextBlock FontSize="14" Text="{Binding Name}" />
</ContentControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>

View File

@ -0,0 +1,88 @@
using OpenCvSharp.Internal;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using Vanara.PInvoke;
namespace BetterGenshinImpact.View
{
/// <summary>
/// PickerWindow.xaml 的交互逻辑
/// </summary>
public partial class PickerWindow : Window
{
private readonly string[] _ignoreProcesses = { "applicationframehost", "shellexperiencehost", "systemsettings", "winstore.app", "searchui" };
public PickerWindow()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
FindWindows();
}
public IntPtr PickCaptureTarget(IntPtr hWnd)
{
new WindowInteropHelper(this).Owner = hWnd;
ShowDialog();
return ((CapturableWindow?)WindowList.SelectedItem)?.Handle ?? IntPtr.Zero;
}
private unsafe void FindWindows()
{
var wih = new WindowInteropHelper(this);
User32.EnumWindows((hWnd, lParam) =>
{
// ignore invisible windows
if (!User32.IsWindowVisible(hWnd))
return true;
// ignore untitled windows
var title = new StringBuilder(1024);
User32.GetWindowText(hWnd, title, title.Capacity);
if (string.IsNullOrWhiteSpace(title.ToString()))
return true;
// ignore me
if (wih.Handle == hWnd)
return true;
User32.GetWindowThreadProcessId(hWnd, out var processId);
// ignore by process name
var process = Process.GetProcessById((int)processId);
if (_ignoreProcesses.Contains(process.ProcessName.ToLower()))
return true;
WindowList.Items.Add(new CapturableWindow
{
Handle = (IntPtr)hWnd,
Name = $"{title} ({process.ProcessName}.exe)"
});
return true;
}, IntPtr.Zero);
}
private void WindowsOnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Close();
}
}
public struct CapturableWindow
{
public string Name { get; set; }
public IntPtr Handle { get; set; }
}
}

View File

@ -1,7 +1,6 @@
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.View;
using BetterGenshinImpact.View.Test;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;

View File

@ -1,28 +1,17 @@
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.View;
using BetterGenshinImpact.View.Test;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Fischless.WindowCapture;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using Vanara.PInvoke;
using System.Windows.Threading;
using BetterGenshinImpact.Model;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
namespace BetterGenshinImpact.ViewModel
{
public partial class MaskWindowViewModel : ObservableRecipient
{
private ILogger<MaskWindowViewModel>? _logger;
[ObservableProperty] private Rect _windowRect;
[ObservableProperty] private ObservableCollection<MaskButton> _maskButtons = new();
@ -69,8 +58,7 @@ namespace BetterGenshinImpact.ViewModel
[RelayCommand]
private void OnLoaded()
{
_logger = App.GetLogger<MaskWindowViewModel>();
_logger.LogInformation("遮罩窗口启OnLoaded");
}
}
}

View File

@ -1,6 +1,5 @@
using BetterGenshinImpact.Core;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.View.Test;
using BetterGenshinImpact.View;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
@ -22,6 +21,8 @@ using BetterGenshinImpact.Service.Interface;
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Core.Recognition.ONNX.SVTR;
using OpenCvSharp;
using System.Windows.Interop;
using BetterGenshinImpact.Helpers;
namespace BetterGenshinImpact.ViewModel.Pages;
@ -86,16 +87,25 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware
[RelayCommand]
private void OnStartCaptureTest()
{
var hWnd = SystemControl.FindGenshinImpactHandle();
if (hWnd == IntPtr.Zero)
{
System.Windows.MessageBox.Show("未找到原神窗口");
return;
}
//var hWnd = SystemControl.FindGenshinImpactHandle();
//if (hWnd == IntPtr.Zero)
//{
// System.Windows.MessageBox.Show("未找到原神窗口");
// return;
//}
CaptureTestWindow captureTestWindow = new();
captureTestWindow.StartCapture(hWnd, Config.CaptureMode.ToCaptureMode());
captureTestWindow.Show();
//CaptureTestWindow captureTestWindow = new();
//captureTestWindow.StartCapture(hWnd, Config.CaptureMode.ToCaptureMode());
//captureTestWindow.Show();
var picker = new PickerWindow();
var hWnd = picker.PickCaptureTarget(new WindowInteropHelper(UIDispatcherHelper.MainWindow).Handle);
if (hWnd != IntPtr.Zero)
{
var captureWindow = new CaptureTestWindow();
captureWindow.StartCapture(hWnd, Config.CaptureMode.ToCaptureMode());
captureWindow.Show();
}
}
private bool CanStartTrigger() => StartButtonEnabled;

View File

@ -32,7 +32,7 @@ public class BitBltCapture : IWindowCapture
{
if (IsClientEnabled)
{
_ = User32.GetWindowRect(_hWnd, out var windowRect);
_ = DwmApi.DwmGetWindowAttribute<RECT>(_hWnd, DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out var windowRect);
var width = windowRect.right - windowRect.left;
var height = windowRect.bottom - windowRect.top;

View File

@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="SharpDX.Direct3D11" Version="4.2.0" />
<PackageReference Include="Vanara.PInvoke.DwmApi" Version="3.4.16" />
<PackageReference Include="Vanara.PInvoke.Gdi32" Version="3.4.16" />
<PackageReference Include="Vanara.PInvoke.User32" Version="3.4.16" />
<PackageReference Include="Vanara.PInvoke.SHCore" Version="3.4.16" />

View File

@ -15,9 +15,25 @@ namespace Vision.WindowCapture.Test
public partial class CaptureTestWindow : Window
{
private IWindowCapture? _capture;
private long _captureTime;
private long _transferTime;
private long _captureCount;
public CaptureTestWindow()
{
_captureTime = 0;
_captureCount = 0;
InitializeComponent();
Closed += (sender, args) =>
{
CompositionTarget.Rendering -= Loop;
_capture?.StopAsync();
Debug.WriteLine("平均截图耗时:" + _captureTime * 1.0 / _captureCount);
Debug.WriteLine("平均转换耗时:" + _transferTime * 1.0 / _captureCount);
Debug.WriteLine("平均总耗时:" + (_captureTime + _transferTime) * 1.0 / _captureCount);
};
}
public async void StartCapture(IntPtr hWnd, CaptureModeEnum captureMode)
@ -42,18 +58,24 @@ namespace Vision.WindowCapture.Test
var bitmap = _capture?.Capture();
sw.Stop();
Debug.WriteLine("截图耗时:" + sw.ElapsedMilliseconds);
_captureTime += sw.ElapsedMilliseconds;
if (bitmap != null)
{
_captureCount++;
sw.Reset();
sw.Start();
DisplayCaptureResultImage.Source = ToBitmapImage(bitmap);
sw.Stop();
Debug.WriteLine("转换耗时:" + sw.ElapsedMilliseconds);
_transferTime += sw.ElapsedMilliseconds;
}
else
{
Debug.WriteLine("截图失败");
}
}
public static BitmapImage ToBitmapImage( Bitmap bitmap)
public static BitmapImage ToBitmapImage(Bitmap bitmap)
{
var ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
@ -65,5 +87,4 @@ namespace Vision.WindowCapture.Test
return image;
}
}
}
}