From 9d8c07a2a9898053b4e0ef5044015c194445372b Mon Sep 17 00:00:00 2001 From: huiyadanli Date: Sat, 7 Oct 2023 11:26:26 +0800 Subject: [PATCH] Add capture test --- BetterGenshinImpact.Win32/NativeMethods.txt | 4 + BetterGenshinImpact.sln | 10 ++ Vision.WindowCapture.Test/App.xaml | 9 ++ Vision.WindowCapture.Test/App.xaml.cs | 17 +++ Vision.WindowCapture.Test/AssemblyInfo.cs | 10 ++ .../CaptureTestWindow.xaml | 12 ++ .../CaptureTestWindow.xaml.cs | 69 +++++++++++ Vision.WindowCapture.Test/MainWindow.xaml | 12 ++ Vision.WindowCapture.Test/MainWindow.xaml.cs | 41 +++++++ Vision.WindowCapture.Test/PickerWindow.xaml | 36 ++++++ .../PickerWindow.xaml.cs | 107 ++++++++++++++++++ .../Vision.WindowCapture.Test.csproj | 21 ++++ 12 files changed, 348 insertions(+) create mode 100644 Vision.WindowCapture.Test/App.xaml create mode 100644 Vision.WindowCapture.Test/App.xaml.cs create mode 100644 Vision.WindowCapture.Test/AssemblyInfo.cs create mode 100644 Vision.WindowCapture.Test/CaptureTestWindow.xaml create mode 100644 Vision.WindowCapture.Test/CaptureTestWindow.xaml.cs create mode 100644 Vision.WindowCapture.Test/MainWindow.xaml create mode 100644 Vision.WindowCapture.Test/MainWindow.xaml.cs create mode 100644 Vision.WindowCapture.Test/PickerWindow.xaml create mode 100644 Vision.WindowCapture.Test/PickerWindow.xaml.cs create mode 100644 Vision.WindowCapture.Test/Vision.WindowCapture.Test.csproj diff --git a/BetterGenshinImpact.Win32/NativeMethods.txt b/BetterGenshinImpact.Win32/NativeMethods.txt index 7c7e59d0..0b5a7257 100644 --- a/BetterGenshinImpact.Win32/NativeMethods.txt +++ b/BetterGenshinImpact.Win32/NativeMethods.txt @@ -21,6 +21,10 @@ GetWindowThreadProcessId mouse_event PostMessage ReleaseDC +EnumWindows +GetWindowText +IsWindowVisible +GetWindowTextLength // COM & WinRT D3D11CreateDevice diff --git a/BetterGenshinImpact.sln b/BetterGenshinImpact.sln index 38db720a..bff2c958 100644 --- a/BetterGenshinImpact.sln +++ b/BetterGenshinImpact.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BetterGenshinImpact.Win32", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fischless.WindowCapture", "Fischless.WindowCapture\Fischless.WindowCapture.csproj", "{6B0A3D96-D88D-48DD-8112-4CD5BA5D27CE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vision.WindowCapture.Test", "Vision.WindowCapture.Test\Vision.WindowCapture.Test.csproj", "{D35CB953-C666-4E57-9A9A-3AAE5BF78402}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,14 @@ Global {6B0A3D96-D88D-48DD-8112-4CD5BA5D27CE}.Release|Any CPU.Build.0 = Release|x64 {6B0A3D96-D88D-48DD-8112-4CD5BA5D27CE}.Release|x64.ActiveCfg = Release|x64 {6B0A3D96-D88D-48DD-8112-4CD5BA5D27CE}.Release|x64.Build.0 = Release|x64 + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Debug|x64.ActiveCfg = Debug|Any CPU + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Debug|x64.Build.0 = Debug|Any CPU + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Release|Any CPU.Build.0 = Release|Any CPU + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Release|x64.ActiveCfg = Release|Any CPU + {D35CB953-C666-4E57-9A9A-3AAE5BF78402}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Vision.WindowCapture.Test/App.xaml b/Vision.WindowCapture.Test/App.xaml new file mode 100644 index 00000000..ec7a534c --- /dev/null +++ b/Vision.WindowCapture.Test/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/Vision.WindowCapture.Test/App.xaml.cs b/Vision.WindowCapture.Test/App.xaml.cs new file mode 100644 index 00000000..91116311 --- /dev/null +++ b/Vision.WindowCapture.Test/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace Vision.WindowCapture.Test +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/Vision.WindowCapture.Test/AssemblyInfo.cs b/Vision.WindowCapture.Test/AssemblyInfo.cs new file mode 100644 index 00000000..8b5504ec --- /dev/null +++ b/Vision.WindowCapture.Test/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/Vision.WindowCapture.Test/CaptureTestWindow.xaml b/Vision.WindowCapture.Test/CaptureTestWindow.xaml new file mode 100644 index 00000000..172d5941 --- /dev/null +++ b/Vision.WindowCapture.Test/CaptureTestWindow.xaml @@ -0,0 +1,12 @@ + + + + + + diff --git a/Vision.WindowCapture.Test/CaptureTestWindow.xaml.cs b/Vision.WindowCapture.Test/CaptureTestWindow.xaml.cs new file mode 100644 index 00000000..0c18c351 --- /dev/null +++ b/Vision.WindowCapture.Test/CaptureTestWindow.xaml.cs @@ -0,0 +1,69 @@ +using System; +using System.Diagnostics; +using System.Drawing; +using System.IO; +using System.Windows; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using Windows.Win32.Foundation; + +namespace Vision.WindowCapture.Test +{ + /// + /// CaptureTestWindow.xaml 的交互逻辑 + /// + public partial class CaptureTestWindow : Window + { + private IWindowCapture? _capture; + public CaptureTestWindow() + { + InitializeComponent(); + } + + public void StartCapture(IntPtr hWnd, CaptureModeEnum captureMode) + { + if (hWnd == IntPtr.Zero) + { + MessageBox.Show("请选择窗口"); + return; + } + + + _capture = WindowCaptureFactory.Create(captureMode); + _capture.Start((HWND)hWnd); + + CompositionTarget.Rendering += Loop; + } + + private void Loop(object? sender, EventArgs e) + { + var sw = new Stopwatch(); + sw.Start(); + var bitmap = _capture?.Capture(); + sw.Stop(); + Debug.WriteLine("截图耗时:" + sw.ElapsedMilliseconds); + + if (bitmap != null) + { + sw.Reset(); + sw.Start(); + DisplayCaptureResultImage.Source = ToBitmapImage(bitmap); + sw.Stop(); + Debug.WriteLine("转换耗时:" + sw.ElapsedMilliseconds); + } + } + + public static BitmapImage ToBitmapImage( Bitmap bitmap) + { + var ms = new MemoryStream(); + bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); + var image = new BitmapImage(); + image.BeginInit(); + ms.Seek(0, SeekOrigin.Begin); + image.StreamSource = ms; + image.EndInit(); + return image; + } + } + +} diff --git a/Vision.WindowCapture.Test/MainWindow.xaml b/Vision.WindowCapture.Test/MainWindow.xaml new file mode 100644 index 00000000..916ee8ba --- /dev/null +++ b/Vision.WindowCapture.Test/MainWindow.xaml @@ -0,0 +1,12 @@ + + +