fasdfsa commited on
Commit
68dd90e
·
1 Parent(s): 7dd6a04

add ScreenGrab

Browse files
imradv3.sln CHANGED
@@ -17,6 +17,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WeChatOcrCpp", "src\WeChatO
17
  EndProject
18
  Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfEditor", "src\WpfEditor\WpfEditor.csproj", "{A995A5F1-6A44-463F-9284-A3F058575206}"
19
  EndProject
 
 
 
 
20
  Global
21
  GlobalSection(SolutionConfigurationPlatforms) = preSolution
22
  Debug|Any CPU = Debug|Any CPU
@@ -111,6 +115,30 @@ Global
111
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.Build.0 = Debug|x64
112
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.ActiveCfg = Release|Any CPU
113
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.Build.0 = Release|Any CPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  EndGlobalSection
115
  GlobalSection(SolutionProperties) = preSolution
116
  HideSolutionNode = FALSE
 
17
  EndProject
18
  Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfEditor", "src\WpfEditor\WpfEditor.csproj", "{A995A5F1-6A44-463F-9284-A3F058575206}"
19
  EndProject
20
+ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScreenGrab", "src\ScreenGrab\ScreenGrab.csproj", "{998A5C87-48A5-B8CF-4435-452B526C6235}"
21
+ EndProject
22
+ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScreenGrab.Sample", "src\ScreenGrab.Sample\ScreenGrab.Sample.csproj", "{34691787-AE69-7042-FB4F-9EE5535A4D5A}"
23
+ EndProject
24
  Global
25
  GlobalSection(SolutionConfigurationPlatforms) = preSolution
26
  Debug|Any CPU = Debug|Any CPU
 
115
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.Build.0 = Debug|x64
116
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.ActiveCfg = Release|Any CPU
117
  {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.Build.0 = Release|Any CPU
118
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Debug|Any CPU.Build.0 = Debug|Any CPU
120
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Debug|x64.ActiveCfg = Debug|Any CPU
121
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Debug|x64.Build.0 = Debug|Any CPU
122
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Debug|x86.ActiveCfg = Debug|Any CPU
123
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Debug|x86.Build.0 = Debug|Any CPU
124
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Release|Any CPU.ActiveCfg = Release|Any CPU
125
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Release|Any CPU.Build.0 = Release|Any CPU
126
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Release|x64.ActiveCfg = Release|Any CPU
127
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Release|x64.Build.0 = Release|Any CPU
128
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Release|x86.ActiveCfg = Release|Any CPU
129
+ {998A5C87-48A5-B8CF-4435-452B526C6235}.Release|x86.Build.0 = Release|Any CPU
130
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
131
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
132
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Debug|x64.ActiveCfg = Debug|Any CPU
133
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Debug|x64.Build.0 = Debug|Any CPU
134
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Debug|x86.ActiveCfg = Debug|Any CPU
135
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Debug|x86.Build.0 = Debug|Any CPU
136
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
137
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Release|Any CPU.Build.0 = Release|Any CPU
138
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Release|x64.ActiveCfg = Release|Any CPU
139
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Release|x64.Build.0 = Release|Any CPU
140
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Release|x86.ActiveCfg = Release|Any CPU
141
+ {34691787-AE69-7042-FB4F-9EE5535A4D5A}.Release|x86.Build.0 = Release|Any CPU
142
  EndGlobalSection
143
  GlobalSection(SolutionProperties) = preSolution
144
  HideSolutionNode = FALSE
reame.txt CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  0001.bmp 这张图 "第一部分 基础知识" 因为是反色的,所以 wechat OCR 没有识别出来,可以做为区分 ali ocr 和 wechat ocr api 的特征
2
 
3
 
 
1
+
2
+ see github\echodict\cut\wpf\ScreenGrab\ScreenGrabber.cs
3
+
4
+
5
  0001.bmp 这张图 "第一部分 基础知识" 因为是反色的,所以 wechat OCR 没有识别出来,可以做为区分 ali ocr 和 wechat ocr api 的特征
6
 
7
 
src/ScreenGrab.Sample/App.xaml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Application x:Class="ScreenGrab.Sample.App"
2
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
+ xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
5
+ xmlns:local="clr-namespace:ScreenGrab.Sample"
6
+ StartupUri="MainWindow.xaml">
7
+ <Application.Resources>
8
+ <ResourceDictionary>
9
+ <ResourceDictionary.MergedDictionaries>
10
+ <ui:ThemesDictionary Theme="Dark" />
11
+ <ui:ControlsDictionary />
12
+ </ResourceDictionary.MergedDictionaries>
13
+ </ResourceDictionary>
14
+ </Application.Resources>
15
+ </Application>
src/ScreenGrab.Sample/App.xaml.cs ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Configuration;
2
+ using System.Data;
3
+ using System.Windows;
4
+
5
+ namespace ScreenGrab.Sample
6
+ {
7
+ /// <summary>
8
+ /// Interaction logic for App.xaml
9
+ /// </summary>
10
+ public partial class App : Application
11
+ {
12
+ }
13
+
14
+ }
src/ScreenGrab.Sample/AssemblyInfo.cs ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Windows;
2
+
3
+ [assembly: ThemeInfo(
4
+ ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5
+ //(used if a resource is not found in the page,
6
+ // or application resource dictionaries)
7
+ ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8
+ //(used if a resource is not found in the page,
9
+ // app, or any theme specific resource dictionaries)
10
+ )]
src/ScreenGrab.Sample/MainWindow.xaml ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <ui:FluentWindow x:Class="ScreenGrab.Sample.MainWindow"
2
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6
+ xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
7
+ xmlns:tray="http://schemas.lepo.co/wpfui/2022/xaml/tray"
8
+ mc:Ignorable="d"
9
+ Loaded="MainWindow_OnLoaded"
10
+ Unloaded="MainWindow_OnUnloaded"
11
+ WindowStartupLocation="CenterScreen"
12
+ Title="ScreenGrab Sample" Height="450" Width="800">
13
+ <Grid>
14
+ <Grid.RowDefinitions>
15
+ <RowDefinition Height="38" />
16
+ <RowDefinition Height="Auto" />
17
+ <RowDefinition Height="*" />
18
+ </Grid.RowDefinitions>
19
+ <ui:TitleBar Title="ScreenGrab Sample" />
20
+ <tray:NotifyIcon Grid.Row="0"
21
+ FocusOnLeftClick="True"
22
+ Icon="nuget.png"
23
+ LeftClick="NotifyIcon_OnLeftClick"
24
+ MenuOnRightClick="True"
25
+ TooltipText="ScreenGrab.Sample">
26
+ <tray:NotifyIcon.Menu>
27
+ <ContextMenu>
28
+ <MenuItem Header="Grab"
29
+ Click="Capture_Click"
30
+ InputGestureText="Win+Shift+A">
31
+ <MenuItem.Icon>
32
+ <ui:SymbolIcon Symbol="Screenshot24" />
33
+ </MenuItem.Icon>
34
+ </MenuItem>
35
+ <Separator />
36
+ <MenuItem Header="Exit"
37
+ Click="MenuItem_OnClick">
38
+ <MenuItem.Icon>
39
+ <ui:SymbolIcon Symbol="PictureInPictureExit24" />
40
+ </MenuItem.Icon>
41
+ </MenuItem>
42
+ </ContextMenu>
43
+ </tray:NotifyIcon.Menu>
44
+ </tray:NotifyIcon>
45
+ <ui:Card Grid.Row="1" Margin="8">
46
+ <StackPanel Orientation="Horizontal">
47
+ <Button Margin="2 0" Content="Capture(Win+Shift+A)" Click="Capture_Click" />
48
+ <Button Margin="2 0" Content="Clean" Click="Clean_Click" />
49
+ <CheckBox Margin="2 0" Content="Auxiliary" Name="AuxiliaryCb" />
50
+ </StackPanel>
51
+ </ui:Card>
52
+ <ui:DynamicScrollViewer Grid.Row="2" Margin="8">
53
+ <Image x:Name="Img" />
54
+ </ui:DynamicScrollViewer>
55
+ </Grid>
56
+ </ui:FluentWindow>
src/ScreenGrab.Sample/MainWindow.xaml.cs ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using NHotkey;
2
+ using NHotkey.Wpf;
3
+ using ScreenGrab.Extensions;
4
+ using System.ComponentModel;
5
+ using System.Drawing;
6
+ using System.Windows;
7
+ using System.Windows.Input;
8
+ using Wpf.Ui.Tray.Controls;
9
+
10
+ namespace ScreenGrab.Sample;
11
+
12
+ public partial class MainWindow
13
+ {
14
+ public MainWindow()
15
+ {
16
+ InitializeComponent();
17
+ }
18
+
19
+ private void Capture()
20
+ {
21
+ Clean();
22
+
23
+ //ScreenGrabber.OnCaptured = bitmap =>
24
+ //{
25
+ // Img.Source = bitmap.ToImageSource();
26
+ // if (WindowState != WindowState.Normal)
27
+ // WindowState = WindowState.Normal;
28
+ // if (!IsVisible)
29
+ // Show();
30
+ // Activate();
31
+ //};
32
+ //ScreenGrabber.Capture(AuxiliaryCb.IsChecked ?? false);
33
+
34
+ if (ScreenGrabber.IsCapturing) return;
35
+ // https://github.com/wuqinchao/ScreenCapturer
36
+ // 替代品
37
+ ScreenGrabber.OnCaptured = async bitmap => await ScreenshotCallbackAsync(bitmap, null);
38
+ ScreenGrabber.Capture(true);
39
+ }
40
+
41
+ internal async Task ScreenshotCallbackAsync(Bitmap? bitmap, CancellationToken? token = null)
42
+ {
43
+ if (bitmap == null)
44
+ {
45
+ //ShowBalloonTip("图像不存在");
46
+ return;
47
+ }
48
+ }
49
+
50
+ private void Capture(object? sender, HotkeyEventArgs e)
51
+ {
52
+ Capture();
53
+ }
54
+
55
+ private void Capture_Click(object sender, RoutedEventArgs e)
56
+ {
57
+ Capture();
58
+ }
59
+
60
+ private void Clean_Click(object sender, RoutedEventArgs e)
61
+ {
62
+ Clean();
63
+ }
64
+
65
+ private void Clean()
66
+ {
67
+ Img.Source?.Freeze();
68
+ Img.Source = null;
69
+ GC.Collect();
70
+ }
71
+
72
+ private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
73
+ {
74
+ HotkeyManager.Current.AddOrReplace("Capture", Key.A, ModifierKeys.Windows | ModifierKeys.Shift, Capture);
75
+ }
76
+
77
+ private void MainWindow_OnUnloaded(object sender, RoutedEventArgs e)
78
+ {
79
+ HotkeyManager.Current.Remove("Capture");
80
+ }
81
+
82
+ protected override void OnClosing(CancelEventArgs e)
83
+ {
84
+ e.Cancel = true;
85
+ Hide();
86
+ }
87
+
88
+ private void NotifyIcon_OnLeftClick(NotifyIcon sender, RoutedEventArgs e)
89
+ {
90
+ Show();
91
+ Activate();
92
+ }
93
+
94
+ private void MenuItem_OnClick(object sender, RoutedEventArgs e)
95
+ {
96
+ Application.Current.Shutdown();
97
+ }
98
+ }
src/ScreenGrab.Sample/ScreenGrab.Sample.csproj ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Project Sdk="Microsoft.NET.Sdk">
2
+
3
+ <PropertyGroup>
4
+ <OutputType>WinExe</OutputType>
5
+ <TargetFramework>net8.0-windows</TargetFramework>
6
+ <Nullable>enable</Nullable>
7
+ <ImplicitUsings>enable</ImplicitUsings>
8
+ <UseWPF>true</UseWPF>
9
+ </PropertyGroup>
10
+
11
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
12
+ <Optimize>False</Optimize>
13
+ </PropertyGroup>
14
+
15
+ <ItemGroup>
16
+ <None Remove="nuget.png" />
17
+ </ItemGroup>
18
+
19
+ <ItemGroup>
20
+ <PackageReference Include="NHotkey" Version="3.0.0" />
21
+ <PackageReference Include="NHotkey.Wpf" Version="3.0.0" />
22
+ <PackageReference Include="WPF-UI" Version="3.0.5" />
23
+ <PackageReference Include="WPF-UI.Tray" Version="3.0.5" />
24
+ </ItemGroup>
25
+
26
+ <ItemGroup>
27
+ <ProjectReference Include="..\ScreenGrab\ScreenGrab.csproj" />
28
+ </ItemGroup>
29
+
30
+ <ItemGroup>
31
+ <Resource Include="nuget.png" />
32
+ </ItemGroup>
33
+
34
+ </Project>
src/ScreenGrab.Sample/nuget.png ADDED

Git LFS Details

  • SHA256: 19684db9157cc826c3d148c36788eb13174c87a066c4e476509993c3482aa6e7
  • Pointer size: 129 Bytes
  • Size of remote file: 2.95 kB
src/ScreenGrab/AutoGrid.cs ADDED
@@ -0,0 +1,529 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.ComponentModel;
2
+ using System.Windows;
3
+ using System.Windows.Controls;
4
+ using ScreenGrab.Extensions;
5
+
6
+ namespace ScreenGrab;
7
+
8
+ /// <summary>
9
+ /// Source <see href="https://github.com/budul100/WpfAutoGrid.Core" />
10
+ /// Defines a flexible grid area that consists of columns and rows.
11
+ /// Depending on the orientation, either the rows or the columns are auto-generated,
12
+ /// and the children's position is set according to their index.
13
+ /// Partially based on work at http://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/
14
+ /// </summary>
15
+ public class AutoGrid : Grid
16
+ {
17
+ #region Protected Methods
18
+
19
+ /// <summary>
20
+ /// Measures the children of a <see cref="T:System.Windows.Controls.Grid" /> in anticipation of arranging them during
21
+ /// the <see cref="M:ArrangeOverride" /> pass.
22
+ /// </summary>
23
+ /// <param name="constraint">Indicates an upper limit size that should not be exceeded.</param>
24
+ /// <returns>
25
+ /// <see cref="Size" /> that represents the required size to arrange child content.
26
+ /// </returns>
27
+ protected override Size MeasureOverride(Size constraint)
28
+ {
29
+ PerformLayout();
30
+ return base.MeasureOverride(constraint);
31
+ }
32
+
33
+ #endregion Protected Methods
34
+
35
+ #region Public Fields
36
+
37
+ public static readonly DependencyProperty ChildHorizontalAlignmentProperty = DependencyProperty.Register(
38
+ "ChildHorizontalAlignment",
39
+ typeof(HorizontalAlignment?),
40
+ typeof(AutoGrid),
41
+ new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure,
42
+ OnChildHorizontalAlignmentChanged));
43
+
44
+ public static readonly DependencyProperty ChildMarginProperty = DependencyProperty.Register(
45
+ "ChildMargin",
46
+ typeof(Thickness?),
47
+ typeof(AutoGrid),
48
+ new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure, OnChildMarginChanged));
49
+
50
+ public static readonly DependencyProperty ChildVerticalAlignmentProperty = DependencyProperty.Register(
51
+ "ChildVerticalAlignment",
52
+ typeof(VerticalAlignment?),
53
+ typeof(AutoGrid),
54
+ new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure,
55
+ OnChildVerticalAlignmentChanged));
56
+
57
+ public static readonly DependencyProperty ColumnCountProperty = DependencyProperty.RegisterAttached(
58
+ "ColumnCount",
59
+ typeof(int),
60
+ typeof(AutoGrid),
61
+ new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, ColumnCountChanged));
62
+
63
+ public static readonly DependencyProperty ColumnsProperty = DependencyProperty.RegisterAttached(
64
+ "Columns",
65
+ typeof(string),
66
+ typeof(AutoGrid),
67
+ new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, ColumnsChanged));
68
+
69
+ public static readonly DependencyProperty ColumnWidthProperty = DependencyProperty.RegisterAttached(
70
+ "ColumnWidth",
71
+ typeof(GridLength),
72
+ typeof(AutoGrid),
73
+ new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure,
74
+ FixedColumnWidthChanged));
75
+
76
+ public static readonly DependencyProperty IsAutoIndexingProperty = DependencyProperty.Register(
77
+ "IsAutoIndexing",
78
+ typeof(bool),
79
+ typeof(AutoGrid),
80
+ new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsMeasure));
81
+
82
+ public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
83
+ "Orientation",
84
+ typeof(Orientation),
85
+ typeof(AutoGrid),
86
+ new FrameworkPropertyMetadata(Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsMeasure));
87
+
88
+ public static readonly DependencyProperty RowCountProperty = DependencyProperty.RegisterAttached(
89
+ "RowCount",
90
+ typeof(int),
91
+ typeof(AutoGrid),
92
+ new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, RowCountChanged));
93
+
94
+ public static readonly DependencyProperty RowHeightProperty = DependencyProperty.RegisterAttached(
95
+ "RowHeight",
96
+ typeof(GridLength),
97
+ typeof(AutoGrid),
98
+ new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure,
99
+ FixedRowHeightChanged));
100
+
101
+ public static readonly DependencyProperty RowsProperty = DependencyProperty.RegisterAttached(
102
+ "Rows",
103
+ typeof(string),
104
+ typeof(AutoGrid),
105
+ new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, RowsChanged));
106
+
107
+ #endregion Public Fields
108
+
109
+ #region Public Properties
110
+
111
+ /// <summary>
112
+ /// Gets or sets the child horizontal alignment.
113
+ /// </summary>
114
+ /// <value>The child horizontal alignment.</value>
115
+ [Category("Layout")]
116
+ [Description("Presets the horizontal alignment of all child controls")]
117
+ public HorizontalAlignment? ChildHorizontalAlignment
118
+ {
119
+ get => (HorizontalAlignment?)GetValue(ChildHorizontalAlignmentProperty);
120
+ set => SetValue(ChildHorizontalAlignmentProperty, value);
121
+ }
122
+
123
+ /// <summary>
124
+ /// Gets or sets the child margin.
125
+ /// </summary>
126
+ /// <value>The child margin.</value>
127
+ [Category("Layout")]
128
+ [Description("Presets the margin of all child controls")]
129
+ public Thickness? ChildMargin
130
+ {
131
+ get => (Thickness?)GetValue(ChildMarginProperty);
132
+ set => SetValue(ChildMarginProperty, value);
133
+ }
134
+
135
+ /// <summary>
136
+ /// Gets or sets the child vertical alignment.
137
+ /// </summary>
138
+ /// <value>The child vertical alignment.</value>
139
+ [Category("Layout")]
140
+ [Description("Presets the vertical alignment of all child controls")]
141
+ public VerticalAlignment? ChildVerticalAlignment
142
+ {
143
+ get => (VerticalAlignment?)GetValue(ChildVerticalAlignmentProperty);
144
+ set => SetValue(ChildVerticalAlignmentProperty, value);
145
+ }
146
+
147
+ /// <summary>
148
+ /// Gets or sets the column count
149
+ /// </summary>
150
+ [Category("Layout")]
151
+ [Description("Defines a set number of columns")]
152
+ public int ColumnCount
153
+ {
154
+ get => (int)GetValue(ColumnCountProperty);
155
+ set => SetValue(ColumnCountProperty, value);
156
+ }
157
+
158
+ /// <summary>
159
+ /// Gets or sets the columns
160
+ /// </summary>
161
+ [Category("Layout")]
162
+ [Description("Defines all columns using comma separated grid length notation")]
163
+ public string Columns
164
+ {
165
+ get => (string)GetValue(ColumnsProperty);
166
+ set => SetValue(ColumnsProperty, value);
167
+ }
168
+
169
+ /// <summary>
170
+ /// Gets or sets the fixed column width
171
+ /// </summary>
172
+ [Category("Layout")]
173
+ [Description("Presets the width of all columns set using the ColumnCount property")]
174
+ public GridLength ColumnWidth
175
+ {
176
+ get => (GridLength)GetValue(ColumnWidthProperty);
177
+ set => SetValue(ColumnWidthProperty, value);
178
+ }
179
+
180
+ /// <summary>
181
+ /// Gets or sets a value indicating whether the children are automatically indexed.
182
+ /// <remarks>
183
+ /// The default is <c>true</c>.
184
+ /// Note that if children are already indexed, setting this property to <c>false</c> will not remove their indices.
185
+ /// </remarks>
186
+ /// </summary>
187
+ [Category("Layout")]
188
+ [Description("Set to false to disable the auto layout functionality")]
189
+ public bool IsAutoIndexing
190
+ {
191
+ get => (bool)GetValue(IsAutoIndexingProperty);
192
+ set => SetValue(IsAutoIndexingProperty, value);
193
+ }
194
+
195
+ /// <summary>
196
+ /// Gets or sets the orientation.
197
+ /// <remarks>The default is Vertical.</remarks>
198
+ /// </summary>
199
+ /// <value>The orientation.</value>
200
+ [Category("Layout")]
201
+ [Description(
202
+ "Defines the directionality of the autolayout. Use vertical for a column first layout, horizontal for a row first layout.")]
203
+ public Orientation Orientation
204
+ {
205
+ get => (Orientation)GetValue(OrientationProperty);
206
+ set => SetValue(OrientationProperty, value);
207
+ }
208
+
209
+ /// <summary>
210
+ /// Gets or sets the number of rows
211
+ /// </summary>
212
+ [Category("Layout")]
213
+ [Description("Defines a set number of rows")]
214
+ public int RowCount
215
+ {
216
+ get => (int)GetValue(RowCountProperty);
217
+ set => SetValue(RowCountProperty, value);
218
+ }
219
+
220
+ /// <summary>
221
+ /// Gets or sets the fixed row height
222
+ /// </summary>
223
+ [Category("Layout")]
224
+ [Description("Presets the height of all rows set using the RowCount property")]
225
+ public GridLength RowHeight
226
+ {
227
+ get => (GridLength)GetValue(RowHeightProperty);
228
+ set => SetValue(RowHeightProperty, value);
229
+ }
230
+
231
+ /// <summary>
232
+ /// Gets or sets the rows
233
+ /// </summary>
234
+ [Category("Layout")]
235
+ [Description("Defines all rows using comma separated grid length notation")]
236
+ public string Rows
237
+ {
238
+ get => (string)GetValue(RowsProperty);
239
+ set => SetValue(RowsProperty, value);
240
+ }
241
+
242
+ #endregion Public Properties
243
+
244
+ #region Public Methods
245
+
246
+ /// <summary>
247
+ /// Handles the column count changed event
248
+ /// </summary>
249
+ public static void ColumnCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
250
+ {
251
+ if ((int)e.NewValue < 0)
252
+ return;
253
+
254
+ if (d is not AutoGrid grid) return;
255
+ //var grid = d as AutoGrid;
256
+
257
+ // look for an existing column definition for the height
258
+ var width = GridLength.Auto;
259
+ if (grid.ColumnDefinitions.Count > 0)
260
+ width = grid.ColumnDefinitions[0].Width;
261
+
262
+ // clear and rebuild
263
+ grid.ColumnDefinitions.Clear();
264
+ for (var i = 0; i < (int)e.NewValue; i++)
265
+ grid.ColumnDefinitions.Add(
266
+ new ColumnDefinition { Width = width });
267
+ }
268
+
269
+ /// <summary>
270
+ /// Handle the columns changed event
271
+ /// </summary>
272
+ public static void ColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
273
+ {
274
+ if (string.IsNullOrEmpty((string)e.NewValue))
275
+ return;
276
+
277
+ if (d is not AutoGrid grid) return;
278
+ //var grid = d as AutoGrid;
279
+ grid.ColumnDefinitions.Clear();
280
+
281
+ var defs = Parse((string)e.NewValue);
282
+ foreach (var def in defs)
283
+ grid.ColumnDefinitions.Add(new ColumnDefinition { Width = def });
284
+ }
285
+
286
+ /// <summary>
287
+ /// Handle the fixed column width changed event
288
+ /// </summary>
289
+ public static void FixedColumnWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
290
+ {
291
+ if (d is not AutoGrid grid) return;
292
+ //var grid = d as AutoGrid;
293
+
294
+ // add a default column if missing
295
+ if (grid.ColumnDefinitions.Count == 0)
296
+ grid.ColumnDefinitions.Add(new ColumnDefinition());
297
+
298
+ // set all existing columns to this width
299
+ for (var i = 0; i < grid.ColumnDefinitions.Count; i++)
300
+ grid.ColumnDefinitions[i].Width = (GridLength)e.NewValue;
301
+ }
302
+
303
+ /// <summary>
304
+ /// Handle the fixed row height changed event
305
+ /// </summary>
306
+ public static void FixedRowHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
307
+ {
308
+ if (d is not AutoGrid grid) return;
309
+ //var grid = d as AutoGrid;
310
+
311
+ // add a default row if missing
312
+ if (grid.RowDefinitions.Count == 0)
313
+ grid.RowDefinitions.Add(new RowDefinition());
314
+
315
+ // set all existing rows to this height
316
+ for (var i = 0; i < grid.RowDefinitions.Count; i++)
317
+ grid.RowDefinitions[i].Height = (GridLength)e.NewValue;
318
+ }
319
+
320
+ /// <summary>
321
+ /// Parse an array of grid lengths from comma delim text
322
+ /// </summary>
323
+ public static GridLength[] Parse(string text)
324
+ {
325
+ var tokens = text.Split(',');
326
+ var definitions = new GridLength[tokens.Length];
327
+ for (var i = 0; i < tokens.Length; i++)
328
+ {
329
+ var str = tokens[i];
330
+ double value;
331
+
332
+ // ratio
333
+ if (str.Contains('*'))
334
+ {
335
+ if (!double.TryParse(str.Replace("*", ""), out value))
336
+ value = 1.0;
337
+
338
+ definitions[i] = new GridLength(value, GridUnitType.Star);
339
+ continue;
340
+ }
341
+
342
+ // pixels
343
+ if (double.TryParse(str, out value))
344
+ {
345
+ definitions[i] = new GridLength(value);
346
+ continue;
347
+ }
348
+
349
+ // auto
350
+ definitions[i] = GridLength.Auto;
351
+ }
352
+
353
+ return definitions;
354
+ }
355
+
356
+ /// <summary>
357
+ /// Handles the row count changed event
358
+ /// </summary>
359
+ public static void RowCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
360
+ {
361
+ if ((int)e.NewValue < 0)
362
+ return;
363
+
364
+ if (d is not AutoGrid grid) return;
365
+ //var grid = d as AutoGrid;
366
+
367
+ // look for an existing row to get the height
368
+ var height = GridLength.Auto;
369
+ if (grid.RowDefinitions.Count > 0)
370
+ height = grid.RowDefinitions[0].Height;
371
+
372
+ // clear and rebuild
373
+ grid.RowDefinitions.Clear();
374
+ for (var i = 0; i < (int)e.NewValue; i++)
375
+ grid.RowDefinitions.Add(
376
+ new RowDefinition { Height = height });
377
+ }
378
+
379
+ /// <summary>
380
+ /// Handle the rows changed event
381
+ /// </summary>
382
+ public static void RowsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
383
+ {
384
+ if (string.IsNullOrEmpty((string)e.NewValue))
385
+ return;
386
+
387
+ if (d is not AutoGrid grid) return;
388
+ //var grid = d as AutoGrid;
389
+ grid.RowDefinitions.Clear();
390
+
391
+ var defs = Parse((string)e.NewValue);
392
+ foreach (var def in defs)
393
+ grid.RowDefinitions.Add(new RowDefinition { Height = def });
394
+ }
395
+
396
+ #endregion Public Methods
397
+
398
+ #region Private Methods
399
+
400
+ /// <summary>
401
+ /// Called when [child horizontal alignment changed].
402
+ /// </summary>
403
+ private static void OnChildHorizontalAlignmentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
404
+ {
405
+ if (d is not AutoGrid grid) return;
406
+ //var grid = d as AutoGrid;
407
+ foreach (UIElement child in grid.Children)
408
+ if (grid.ChildHorizontalAlignment.HasValue)
409
+ child.SetValue(HorizontalAlignmentProperty, grid.ChildHorizontalAlignment);
410
+ else
411
+ child.SetValue(HorizontalAlignmentProperty, DependencyProperty.UnsetValue);
412
+ }
413
+
414
+ /// <summary>
415
+ /// Called when [child layout changed].
416
+ /// </summary>
417
+ private static void OnChildMarginChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
418
+ {
419
+ if (d is not AutoGrid grid) return;
420
+ //var grid = d as AutoGrid;
421
+ foreach (UIElement child in grid.Children)
422
+ if (grid.ChildMargin.HasValue)
423
+ child.SetValue(MarginProperty, grid.ChildMargin);
424
+ else
425
+ child.SetValue(MarginProperty, DependencyProperty.UnsetValue);
426
+ }
427
+
428
+ /// <summary>
429
+ /// Called when [child vertical alignment changed].
430
+ /// </summary>
431
+ private static void OnChildVerticalAlignmentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
432
+ {
433
+ if (d is not AutoGrid grid) return;
434
+ //var grid = d as AutoGrid;
435
+ foreach (UIElement child in grid.Children)
436
+ if (grid.ChildVerticalAlignment.HasValue)
437
+ child.SetValue(VerticalAlignmentProperty, grid.ChildVerticalAlignment);
438
+ else
439
+ child.SetValue(VerticalAlignmentProperty, DependencyProperty.UnsetValue);
440
+ }
441
+
442
+ ///// <summary>
443
+ ///// Handled the redraw properties changed event
444
+ ///// </summary>
445
+ //private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
446
+ //{ }
447
+
448
+ /// <summary>
449
+ /// Apply child margins and layout effects such as alignment
450
+ /// </summary>
451
+ private void ApplyChildLayout(UIElement child)
452
+ {
453
+ if (ChildMargin != null) child.SetIfDefault(MarginProperty, ChildMargin.Value);
454
+ if (ChildHorizontalAlignment != null)
455
+ child.SetIfDefault(HorizontalAlignmentProperty, ChildHorizontalAlignment.Value);
456
+ if (ChildVerticalAlignment != null) child.SetIfDefault(VerticalAlignmentProperty, ChildVerticalAlignment.Value);
457
+ }
458
+
459
+ /// <summary>
460
+ /// Clamp a value to its maximum.
461
+ /// </summary>
462
+ private int Clamp(int value, int max)
463
+ {
464
+ return value > max ? max : value;
465
+ }
466
+
467
+ /// <summary>
468
+ /// Perform the grid layout of row and column indexes
469
+ /// </summary>
470
+ private void PerformLayout()
471
+ {
472
+ var fillRowFirst = Orientation == Orientation.Horizontal;
473
+ var rowCount = RowDefinitions.Count;
474
+ var colCount = ColumnDefinitions.Count;
475
+
476
+ if (rowCount == 0 || colCount == 0)
477
+ return;
478
+
479
+ var position = 0;
480
+ var skip = new bool[rowCount, colCount];
481
+ foreach (UIElement child in Children)
482
+ {
483
+ var childIsCollapsed = child.Visibility == Visibility.Collapsed;
484
+ if (IsAutoIndexing && !childIsCollapsed)
485
+ {
486
+ if (fillRowFirst)
487
+ {
488
+ var row = Clamp(position / colCount, rowCount - 1);
489
+ var col = Clamp(position % colCount, colCount - 1);
490
+ if (skip[row, col])
491
+ {
492
+ position++;
493
+ row = position / colCount;
494
+ col = position % colCount;
495
+ }
496
+
497
+ SetRow(child, row);
498
+ SetColumn(child, col);
499
+ position += GetColumnSpan(child);
500
+
501
+ var offset = GetRowSpan(child) - 1;
502
+ while (offset > 0) skip[row + offset--, col] = true;
503
+ }
504
+ else
505
+ {
506
+ var row = Clamp(position % rowCount, rowCount - 1);
507
+ var col = Clamp(position / rowCount, colCount - 1);
508
+ if (skip[row, col])
509
+ {
510
+ position++;
511
+ row = position % rowCount;
512
+ col = position / rowCount;
513
+ }
514
+
515
+ SetRow(child, row);
516
+ SetColumn(child, col);
517
+ position += GetRowSpan(child);
518
+
519
+ var offset = GetColumnSpan(child) - 1;
520
+ while (offset > 0) skip[row, col + offset--] = true;
521
+ }
522
+ }
523
+
524
+ ApplyChildLayout(child);
525
+ }
526
+ }
527
+
528
+ #endregion Private Methods
529
+ }
src/ScreenGrab/Extensions/DapploExtensions.cs ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Windows;
2
+ using Dapplo.Windows.User32;
3
+
4
+ namespace ScreenGrab.Extensions;
5
+
6
+ public static class DapploExtensions
7
+ {
8
+ public static Point ScaledCenterPoint(this DisplayInfo displayInfo)
9
+ {
10
+ Rect displayRect = displayInfo.Bounds;
11
+ NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out var scaleFactor);
12
+ var scaleFraction = scaleFactor / 100.0;
13
+ var rawCenter = displayRect.CenterPoint();
14
+ Point displayScaledCenterPoint = new(rawCenter.X / scaleFraction, rawCenter.Y / scaleFraction);
15
+ return displayScaledCenterPoint;
16
+ }
17
+
18
+ public static Rect ScaledBounds(this DisplayInfo displayInfo)
19
+ {
20
+ Rect displayRect = displayInfo.Bounds;
21
+ NativeMethods.GetScaleFactorForMonitor(displayInfo.MonitorHandle, out var scaleFactor);
22
+ var scaleFraction = scaleFactor / 100.0;
23
+
24
+ // Scale size and position
25
+ Rect scaledBounds = new(
26
+ displayRect.X / scaleFraction,
27
+ displayRect.Y / scaleFraction,
28
+ displayRect.Width / scaleFraction,
29
+ displayRect.Height / scaleFraction);
30
+ return scaledBounds;
31
+ }
32
+ }
src/ScreenGrab/Extensions/DependencyExtensions.cs ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Windows;
2
+
3
+ namespace ScreenGrab.Extensions;
4
+
5
+ public static class DependencyExtensions
6
+ {
7
+ #region Public Methods
8
+
9
+ /// <summary>
10
+ /// Sets the value of the <paramref name="property" /> only if it hasn't been explicitly set.
11
+ /// </summary>
12
+ public static bool SetIfDefault<T>(this DependencyObject o, DependencyProperty property, T value)
13
+ {
14
+ if (DependencyPropertyHelper.GetValueSource(o, property).BaseValueSource == BaseValueSource.Default)
15
+ {
16
+ o.SetValue(property, value);
17
+
18
+ return true;
19
+ }
20
+
21
+ return false;
22
+ }
23
+
24
+ #endregion Public Methods
25
+ }
src/ScreenGrab/Extensions/ImageExtensions.cs ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Drawing;
2
+ using System.Drawing.Imaging;
3
+ using System.IO;
4
+ using System.Windows;
5
+ using System.Windows.Media;
6
+ using System.Windows.Media.Imaging;
7
+ using ScreenGrab.Utilities;
8
+ using PixelFormat = System.Drawing.Imaging.PixelFormat;
9
+
10
+ namespace ScreenGrab.Extensions;
11
+
12
+ public static class ImageExtensions
13
+ {
14
+ public static ImageSource? GetWindowBoundsImage(this Window passedWindow)
15
+ {
16
+ var bmp = passedWindow.GetWindowsBoundsBitmap();
17
+ return bmp.ToImageSource();
18
+ }
19
+
20
+ public static Tuple<double, double> GetWidthHeight(this Window window)
21
+ {
22
+ var dpi = VisualTreeHelper.GetDpi(window);
23
+ return new Tuple<double, double>(window.ActualWidth * dpi.DpiScaleX, window.ActualHeight * dpi.DpiScaleY);
24
+ }
25
+
26
+ public static Bitmap GetWindowsBoundsBitmap(this Window passedWindow)
27
+ {
28
+ var (windowWidth, windowHeight) = passedWindow.GetWidthHeight();
29
+
30
+ var absPosPoint = passedWindow.GetAbsolutePosition();
31
+
32
+ var thisCorrectedLeft = (int)absPosPoint.X;
33
+ var thisCorrectedTop = (int)absPosPoint.Y;
34
+
35
+ Bitmap bmp = new((int)windowWidth, (int)windowHeight, PixelFormat.Format32bppArgb);
36
+ using var g = Graphics.FromImage(bmp);
37
+
38
+ g.CopyFromScreen(thisCorrectedLeft, thisCorrectedTop, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
39
+ return bmp;
40
+ }
41
+
42
+ public static BitmapImage? ToImageSource(this Bitmap? bitmap)
43
+ {
44
+ if (bitmap == null) return default;
45
+ using MemoryStream memory = new();
46
+ using WrappingStream wrapper = new(memory);
47
+
48
+ bitmap.Save(wrapper, ImageFormat.Bmp);
49
+ wrapper.Position = 0;
50
+ BitmapImage bitmapImage = new();
51
+ bitmapImage.BeginInit();
52
+ bitmapImage.StreamSource = wrapper;
53
+ bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
54
+ bitmapImage.EndInit();
55
+ bitmapImage.StreamSource = null;
56
+ bitmapImage.Freeze();
57
+
58
+ memory.Flush();
59
+ wrapper.Flush();
60
+
61
+ return bitmapImage;
62
+ }
63
+
64
+ public static Bitmap GetRegionOfScreenAsBitmap(this Rectangle region)
65
+ {
66
+ Bitmap bmp = new(region.Width, region.Height, PixelFormat.Format32bppArgb);
67
+ using var g = Graphics.FromImage(bmp);
68
+
69
+ g.CopyFromScreen(region.Left, region.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
70
+ bmp = bmp.PadImage();
71
+
72
+ return bmp;
73
+ }
74
+
75
+ public static Bitmap PadImage(this Bitmap image, int minW = 64, int minH = 64)
76
+ {
77
+ if (image.Height >= minH && image.Width >= minW)
78
+ return image;
79
+
80
+ var width = Math.Max(image.Width + 16, minW + 16);
81
+ var height = Math.Max(image.Height + 16, minH + 16);
82
+
83
+ // Create a compatible bitmap
84
+ Bitmap destination = new(width, height, image.PixelFormat);
85
+ using var gd = Graphics.FromImage(destination);
86
+
87
+ gd.Clear(image.GetPixel(0, 0));
88
+ gd.DrawImageUnscaled(image, 8, 8);
89
+
90
+ return destination;
91
+ }
92
+ }
src/ScreenGrab/Extensions/ShapeExtensions.cs ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Drawing;
2
+ using System.Windows;
3
+ using Point = System.Windows.Point;
4
+
5
+ namespace ScreenGrab.Extensions;
6
+
7
+ public static class ShapeExtensions
8
+ {
9
+ public static Rect AsRect(this Rectangle rectangle)
10
+ {
11
+ return new Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
12
+ }
13
+
14
+ public static Rectangle AsRectangle(this Rect rect)
15
+ {
16
+ return new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);
17
+ }
18
+
19
+ public static Rect GetScaledDownByDpi(this Rect rect, DpiScale dpi)
20
+ {
21
+ return new Rect(rect.X / dpi.DpiScaleX,
22
+ rect.Y / dpi.DpiScaleY,
23
+ rect.Width / dpi.DpiScaleX,
24
+ rect.Height / dpi.DpiScaleY);
25
+ }
26
+
27
+ public static Rect GetScaledUpByDpi(this Rect rect, DpiScale dpi)
28
+ {
29
+ return new Rect(rect.X * dpi.DpiScaleX,
30
+ rect.Y * dpi.DpiScaleY,
31
+ rect.Width * dpi.DpiScaleX,
32
+ rect.Height * dpi.DpiScaleY);
33
+ }
34
+
35
+ public static Rect GetScaledUpByFraction(this Rect rect, double scaleFactor)
36
+ {
37
+ return new Rect(rect.X * scaleFactor,
38
+ rect.Y * scaleFactor,
39
+ rect.Width * scaleFactor,
40
+ rect.Height * scaleFactor);
41
+ }
42
+
43
+ public static Rect GetScaleSizeByFraction(this Rect rect, double scaleFactor)
44
+ {
45
+ return new Rect(rect.X,
46
+ rect.Y,
47
+ rect.Width * scaleFactor,
48
+ rect.Height * scaleFactor);
49
+ }
50
+
51
+ public static bool IsGood(this Rect rect)
52
+ {
53
+ if (double.IsNaN(rect.X)
54
+ || double.IsNegativeInfinity(rect.X)
55
+ || double.IsPositiveInfinity(rect.X))
56
+ return false;
57
+
58
+ if (double.IsNaN(rect.Y)
59
+ || double.IsNegativeInfinity(rect.Y)
60
+ || double.IsPositiveInfinity(rect.Y))
61
+ return false;
62
+
63
+ if (double.IsNaN(rect.Height)
64
+ || rect.Height == 0
65
+ || double.IsNegativeInfinity(rect.Height)
66
+ || double.IsPositiveInfinity(rect.Height))
67
+ return false;
68
+
69
+ if (double.IsNaN(rect.Width)
70
+ || rect.Width == 0
71
+ || double.IsNegativeInfinity(rect.Width)
72
+ || double.IsPositiveInfinity(rect.Width))
73
+ return false;
74
+
75
+ return true;
76
+ }
77
+
78
+ public static Point CenterPoint(this Rect rect)
79
+ {
80
+ var x = rect.Left + rect.Width / 2;
81
+ var y = rect.Top + rect.Height / 2;
82
+ return new Point(x, y);
83
+ }
84
+ }
src/ScreenGrab/Extensions/WpfExtensions.cs ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Runtime.InteropServices;
2
+ using System.Windows;
3
+ using System.Windows.Interop;
4
+
5
+ namespace ScreenGrab.Extensions;
6
+
7
+ public static class WpfExtensions
8
+ {
9
+ /// <summary>
10
+ /// Gets the absolute position of the specified window.
11
+ /// </summary>
12
+ /// <param name="w">The window for which to get the absolute position.</param>
13
+ /// <returns>A <see cref="Point" /> representing the absolute position of the window.</returns>
14
+ public static Point GetAbsolutePosition(this Window w)
15
+ {
16
+ // If the window is not maximized, return the top-left corner position of the window
17
+ if (w.WindowState != WindowState.Maximized)
18
+ return new Point(w.Left, w.Top);
19
+
20
+ Int32Rect r;
21
+ // Check if the system supports multiple monitors
22
+ var multimonSupported = OSInterop.GetSystemMetrics(OSInterop.SM_CMONITORS) != 0;
23
+ if (!multimonSupported)
24
+ {
25
+ // If multiple monitors are not supported, get the work area rectangle of the primary monitor
26
+ var rc = new OSInterop.RECT();
27
+ OSInterop.SystemParametersInfo(48, 0, ref rc, 0);
28
+ r = new Int32Rect(rc.left, rc.top, rc.width, rc.height);
29
+ }
30
+ else
31
+ {
32
+ // If multiple monitors are supported, get the monitor information for the monitor containing the window
33
+ var helper = new WindowInteropHelper(w);
34
+ var hmonitor = OSInterop.MonitorFromWindow(new HandleRef(null, helper.EnsureHandle()), 2);
35
+ var info = new OSInterop.MONITORINFOEX();
36
+ OSInterop.GetMonitorInfo(new HandleRef(null, hmonitor), info);
37
+ r = new Int32Rect(info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.width, info.rcMonitor.height);
38
+ }
39
+
40
+ // Return the top-left corner position of the monitor
41
+ return new Point(r.X, r.Y);
42
+ }
43
+ }
src/ScreenGrab/Models/NullAsyncResult.cs ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ namespace ScreenGrab.Models;
2
+
3
+ public class NullAsyncResult : IAsyncResult
4
+ {
5
+ public object? AsyncState => null;
6
+
7
+ public WaitHandle AsyncWaitHandle => new NullWaitHandle();
8
+
9
+ public bool CompletedSynchronously => true;
10
+
11
+ public bool IsCompleted => true;
12
+ }
13
+
14
+ public class NullWaitHandle : WaitHandle
15
+ {
16
+ }
src/ScreenGrab/NativeMethods.cs ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Runtime.InteropServices;
2
+
3
+ internal static partial class NativeMethods
4
+ {
5
+ #if NET7_0_OR_GREATER
6
+
7
+ [LibraryImport("shcore.dll")]
8
+ public static partial void GetScaleFactorForMonitor(IntPtr hMon, out uint pScale);
9
+ #else
10
+ [DllImport("shcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
11
+ public static extern void GetScaleFactorForMonitor(IntPtr hMon, out uint pScale);
12
+ #endif
13
+ }
src/ScreenGrab/OSInterop.cs ADDED
@@ -0,0 +1,1197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Runtime.InteropServices;
2
+
3
+ internal static partial class OSInterop
4
+ {
5
+ public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
6
+
7
+ public enum InputType : uint
8
+ {
9
+ INPUT_MOUSE,
10
+ INPUT_KEYBOARD,
11
+ INPUT_HARDWARE
12
+ }
13
+
14
+ public const int SM_CMONITORS = 80;
15
+
16
+ public const int WH_KEYBOARD_LL = 13;
17
+ public const int VK_SHIFT = 0x10;
18
+ public const int VK_CONTROL = 0x11;
19
+ public const int VK_MENU = 0x12;
20
+ public const int VK_LWIN = 0x5B;
21
+ public const int VK_RWIN = 0x5C;
22
+ public const int VK_ESCAPE = 0x1B;
23
+ public const int WM_HOTKEY = 0x0312;
24
+ public const int WM_KEYDOWN = 0x0100;
25
+ public const int WM_KEYUP = 0x0101;
26
+
27
+ [DllImport("user32.dll", CharSet = CharSet.Auto)]
28
+ public static extern bool GetMonitorInfo(HandleRef hmonitor, [In] [Out] MONITORINFOEX info);
29
+
30
+ [DllImport("user32.dll")]
31
+ public static extern IntPtr MonitorFromWindow(HandleRef handle, int flags);
32
+ #if NET7_0_OR_GREATER
33
+ [LibraryImport("user32.dll")]
34
+ public static partial int GetSystemMetrics(int smIndex);
35
+
36
+ [LibraryImport("user32.dll")]
37
+ [return: MarshalAs(UnmanagedType.Bool)]
38
+ public static partial bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate);
39
+
40
+ [LibraryImport("user32.dll")]
41
+ [return: MarshalAs(UnmanagedType.Bool)]
42
+ public static partial bool ClipCursor(ref RECT lpRect);
43
+ #else
44
+ [DllImport("user32.dll", SetLastError = true)]
45
+ public static extern int GetSystemMetrics(int smIndex);
46
+
47
+ [DllImport("user32.dll", SetLastError = true)]
48
+ [return: MarshalAs(UnmanagedType.Bool)]
49
+ public static extern bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate);
50
+
51
+ [DllImport("user32.dll", SetLastError = true)]
52
+ [return: MarshalAs(UnmanagedType.Bool)]
53
+ public static extern bool ClipCursor(ref RECT lpRect);
54
+ #endif
55
+
56
+ [DllImport("user32.dll")]
57
+ public static extern bool ClipCursor([In] IntPtr lpRect);
58
+
59
+ public struct RECT
60
+ {
61
+ public int left;
62
+ public int top;
63
+ public int right;
64
+ public int bottom;
65
+ public int width => right - left;
66
+ public int height => bottom - top;
67
+ }
68
+
69
+ [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Auto)]
70
+ public class MONITORINFOEX
71
+ {
72
+ public int cbSize = Marshal.SizeOf(typeof(MONITORINFOEX));
73
+ public RECT rcMonitor = new();
74
+ public RECT rcWork = new();
75
+
76
+ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
77
+ public char[] szDevice = new char[32];
78
+
79
+ public int dwFlags;
80
+ }
81
+
82
+ [StructLayout(LayoutKind.Sequential)]
83
+ public struct INPUT
84
+ {
85
+ public InputType Type;
86
+ public InputUnion U;
87
+
88
+ public static int Size => Marshal.SizeOf(typeof(INPUT));
89
+ }
90
+
91
+ [StructLayout(LayoutKind.Explicit)]
92
+ public struct InputUnion
93
+ {
94
+ [FieldOffset(0)] internal MOUSEINPUT Mi;
95
+ [FieldOffset(0)] internal KEYBDINPUT Ki;
96
+ [FieldOffset(0)] internal HARDWAREINPUT Hi;
97
+ }
98
+
99
+ [StructLayout(LayoutKind.Sequential)]
100
+ internal struct MOUSEINPUT
101
+ {
102
+ internal int Dx;
103
+ internal int Dy;
104
+ internal int MouseData;
105
+ internal MOUSEEVENTF DwFlags;
106
+ internal uint Time;
107
+ internal UIntPtr DwExtraInfo;
108
+ }
109
+
110
+ [Flags]
111
+ internal enum MOUSEEVENTF : uint
112
+ {
113
+ ABSOLUTE = 0x8000,
114
+ HWHEEL = 0x01000,
115
+ MOVE = 0x0001,
116
+ MOVE_NOCOALESCE = 0x2000,
117
+ LEFTDOWN = 0x0002,
118
+ LEFTUP = 0x0004,
119
+ RIGHTDOWN = 0x0008,
120
+ RIGHTUP = 0x0010,
121
+ MIDDLEDOWN = 0x0020,
122
+ MIDDLEUP = 0x0040,
123
+ VIRTUALDESK = 0x4000,
124
+ WHEEL = 0x0800,
125
+ XDOWN = 0x0080,
126
+ XUP = 0x0100
127
+ }
128
+
129
+ [StructLayout(LayoutKind.Sequential)]
130
+ internal struct KEYBDINPUT
131
+ {
132
+ internal VirtualKeyShort WVk;
133
+ internal ScanCodeShort WScan;
134
+ internal KEYEVENTF DwFlags;
135
+ internal int Time;
136
+ internal UIntPtr DwExtraInfo;
137
+ }
138
+
139
+ [Flags]
140
+ internal enum KEYEVENTF : uint
141
+ {
142
+ EXTENDEDKEY = 0x0001,
143
+ KEYUP = 0x0002,
144
+ SCANCODE = 0x0008,
145
+ UNICODE = 0x0004
146
+ }
147
+
148
+ internal enum VirtualKeyShort : short
149
+ {
150
+ /// <summary>
151
+ /// Left mouse button
152
+ /// </summary>
153
+ LBUTTON = 0x01,
154
+
155
+ /// <summary>
156
+ /// Right mouse button
157
+ /// </summary>
158
+ RBUTTON = 0x02,
159
+
160
+ /// <summary>
161
+ /// Control-break processing
162
+ /// </summary>
163
+ CANCEL = 0x03,
164
+
165
+ /// <summary>
166
+ /// Middle mouse button (three-button mouse)
167
+ /// </summary>
168
+ MBUTTON = 0x04,
169
+
170
+ /// <summary>
171
+ /// Windows 2000/XP: X1 mouse button
172
+ /// </summary>
173
+ XBUTTON1 = 0x05,
174
+
175
+ /// <summary>
176
+ /// Windows 2000/XP: X2 mouse button
177
+ /// </summary>
178
+ XBUTTON2 = 0x06,
179
+
180
+ /// <summary>
181
+ /// BACKSPACE key
182
+ /// </summary>
183
+ BACK = 0x08,
184
+
185
+ /// <summary>
186
+ /// TAB key
187
+ /// </summary>
188
+ TAB = 0x09,
189
+
190
+ /// <summary>
191
+ /// CLEAR key
192
+ /// </summary>
193
+ CLEAR = 0x0C,
194
+
195
+ /// <summary>
196
+ /// ENTER key
197
+ /// </summary>
198
+ RETURN = 0x0D,
199
+
200
+ /// <summary>
201
+ /// SHIFT key
202
+ /// </summary>
203
+ SHIFT = 0x10,
204
+
205
+ /// <summary>
206
+ /// CTRL key
207
+ /// </summary>
208
+ CONTROL = 0x11,
209
+
210
+ /// <summary>
211
+ /// ALT key
212
+ /// </summary>
213
+ MENU = 0x12,
214
+
215
+ /// <summary>
216
+ /// PAUSE key
217
+ /// </summary>
218
+ PAUSE = 0x13,
219
+
220
+ /// <summary>
221
+ /// CAPS LOCK key
222
+ /// </summary>
223
+ CAPITAL = 0x14,
224
+
225
+ /// <summary>
226
+ /// Input Method Editor (IME) Kana mode
227
+ /// </summary>
228
+ KANA = 0x15,
229
+
230
+ /// <summary>
231
+ /// IME Hangul mode
232
+ /// </summary>
233
+ HANGUL = 0x15,
234
+
235
+ /// <summary>
236
+ /// IME Junja mode
237
+ /// </summary>
238
+ JUNJA = 0x17,
239
+
240
+ /// <summary>
241
+ /// IME final mode
242
+ /// </summary>
243
+ FINAL = 0x18,
244
+
245
+ /// <summary>
246
+ /// IME Hanja mode
247
+ /// </summary>
248
+ HANJA = 0x19,
249
+
250
+ /// <summary>
251
+ /// IME Kanji mode
252
+ /// </summary>
253
+ KANJI = 0x19,
254
+
255
+ /// <summary>
256
+ /// ESC key
257
+ /// </summary>
258
+ ESCAPE = 0x1B,
259
+
260
+ /// <summary>
261
+ /// IME convert
262
+ /// </summary>
263
+ CONVERT = 0x1C,
264
+
265
+ /// <summary>
266
+ /// IME nonconvert
267
+ /// </summary>
268
+ NONCONVERT = 0x1D,
269
+
270
+ /// <summary>
271
+ /// IME accept
272
+ /// </summary>
273
+ ACCEPT = 0x1E,
274
+
275
+ /// <summary>
276
+ /// IME mode change request
277
+ /// </summary>
278
+ MODECHANGE = 0x1F,
279
+
280
+ /// <summary>
281
+ /// SPACEBAR
282
+ /// </summary>
283
+ SPACE = 0x20,
284
+
285
+ /// <summary>
286
+ /// PAGE UP key
287
+ /// </summary>
288
+ PRIOR = 0x21,
289
+
290
+ /// <summary>
291
+ /// PAGE DOWN key
292
+ /// </summary>
293
+ NEXT = 0x22,
294
+
295
+ /// <summary>
296
+ /// END key
297
+ /// </summary>
298
+ END = 0x23,
299
+
300
+ /// <summary>
301
+ /// HOME key
302
+ /// </summary>
303
+ HOME = 0x24,
304
+
305
+ /// <summary>
306
+ /// LEFT ARROW key
307
+ /// </summary>
308
+ LEFT = 0x25,
309
+
310
+ /// <summary>
311
+ /// UP ARROW key
312
+ /// </summary>
313
+ UP = 0x26,
314
+
315
+ /// <summary>
316
+ /// RIGHT ARROW key
317
+ /// </summary>
318
+ RIGHT = 0x27,
319
+
320
+ /// <summary>
321
+ /// DOWN ARROW key
322
+ /// </summary>
323
+ DOWN = 0x28,
324
+
325
+ /// <summary>
326
+ /// SELECT key
327
+ /// </summary>
328
+ SELECT = 0x29,
329
+
330
+ /// <summary>
331
+ /// PRINT key
332
+ /// </summary>
333
+ PRINT = 0x2A,
334
+
335
+ /// <summary>
336
+ /// EXECUTE key
337
+ /// </summary>
338
+ EXECUTE = 0x2B,
339
+
340
+ /// <summary>
341
+ /// PRINT SCREEN key
342
+ /// </summary>
343
+ SNAPSHOT = 0x2C,
344
+
345
+ /// <summary>
346
+ /// INS key
347
+ /// </summary>
348
+ INSERT = 0x2D,
349
+
350
+ /// <summary>
351
+ /// DEL key
352
+ /// </summary>
353
+ DELETE = 0x2E,
354
+
355
+ /// <summary>
356
+ /// HELP key
357
+ /// </summary>
358
+ HELP = 0x2F,
359
+
360
+ /// <summary>
361
+ /// 0 key
362
+ /// </summary>
363
+ KEY_0 = 0x30,
364
+
365
+ /// <summary>
366
+ /// 1 key
367
+ /// </summary>
368
+ KEY_1 = 0x31,
369
+
370
+ /// <summary>
371
+ /// 2 key
372
+ /// </summary>
373
+ KEY_2 = 0x32,
374
+
375
+ /// <summary>
376
+ /// 3 key
377
+ /// </summary>
378
+ KEY_3 = 0x33,
379
+
380
+ /// <summary>
381
+ /// 4 key
382
+ /// </summary>
383
+ KEY_4 = 0x34,
384
+
385
+ /// <summary>
386
+ /// 5 key
387
+ /// </summary>
388
+ KEY_5 = 0x35,
389
+
390
+ /// <summary>
391
+ /// 6 key
392
+ /// </summary>
393
+ KEY_6 = 0x36,
394
+
395
+ /// <summary>
396
+ /// 7 key
397
+ /// </summary>
398
+ KEY_7 = 0x37,
399
+
400
+ /// <summary>
401
+ /// 8 key
402
+ /// </summary>
403
+ KEY_8 = 0x38,
404
+
405
+ /// <summary>
406
+ /// 9 key
407
+ /// </summary>
408
+ KEY_9 = 0x39,
409
+
410
+ /// <summary>
411
+ /// A key
412
+ /// </summary>
413
+ KEY_A = 0x41,
414
+
415
+ /// <summary>
416
+ /// B key
417
+ /// </summary>
418
+ KEY_B = 0x42,
419
+
420
+ /// <summary>
421
+ /// C key
422
+ /// </summary>
423
+ KEY_C = 0x43,
424
+
425
+ /// <summary>
426
+ /// D key
427
+ /// </summary>
428
+ KEY_D = 0x44,
429
+
430
+ /// <summary>
431
+ /// E key
432
+ /// </summary>
433
+ KEY_E = 0x45,
434
+
435
+ /// <summary>
436
+ /// F key
437
+ /// </summary>
438
+ KEY_F = 0x46,
439
+
440
+ /// <summary>
441
+ /// G key
442
+ /// </summary>
443
+ KEY_G = 0x47,
444
+
445
+ /// <summary>
446
+ /// H key
447
+ /// </summary>
448
+ KEY_H = 0x48,
449
+
450
+ /// <summary>
451
+ /// I key
452
+ /// </summary>
453
+ KEY_I = 0x49,
454
+
455
+ /// <summary>
456
+ /// J key
457
+ /// </summary>
458
+ KEY_J = 0x4A,
459
+
460
+ /// <summary>
461
+ /// K key
462
+ /// </summary>
463
+ KEY_K = 0x4B,
464
+
465
+ /// <summary>
466
+ /// L key
467
+ /// </summary>
468
+ KEY_L = 0x4C,
469
+
470
+ /// <summary>
471
+ /// M key
472
+ /// </summary>
473
+ KEY_M = 0x4D,
474
+
475
+ /// <summary>
476
+ /// N key
477
+ /// </summary>
478
+ KEY_N = 0x4E,
479
+
480
+ /// <summary>
481
+ /// O key
482
+ /// </summary>
483
+ KEY_O = 0x4F,
484
+
485
+ /// <summary>
486
+ /// P key
487
+ /// </summary>
488
+ KEY_P = 0x50,
489
+
490
+ /// <summary>
491
+ /// Q key
492
+ /// </summary>
493
+ KEY_Q = 0x51,
494
+
495
+ /// <summary>
496
+ /// R key
497
+ /// </summary>
498
+ KEY_R = 0x52,
499
+
500
+ /// <summary>
501
+ /// S key
502
+ /// </summary>
503
+ KEY_S = 0x53,
504
+
505
+ /// <summary>
506
+ /// T key
507
+ /// </summary>
508
+ KEY_T = 0x54,
509
+
510
+ /// <summary>
511
+ /// U key
512
+ /// </summary>
513
+ KEY_U = 0x55,
514
+
515
+ /// <summary>
516
+ /// V key
517
+ /// </summary>
518
+ KEY_V = 0x56,
519
+
520
+ /// <summary>
521
+ /// W key
522
+ /// </summary>
523
+ KEY_W = 0x57,
524
+
525
+ /// <summary>
526
+ /// X key
527
+ /// </summary>
528
+ KEY_X = 0x58,
529
+
530
+ /// <summary>
531
+ /// Y key
532
+ /// </summary>
533
+ KEY_Y = 0x59,
534
+
535
+ /// <summary>
536
+ /// Z key
537
+ /// </summary>
538
+ KEY_Z = 0x5A,
539
+
540
+ /// <summary>
541
+ /// Left Windows key (Microsoft Natural keyboard)
542
+ /// </summary>
543
+ LWIN = 0x5B,
544
+
545
+ /// <summary>
546
+ /// Right Windows key (Natural keyboard)
547
+ /// </summary>
548
+ RWIN = 0x5C,
549
+
550
+ /// <summary>
551
+ /// Applications key (Natural keyboard)
552
+ /// </summary>
553
+ APPS = 0x5D,
554
+
555
+ /// <summary>
556
+ /// Computer Sleep key
557
+ /// </summary>
558
+ SLEEP = 0x5F,
559
+
560
+ /// <summary>
561
+ /// Numeric keypad 0 key
562
+ /// </summary>
563
+ NUMPAD0 = 0x60,
564
+
565
+ /// <summary>
566
+ /// Numeric keypad 1 key
567
+ /// </summary>
568
+ NUMPAD1 = 0x61,
569
+
570
+ /// <summary>
571
+ /// Numeric keypad 2 key
572
+ /// </summary>
573
+ NUMPAD2 = 0x62,
574
+
575
+ /// <summary>
576
+ /// Numeric keypad 3 key
577
+ /// </summary>
578
+ NUMPAD3 = 0x63,
579
+
580
+ /// <summary>
581
+ /// Numeric keypad 4 key
582
+ /// </summary>
583
+ NUMPAD4 = 0x64,
584
+
585
+ /// <summary>
586
+ /// Numeric keypad 5 key
587
+ /// </summary>
588
+ NUMPAD5 = 0x65,
589
+
590
+ /// <summary>
591
+ /// Numeric keypad 6 key
592
+ /// </summary>
593
+ NUMPAD6 = 0x66,
594
+
595
+ /// <summary>
596
+ /// Numeric keypad 7 key
597
+ /// </summary>
598
+ NUMPAD7 = 0x67,
599
+
600
+ /// <summary>
601
+ /// Numeric keypad 8 key
602
+ /// </summary>
603
+ NUMPAD8 = 0x68,
604
+
605
+ /// <summary>
606
+ /// Numeric keypad 9 key
607
+ /// </summary>
608
+ NUMPAD9 = 0x69,
609
+
610
+ /// <summary>
611
+ /// Multiply key
612
+ /// </summary>
613
+ MULTIPLY = 0x6A,
614
+
615
+ /// <summary>
616
+ /// Add key
617
+ /// </summary>
618
+ ADD = 0x6B,
619
+
620
+ /// <summary>
621
+ /// Separator key
622
+ /// </summary>
623
+ SEPARATOR = 0x6C,
624
+
625
+ /// <summary>
626
+ /// Subtract key
627
+ /// </summary>
628
+ SUBTRACT = 0x6D,
629
+
630
+ /// <summary>
631
+ /// Decimal key
632
+ /// </summary>
633
+ DECIMAL = 0x6E,
634
+
635
+ /// <summary>
636
+ /// Divide key
637
+ /// </summary>
638
+ DIVIDE = 0x6F,
639
+
640
+ /// <summary>
641
+ /// F1 key
642
+ /// </summary>
643
+ F1 = 0x70,
644
+
645
+ /// <summary>
646
+ /// F2 key
647
+ /// </summary>
648
+ F2 = 0x71,
649
+
650
+ /// <summary>
651
+ /// F3 key
652
+ /// </summary>
653
+ F3 = 0x72,
654
+
655
+ /// <summary>
656
+ /// F4 key
657
+ /// </summary>
658
+ F4 = 0x73,
659
+
660
+ /// <summary>
661
+ /// F5 key
662
+ /// </summary>
663
+ F5 = 0x74,
664
+
665
+ /// <summary>
666
+ /// F6 key
667
+ /// </summary>
668
+ F6 = 0x75,
669
+
670
+ /// <summary>
671
+ /// F7 key
672
+ /// </summary>
673
+ F7 = 0x76,
674
+
675
+ /// <summary>
676
+ /// F8 key
677
+ /// </summary>
678
+ F8 = 0x77,
679
+
680
+ /// <summary>
681
+ /// F9 key
682
+ /// </summary>
683
+ F9 = 0x78,
684
+
685
+ /// <summary>
686
+ /// F10 key
687
+ /// </summary>
688
+ F10 = 0x79,
689
+
690
+ /// <summary>
691
+ /// F11 key
692
+ /// </summary>
693
+ F11 = 0x7A,
694
+
695
+ /// <summary>
696
+ /// F12 key
697
+ /// </summary>
698
+ F12 = 0x7B,
699
+
700
+ /// <summary>
701
+ /// F13 key
702
+ /// </summary>
703
+ F13 = 0x7C,
704
+
705
+ /// <summary>
706
+ /// F14 key
707
+ /// </summary>
708
+ F14 = 0x7D,
709
+
710
+ /// <summary>
711
+ /// F15 key
712
+ /// </summary>
713
+ F15 = 0x7E,
714
+
715
+ /// <summary>
716
+ /// F16 key
717
+ /// </summary>
718
+ F16 = 0x7F,
719
+
720
+ /// <summary>
721
+ /// F17 key
722
+ /// </summary>
723
+ F17 = 0x80,
724
+
725
+ /// <summary>
726
+ /// F18 key
727
+ /// </summary>
728
+ F18 = 0x81,
729
+
730
+ /// <summary>
731
+ /// F19 key
732
+ /// </summary>
733
+ F19 = 0x82,
734
+
735
+ /// <summary>
736
+ /// F20 key
737
+ /// </summary>
738
+ F20 = 0x83,
739
+
740
+ /// <summary>
741
+ /// F21 key
742
+ /// </summary>
743
+ F21 = 0x84,
744
+
745
+ /// <summary>
746
+ /// F22 key, (PPC only) Key used to lock device.
747
+ /// </summary>
748
+ F22 = 0x85,
749
+
750
+ /// <summary>
751
+ /// F23 key
752
+ /// </summary>
753
+ F23 = 0x86,
754
+
755
+ /// <summary>
756
+ /// F24 key
757
+ /// </summary>
758
+ F24 = 0x87,
759
+
760
+ /// <summary>
761
+ /// NUM LOCK key
762
+ /// </summary>
763
+ NUMLOCK = 0x90,
764
+
765
+ /// <summary>
766
+ /// SCROLL LOCK key
767
+ /// </summary>
768
+ SCROLL = 0x91,
769
+
770
+ /// <summary>
771
+ /// Left SHIFT key
772
+ /// </summary>
773
+ LSHIFT = 0xA0,
774
+
775
+ /// <summary>
776
+ /// Right SHIFT key
777
+ /// </summary>
778
+ RSHIFT = 0xA1,
779
+
780
+ /// <summary>
781
+ /// Left CONTROL key
782
+ /// </summary>
783
+ LCONTROL = 0xA2,
784
+
785
+ /// <summary>
786
+ /// Right CONTROL key
787
+ /// </summary>
788
+ RCONTROL = 0xA3,
789
+
790
+ /// <summary>
791
+ /// Left MENU key
792
+ /// </summary>
793
+ LMENU = 0xA4,
794
+
795
+ /// <summary>
796
+ /// Right MENU key
797
+ /// </summary>
798
+ RMENU = 0xA5,
799
+
800
+ /// <summary>
801
+ /// Windows 2000/XP: Browser Back key
802
+ /// </summary>
803
+ BROWSER_BACK = 0xA6,
804
+
805
+ /// <summary>
806
+ /// Windows 2000/XP: Browser Forward key
807
+ /// </summary>
808
+ BROWSER_FORWARD = 0xA7,
809
+
810
+ /// <summary>
811
+ /// Windows 2000/XP: Browser Refresh key
812
+ /// </summary>
813
+ BROWSER_REFRESH = 0xA8,
814
+
815
+ /// <summary>
816
+ /// Windows 2000/XP: Browser Stop key
817
+ /// </summary>
818
+ BROWSER_STOP = 0xA9,
819
+
820
+ /// <summary>
821
+ /// Windows 2000/XP: Browser Search key
822
+ /// </summary>
823
+ BROWSER_SEARCH = 0xAA,
824
+
825
+ /// <summary>
826
+ /// Windows 2000/XP: Browser Favorites key
827
+ /// </summary>
828
+ BROWSER_FAVORITES = 0xAB,
829
+
830
+ /// <summary>
831
+ /// Windows 2000/XP: Browser Start and Home key
832
+ /// </summary>
833
+ BROWSER_HOME = 0xAC,
834
+
835
+ /// <summary>
836
+ /// Windows 2000/XP: Volume Mute key
837
+ /// </summary>
838
+ VOLUME_MUTE = 0xAD,
839
+
840
+ /// <summary>
841
+ /// Windows 2000/XP: Volume Down key
842
+ /// </summary>
843
+ VOLUME_DOWN = 0xAE,
844
+
845
+ /// <summary>
846
+ /// Windows 2000/XP: Volume Up key
847
+ /// </summary>
848
+ VOLUME_UP = 0xAF,
849
+
850
+ /// <summary>
851
+ /// Windows 2000/XP: Next Track key
852
+ /// </summary>
853
+ MEDIA_NEXT_TRACK = 0xB0,
854
+
855
+ /// <summary>
856
+ /// Windows 2000/XP: Previous Track key
857
+ /// </summary>
858
+ MEDIA_PREV_TRACK = 0xB1,
859
+
860
+ /// <summary>
861
+ /// Windows 2000/XP: Stop Media key
862
+ /// </summary>
863
+ MEDIA_STOP = 0xB2,
864
+
865
+ /// <summary>
866
+ /// Windows 2000/XP: Play/Pause Media key
867
+ /// </summary>
868
+ MEDIA_PLAY_PAUSE = 0xB3,
869
+
870
+ /// <summary>
871
+ /// Windows 2000/XP: Start Mail key
872
+ /// </summary>
873
+ LAUNCH_MAIL = 0xB4,
874
+
875
+ /// <summary>
876
+ /// Windows 2000/XP: Select Media key
877
+ /// </summary>
878
+ LAUNCH_MEDIA_SELECT = 0xB5,
879
+
880
+ /// <summary>
881
+ /// Windows 2000/XP: Start Application 1 key
882
+ /// </summary>
883
+ LAUNCH_APP1 = 0xB6,
884
+
885
+ /// <summary>
886
+ /// Windows 2000/XP: Start Application 2 key
887
+ /// </summary>
888
+ LAUNCH_APP2 = 0xB7,
889
+
890
+ /// <summary>
891
+ /// Used for miscellaneous characters; it can vary by keyboard.
892
+ /// </summary>
893
+ OEM_1 = 0xBA,
894
+
895
+ /// <summary>
896
+ /// Windows 2000/XP: For any country/region, the '+' key
897
+ /// </summary>
898
+ OEM_PLUS = 0xBB,
899
+
900
+ /// <summary>
901
+ /// Windows 2000/XP: For any country/region, the ',' key
902
+ /// </summary>
903
+ OEM_COMMA = 0xBC,
904
+
905
+ /// <summary>
906
+ /// Windows 2000/XP: For any country/region, the '-' key
907
+ /// </summary>
908
+ OEM_MINUS = 0xBD,
909
+
910
+ /// <summary>
911
+ /// Windows 2000/XP: For any country/region, the '.' key
912
+ /// </summary>
913
+ OEM_PERIOD = 0xBE,
914
+
915
+ /// <summary>
916
+ /// Used for miscellaneous characters; it can vary by keyboard.
917
+ /// </summary>
918
+ OEM_2 = 0xBF,
919
+
920
+ /// <summary>
921
+ /// Used for miscellaneous characters; it can vary by keyboard.
922
+ /// </summary>
923
+ OEM_3 = 0xC0,
924
+
925
+ /// <summary>
926
+ /// Used for miscellaneous characters; it can vary by keyboard.
927
+ /// </summary>
928
+ OEM_4 = 0xDB,
929
+
930
+ /// <summary>
931
+ /// Used for miscellaneous characters; it can vary by keyboard.
932
+ /// </summary>
933
+ OEM_5 = 0xDC,
934
+
935
+ /// <summary>
936
+ /// Used for miscellaneous characters; it can vary by keyboard.
937
+ /// </summary>
938
+ OEM_6 = 0xDD,
939
+
940
+ /// <summary>
941
+ /// Used for miscellaneous characters; it can vary by keyboard.
942
+ /// </summary>
943
+ OEM_7 = 0xDE,
944
+
945
+ /// <summary>
946
+ /// Used for miscellaneous characters; it can vary by keyboard.
947
+ /// </summary>
948
+ OEM_8 = 0xDF,
949
+
950
+ /// <summary>
951
+ /// Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
952
+ /// </summary>
953
+ OEM_102 = 0xE2,
954
+
955
+ /// <summary>
956
+ /// Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
957
+ /// </summary>
958
+ PROCESSKEY = 0xE5,
959
+
960
+ /// <summary>
961
+ /// Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes.
962
+ /// The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more
963
+ /// information,
964
+ /// see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP
965
+ /// </summary>
966
+ PACKET = 0xE7,
967
+
968
+ /// <summary>
969
+ /// Attn key
970
+ /// </summary>
971
+ ATTN = 0xF6,
972
+
973
+ /// <summary>
974
+ /// CrSel key
975
+ /// </summary>
976
+ CRSEL = 0xF7,
977
+
978
+ /// <summary>
979
+ /// ExSel key
980
+ /// </summary>
981
+ EXSEL = 0xF8,
982
+
983
+ /// <summary>
984
+ /// Erase EOF key
985
+ /// </summary>
986
+ EREOF = 0xF9,
987
+
988
+ /// <summary>
989
+ /// Play key
990
+ /// </summary>
991
+ PLAY = 0xFA,
992
+
993
+ /// <summary>
994
+ /// Zoom key
995
+ /// </summary>
996
+ ZOOM = 0xFB,
997
+
998
+ /// <summary>
999
+ /// Reserved
1000
+ /// </summary>
1001
+ NONAME = 0xFC,
1002
+
1003
+ /// <summary>
1004
+ /// PA1 key
1005
+ /// </summary>
1006
+ PA1 = 0xFD,
1007
+
1008
+ /// <summary>
1009
+ /// Clear key
1010
+ /// </summary>
1011
+ OEM_CLEAR = 0xFE
1012
+ }
1013
+
1014
+ internal enum ScanCodeShort : short
1015
+ {
1016
+ LBUTTON = 0,
1017
+ RBUTTON = 0,
1018
+ CANCEL = 70,
1019
+ MBUTTON = 0,
1020
+ XBUTTON1 = 0,
1021
+ XBUTTON2 = 0,
1022
+ BACK = 14,
1023
+ TAB = 15,
1024
+ CLEAR = 76,
1025
+ RETURN = 28,
1026
+ SHIFT = 42,
1027
+ CONTROL = 29,
1028
+ MENU = 56,
1029
+ PAUSE = 0,
1030
+ CAPITAL = 58,
1031
+ KANA = 0,
1032
+ HANGUL = 0,
1033
+ JUNJA = 0,
1034
+ FINAL = 0,
1035
+ HANJA = 0,
1036
+ KANJI = 0,
1037
+ ESCAPE = 1,
1038
+ CONVERT = 0,
1039
+ NONCONVERT = 0,
1040
+ ACCEPT = 0,
1041
+ MODECHANGE = 0,
1042
+ SPACE = 57,
1043
+ PRIOR = 73,
1044
+ NEXT = 81,
1045
+ END = 79,
1046
+ HOME = 71,
1047
+ LEFT = 75,
1048
+ UP = 72,
1049
+ RIGHT = 77,
1050
+ DOWN = 80,
1051
+ SELECT = 0,
1052
+ PRINT = 0,
1053
+ EXECUTE = 0,
1054
+ SNAPSHOT = 84,
1055
+ INSERT = 82,
1056
+ DELETE = 83,
1057
+ HELP = 99,
1058
+ KEY_0 = 11,
1059
+ KEY_1 = 2,
1060
+ KEY_2 = 3,
1061
+ KEY_3 = 4,
1062
+ KEY_4 = 5,
1063
+ KEY_5 = 6,
1064
+ KEY_6 = 7,
1065
+ KEY_7 = 8,
1066
+ KEY_8 = 9,
1067
+ KEY_9 = 10,
1068
+ KEY_A = 30,
1069
+ KEY_B = 48,
1070
+ KEY_C = 46,
1071
+ KEY_D = 32,
1072
+ KEY_E = 18,
1073
+ KEY_F = 33,
1074
+ KEY_G = 34,
1075
+ KEY_H = 35,
1076
+ KEY_I = 23,
1077
+ KEY_J = 36,
1078
+ KEY_K = 37,
1079
+ KEY_L = 38,
1080
+ KEY_M = 50,
1081
+ KEY_N = 49,
1082
+ KEY_O = 24,
1083
+ KEY_P = 25,
1084
+ KEY_Q = 16,
1085
+ KEY_R = 19,
1086
+ KEY_S = 31,
1087
+ KEY_T = 20,
1088
+ KEY_U = 22,
1089
+ KEY_V = 47,
1090
+ KEY_W = 17,
1091
+ KEY_X = 45,
1092
+ KEY_Y = 21,
1093
+ KEY_Z = 44,
1094
+ LWIN = 91,
1095
+ RWIN = 92,
1096
+ APPS = 93,
1097
+ SLEEP = 95,
1098
+ NUMPAD0 = 82,
1099
+ NUMPAD1 = 79,
1100
+ NUMPAD2 = 80,
1101
+ NUMPAD3 = 81,
1102
+ NUMPAD4 = 75,
1103
+ NUMPAD5 = 76,
1104
+ NUMPAD6 = 77,
1105
+ NUMPAD7 = 71,
1106
+ NUMPAD8 = 72,
1107
+ NUMPAD9 = 73,
1108
+ MULTIPLY = 55,
1109
+ ADD = 78,
1110
+ SEPARATOR = 0,
1111
+ SUBTRACT = 74,
1112
+ DECIMAL = 83,
1113
+ DIVIDE = 53,
1114
+ F1 = 59,
1115
+ F2 = 60,
1116
+ F3 = 61,
1117
+ F4 = 62,
1118
+ F5 = 63,
1119
+ F6 = 64,
1120
+ F7 = 65,
1121
+ F8 = 66,
1122
+ F9 = 67,
1123
+ F10 = 68,
1124
+ F11 = 87,
1125
+ F12 = 88,
1126
+ F13 = 100,
1127
+ F14 = 101,
1128
+ F15 = 102,
1129
+ F16 = 103,
1130
+ F17 = 104,
1131
+ F18 = 105,
1132
+ F19 = 106,
1133
+ F20 = 107,
1134
+ F21 = 108,
1135
+ F22 = 109,
1136
+ F23 = 110,
1137
+ F24 = 118,
1138
+ NUMLOCK = 69,
1139
+ SCROLL = 70,
1140
+ LSHIFT = 42,
1141
+ RSHIFT = 54,
1142
+ LCONTROL = 29,
1143
+ RCONTROL = 29,
1144
+ LMENU = 56,
1145
+ RMENU = 56,
1146
+ BROWSER_BACK = 106,
1147
+ BROWSER_FORWARD = 105,
1148
+ BROWSER_REFRESH = 103,
1149
+ BROWSER_STOP = 104,
1150
+ BROWSER_SEARCH = 101,
1151
+ BROWSER_FAVORITES = 102,
1152
+ BROWSER_HOME = 50,
1153
+ VOLUME_MUTE = 32,
1154
+ VOLUME_DOWN = 46,
1155
+ VOLUME_UP = 48,
1156
+ MEDIA_NEXT_TRACK = 25,
1157
+ MEDIA_PREV_TRACK = 16,
1158
+ MEDIA_STOP = 36,
1159
+ MEDIA_PLAY_PAUSE = 34,
1160
+ LAUNCH_MAIL = 108,
1161
+ LAUNCH_MEDIA_SELECT = 109,
1162
+ LAUNCH_APP1 = 107,
1163
+ LAUNCH_APP2 = 33,
1164
+ OEM_1 = 39,
1165
+ OEM_PLUS = 13,
1166
+ OEM_COMMA = 51,
1167
+ OEM_MINUS = 12,
1168
+ OEM_PERIOD = 52,
1169
+ OEM_2 = 53,
1170
+ OEM_3 = 41,
1171
+ OEM_4 = 26,
1172
+ OEM_5 = 43,
1173
+ OEM_6 = 27,
1174
+ OEM_7 = 40,
1175
+ OEM_8 = 0,
1176
+ OEM_102 = 86,
1177
+ PROCESSKEY = 0,
1178
+ PACKET = 0,
1179
+ ATTN = 0,
1180
+ CRSEL = 0,
1181
+ EXSEL = 0,
1182
+ EREOF = 93,
1183
+ PLAY = 0,
1184
+ ZOOM = 98,
1185
+ NONAME = 0,
1186
+ PA1 = 0,
1187
+ OEM_CLEAR = 0
1188
+ }
1189
+
1190
+ [StructLayout(LayoutKind.Sequential)]
1191
+ internal struct HARDWAREINPUT
1192
+ {
1193
+ internal int UMsg;
1194
+ internal short WParamL;
1195
+ internal short WParamH;
1196
+ }
1197
+ }
src/ScreenGrab/ScreenGrab.csproj ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Project Sdk="Microsoft.NET.Sdk">
2
+
3
+ <PropertyGroup>
4
+ <TargetFramework>net8.0-windows</TargetFramework>
5
+ <Nullable>enable</Nullable>
6
+ <UseWPF>true</UseWPF>
7
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8
+ <ImplicitUsings>enable</ImplicitUsings>
9
+ </PropertyGroup>
10
+
11
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
12
+ <Optimize>False</Optimize>
13
+ </PropertyGroup>
14
+
15
+ <ItemGroup>
16
+ <PackageReference Include="Dapplo.Windows.User32" Version="1.0.28" />
17
+ </ItemGroup>
18
+
19
+ <ItemGroup>
20
+ <Folder Include="Extensions\" />
21
+ </ItemGroup>
22
+
23
+ </Project>
src/ScreenGrab/ScreenGrabView.xaml ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Window
2
+ x:Class="ScreenGrab.ScreenGrabView"
3
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6
+ xmlns:local="clr-namespace:ScreenGrab"
7
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8
+ Title="ScreenGrab"
9
+ Width="800"
10
+ Height="450"
11
+ AllowsTransparency="True"
12
+ Background="Transparent"
13
+ Closed="Window_Closed"
14
+ Loaded="Window_Loaded"
15
+ ResizeMode="NoResize"
16
+ ShowInTaskbar="False"
17
+ Topmost="True"
18
+ Unloaded="Window_Unloaded"
19
+ WindowStyle="None"
20
+ mc:Ignorable="d">
21
+
22
+ <Grid d:Background="NavajoWhite">
23
+ <Viewbox>
24
+ <Image x:Name="BackgroundImage" Stretch="UniformToFill" />
25
+ </Viewbox>
26
+ <Canvas
27
+ x:Name="RegionClickCanvas"
28
+ Cursor="Cross"
29
+ MouseDown="RegionClickCanvas_MouseDown"
30
+ MouseEnter="RegionClickCanvas_MouseEnter"
31
+ MouseLeave="RegionClickCanvas_MouseLeave"
32
+ MouseMove="RegionClickCanvas_MouseMove"
33
+ MouseUp="RegionClickCanvas_MouseUp">
34
+ <Canvas.Clip>
35
+ <CombinedGeometry GeometryCombineMode="Exclude">
36
+ <CombinedGeometry.Geometry1>
37
+ <RectangleGeometry x:Name="FullWindow" />
38
+ </CombinedGeometry.Geometry1>
39
+ <CombinedGeometry.Geometry2>
40
+ <RectangleGeometry x:Name="ClippingGeometry" />
41
+ </CombinedGeometry.Geometry2>
42
+ </CombinedGeometry>
43
+ </Canvas.Clip>
44
+ <Canvas.Background>
45
+ <SolidColorBrush
46
+ x:Name="BackgroundBrush"
47
+ Opacity="0"
48
+ Color="#FF202020" />
49
+ </Canvas.Background>
50
+ <!-- Horizontal Line -->
51
+ <Line
52
+ x:Name="HorizontalLine"
53
+ Stroke="#92caf4"
54
+ StrokeThickness="1"
55
+ Visibility="Collapsed" />
56
+ <!-- Vertical Line -->
57
+ <Line
58
+ x:Name="VerticalLine"
59
+ Stroke="#92caf4"
60
+ StrokeThickness="1"
61
+ Visibility="Collapsed" />
62
+ </Canvas>
63
+
64
+ <Border
65
+ x:Name="PromptMsg"
66
+ Margin="15"
67
+ HorizontalAlignment="Left"
68
+ VerticalAlignment="Bottom"
69
+ Background="#424242"
70
+ BorderBrush="WhiteSmoke"
71
+ BorderThickness=".6"
72
+ Opacity="0.9"
73
+ Visibility="Collapsed">
74
+ <local:AutoGrid
75
+ ChildMargin="3"
76
+ ChildVerticalAlignment="Center"
77
+ Columns="*, *"
78
+ RowCount="2"
79
+ RowHeight="30">
80
+ <Grid.Resources>
81
+ <Style TargetType="StackPanel">
82
+ <Setter Property="Orientation" Value="Horizontal" />
83
+ <Setter Property="HorizontalAlignment" Value="Right" />
84
+ </Style>
85
+ <Style TargetType="TextBlock">
86
+ <Setter Property="Foreground" Value="White" />
87
+ <Setter Property="FontSize" Value="16" />
88
+ <Setter Property="HorizontalAlignment" Value="Left" />
89
+
90
+ </Style>
91
+ </Grid.Resources>
92
+ <StackPanel>
93
+ <local:UcCharacter DisplayChar="Shift" />
94
+ <local:UcCharacter DisplayChar="🖱" />
95
+ </StackPanel>
96
+ <TextBlock Text="移动选区" />
97
+ <StackPanel>
98
+ <local:UcCharacter DisplayChar="F" />
99
+ </StackPanel>
100
+ <TextBlock x:Name="FreezeTb" Text="取消冻结窗口" />
101
+ </local:AutoGrid>
102
+ </Border>
103
+ </Grid>
104
+ </Window>
src/ScreenGrab/ScreenGrabView.xaml.cs ADDED
@@ -0,0 +1,442 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Drawing;
2
+ using System.Windows;
3
+ using System.Windows.Controls;
4
+ using System.Windows.Input;
5
+ using System.Windows.Media;
6
+ using Dapplo.Windows.User32;
7
+ using ScreenGrab.Extensions;
8
+ using ScreenGrab.Utilities;
9
+ using Color = System.Windows.Media.Color;
10
+ using Point = System.Windows.Point;
11
+ using Size = System.Windows.Size;
12
+
13
+ namespace ScreenGrab;
14
+
15
+ /// <summary>
16
+ /// Extracted from the project <see href="https://github.com/TheJoeFin/Text-Grab" />
17
+ /// </summary>
18
+ public partial class ScreenGrabView
19
+ {
20
+ #region Constructors
21
+
22
+ public ScreenGrabView(Action<Bitmap>? action, bool isAuxiliary = false)
23
+ {
24
+ InitializeComponent();
25
+ _onImageCaptured = action;
26
+ _isAuxiliary = isAuxiliary;
27
+ }
28
+
29
+ #endregion Constructors
30
+
31
+ #region Properties
32
+
33
+ public Action? OnGrabClose { get; set; }
34
+ private DisplayInfo? CurrentScreen { get; set; }
35
+
36
+ #endregion Properties
37
+
38
+ #region Fields
39
+
40
+ private Point _clickedPoint;
41
+ private DpiScale? _dpiScale;
42
+ private bool _isSelecting;
43
+ private bool _isShiftDown;
44
+ private double _selectLeft;
45
+ private double _selectTop;
46
+ private Point _shiftPoint;
47
+ private double _xShiftDelta;
48
+ private double _yShiftDelta;
49
+ private Point _promptMsgTopLeft;
50
+ private Point _promptMsgBottomRight;
51
+ private bool _isFreezeHandle;
52
+
53
+ private readonly Border _selectBorder = new();
54
+ private const double SelectBorderThickness = 2;
55
+ private readonly Color _borderColor = Color.FromArgb(255, 146, 202, 244);
56
+
57
+ private readonly Action<Bitmap>? _onImageCaptured;
58
+ private readonly bool _isAuxiliary;
59
+
60
+ #endregion Fields
61
+
62
+ #region Window Events
63
+
64
+ private void Window_Closed(object? sender, EventArgs e)
65
+ {
66
+ InputMethodOperate(false);
67
+
68
+ Close();
69
+
70
+ GC.Collect();
71
+ }
72
+
73
+ private void Window_Loaded(object sender, RoutedEventArgs e)
74
+ {
75
+ WindowState = WindowState.Maximized;
76
+ FullWindow.Rect = new Rect(0, 0, Width, Height);
77
+ KeyDown += ScreenGrab_KeyDown;
78
+ KeyUp += ScreenGrab_KeyUp;
79
+
80
+ SetImageToBackground();
81
+
82
+ #if DEBUG
83
+ Topmost = false;
84
+ #endif
85
+ InputMethodOperate();
86
+
87
+ if (this.IsMouseInWindow())
88
+ SetPromptMsgVisibility(true);
89
+
90
+ if (!_isAuxiliary) return;
91
+ (HorizontalLine.X1, VerticalLine.Y1, (HorizontalLine.X2, VerticalLine.Y2)) = (0, 0, this.GetWidthHeight());
92
+
93
+ // Set the Auxiliary to be visible when the mouse in the window
94
+ if (this.IsMouseInWindow())
95
+ SetAuxiliaryVisibility(true);
96
+ }
97
+
98
+ private void Window_Unloaded(object sender, RoutedEventArgs e)
99
+ {
100
+ FreezeBgImage();
101
+ BackgroundImage.UpdateLayout();
102
+ CurrentScreen = null;
103
+ _dpiScale = null;
104
+
105
+ Loaded -= Window_Loaded;
106
+ Unloaded -= Window_Unloaded;
107
+
108
+ RegionClickCanvas.MouseDown -= RegionClickCanvas_MouseDown;
109
+ RegionClickCanvas.MouseMove -= RegionClickCanvas_MouseMove;
110
+ RegionClickCanvas.MouseUp -= RegionClickCanvas_MouseUp;
111
+
112
+ KeyDown -= ScreenGrab_KeyDown;
113
+ KeyUp -= ScreenGrab_KeyUp;
114
+ }
115
+
116
+ private void ScreenGrab_KeyDown(object sender, KeyEventArgs e)
117
+ {
118
+ switch (e.Key)
119
+ {
120
+ case Key.Escape:
121
+ CloseAllScreenGrabs();
122
+ break;
123
+ case Key.F:
124
+ FreezeUnfreezeAllScreenGrabs();
125
+ break;
126
+ }
127
+ }
128
+
129
+ private void ScreenGrab_KeyUp(object sender, KeyEventArgs e)
130
+ {
131
+ switch (e.Key)
132
+ {
133
+ case Key.LeftShift:
134
+ case Key.RightShift:
135
+ _isShiftDown = false;
136
+ _clickedPoint = new Point(_clickedPoint.X + _xShiftDelta, _clickedPoint.Y + _yShiftDelta);
137
+ break;
138
+ }
139
+ }
140
+
141
+ private void SetImageToBackground()
142
+ {
143
+ FreezeBgImage();
144
+ BackgroundImage.Source = this.GetWindowBoundsImage();
145
+ BackgroundBrush.Opacity = 0.2;
146
+ }
147
+
148
+ private async void FreezeUnfreeze()
149
+ {
150
+ _isFreezeHandle = true;
151
+ if (BackgroundImage.Source == null)
152
+ {
153
+ if (_isAuxiliary)
154
+ SetAuxiliaryVisibility(false);
155
+
156
+ SetPromptMsgVisibility(false);
157
+ BackgroundBrush.Opacity = 0;
158
+ await Task.Delay(150);
159
+ SetImageToBackground();
160
+
161
+ if (this.IsMouseInWindow())
162
+ {
163
+ SetAuxiliaryVisibility(_isAuxiliary);
164
+ SetPromptMsgVisibility(true);
165
+ }
166
+
167
+ // 刷新提示词
168
+ FreezeTb.Text = "取消冻结窗口";
169
+ }
170
+ else
171
+ {
172
+ FreezeBgImage();
173
+ // 刷新提示词
174
+ FreezeTb.Text = "冻结窗口";
175
+ }
176
+ _isFreezeHandle = false;
177
+ }
178
+
179
+ private void CloseAllScreenGrabs()
180
+ {
181
+ foreach (var window in Application.Current.Windows)
182
+ if (window is ScreenGrabView sgv)
183
+ sgv.Close();
184
+ OnGrabClose?.Invoke();
185
+ }
186
+
187
+ private void FreezeUnfreezeAllScreenGrabs()
188
+ {
189
+ foreach (var window in Application.Current.Windows)
190
+ if (window is ScreenGrabView sgv)
191
+ sgv.FreezeUnfreeze();
192
+ }
193
+
194
+ private void FreezeBgImage()
195
+ {
196
+ BackgroundImage.Source?.Freeze();
197
+ BackgroundImage.Source = null;
198
+ }
199
+
200
+ private void SetAuxiliaryVisibility(bool isVisible)
201
+ {
202
+ var setVisibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
203
+ if (HorizontalLine.Visibility == setVisibility && VerticalLine.Visibility == setVisibility)
204
+ return;
205
+ HorizontalLine.Visibility = setVisibility;
206
+ VerticalLine.Visibility = setVisibility;
207
+ }
208
+
209
+ private void SetPromptMsgVisibility(bool isVisible)
210
+ {
211
+ var setVisibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
212
+ if (PromptMsg.Visibility == setVisibility)
213
+ return;
214
+
215
+ PromptMsg.Visibility = setVisibility;
216
+ }
217
+
218
+ #endregion
219
+
220
+ #region Mouse Events
221
+
222
+ private void RegionClickCanvas_MouseLeave(object sender, MouseEventArgs e)
223
+ {
224
+ SetAuxiliaryVisibility(false);
225
+ SetPromptMsgVisibility(false);
226
+ }
227
+
228
+ private void RegionClickCanvas_MouseEnter(object sender, MouseEventArgs e)
229
+ {
230
+ SetAuxiliaryVisibility(_isAuxiliary);
231
+ SetPromptMsgVisibility(true);
232
+
233
+ // 获取 PromptMsg 控件的边界
234
+ _promptMsgTopLeft = PromptMsg.TranslatePoint(new Point(0, 0), this);
235
+ _promptMsgBottomRight =
236
+ PromptMsg.TranslatePoint(new Point(PromptMsg.ActualWidth, PromptMsg.ActualHeight), this);
237
+ }
238
+
239
+ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e)
240
+ {
241
+ // Right click to close
242
+ if (e.RightButton == MouseButtonState.Pressed)
243
+ {
244
+ CloseAllScreenGrabs();
245
+ return;
246
+ }
247
+
248
+ _isSelecting = true;
249
+ SetAuxiliaryVisibility(false);
250
+ SetPromptMsgVisibility(false);
251
+ RegionClickCanvas.CaptureMouse();
252
+ CursorClipper.ClipCursor(this);
253
+ _clickedPoint = e.GetPosition(this);
254
+ _selectBorder.Height = 2;
255
+ _selectBorder.Width = 2;
256
+
257
+ _dpiScale = VisualTreeHelper.GetDpi(this);
258
+
259
+ try
260
+ {
261
+ RegionClickCanvas.Children.Remove(_selectBorder);
262
+ }
263
+ catch (Exception)
264
+ {
265
+ // ignored
266
+ }
267
+
268
+ _selectBorder.BorderThickness = new Thickness(SelectBorderThickness);
269
+ _selectBorder.BorderBrush = new SolidColorBrush(_borderColor);
270
+ _ = RegionClickCanvas.Children.Add(_selectBorder);
271
+ Canvas.SetLeft(_selectBorder, _clickedPoint.X);
272
+ Canvas.SetTop(_selectBorder, _clickedPoint.Y);
273
+
274
+ // Initialize ClippingGeometry.Rect with a valid Rect
275
+ ClippingGeometry.Rect = new Rect(_clickedPoint, new Size(0, 0));
276
+
277
+ WindowUtilities.GetMousePosition(out var mousePoint);
278
+ foreach (var screen in DisplayInfo.AllDisplayInfos)
279
+ {
280
+ var bound = screen.ScaledBounds();
281
+ if (bound.Contains(mousePoint))
282
+ CurrentScreen = screen;
283
+ }
284
+ }
285
+
286
+ private void RegionClickCanvas_MouseMove(object sender, MouseEventArgs e)
287
+ {
288
+ if (_isFreezeHandle) return;
289
+
290
+ var movingPoint = e.GetPosition(this);
291
+
292
+ if (!_isSelecting)
293
+ {
294
+ // 检查鼠标是否在 PromptMsg 控件区域内
295
+ var isMouseInPromptMsg = movingPoint.X >= _promptMsgTopLeft.X && movingPoint.X <= _promptMsgBottomRight.X &&
296
+ movingPoint.Y >= _promptMsgTopLeft.Y && movingPoint.Y <= _promptMsgBottomRight.Y;
297
+
298
+ SetPromptMsgVisibility(!isMouseInPromptMsg);
299
+
300
+ // Determine whether to update auxiliary line information based on configuration
301
+ if (!_isAuxiliary) return;
302
+
303
+ // Update the horizontal line to match the mouse Y position
304
+ HorizontalLine.Y1 = HorizontalLine.Y2 = movingPoint.Y;
305
+
306
+ // Update the vertical line to match the mouse X position
307
+ VerticalLine.X1 = VerticalLine.X2 = movingPoint.X;
308
+ return;
309
+ }
310
+
311
+ if (Keyboard.Modifiers == ModifierKeys.Shift)
312
+ {
313
+ PanSelection(movingPoint);
314
+ return;
315
+ }
316
+
317
+ _isShiftDown = false;
318
+
319
+ var left = Math.Min(_clickedPoint.X, movingPoint.X);
320
+ var top = Math.Min(_clickedPoint.Y, movingPoint.Y);
321
+
322
+ _selectBorder.Height = Math.Max(_clickedPoint.Y, movingPoint.Y) - top;
323
+ _selectBorder.Width = Math.Max(_clickedPoint.X, movingPoint.X) - left;
324
+ _selectBorder.Height += SelectBorderThickness;
325
+ _selectBorder.Width += SelectBorderThickness;
326
+
327
+ ClippingGeometry.Rect = new Rect(
328
+ new Point(left, top),
329
+ new Size(_selectBorder.Width - SelectBorderThickness, _selectBorder.Height - SelectBorderThickness));
330
+ Canvas.SetLeft(_selectBorder, left - 1);
331
+ Canvas.SetTop(_selectBorder, top - 1);
332
+ }
333
+
334
+ private void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs e)
335
+ {
336
+ if (!_isSelecting || _dpiScale is null)
337
+ return;
338
+
339
+ _isSelecting = false;
340
+ CurrentScreen = null;
341
+ CursorClipper.UnClipCursor();
342
+ RegionClickCanvas.ReleaseMouseCapture();
343
+ ClippingGeometry.Rect = new Rect(new Point(0, 0), new Size(0, 0));
344
+
345
+ // Get the scaled dimensions of the selection border except the border thickness
346
+ var xDimScaled = (Canvas.GetLeft(_selectBorder) + SelectBorderThickness) * _dpiScale.Value.DpiScaleX;
347
+ var yDimScaled = (Canvas.GetTop(_selectBorder) + SelectBorderThickness) * _dpiScale.Value.DpiScaleY;
348
+
349
+ // Get the scaled dimensions of the selection border except the border thickness
350
+ Rectangle regionScaled = new(
351
+ (int)xDimScaled,
352
+ (int)yDimScaled,
353
+ (int)((_selectBorder.Width - 2 * SelectBorderThickness) * _dpiScale.Value.DpiScaleX),
354
+ (int)((_selectBorder.Height - 2 * SelectBorderThickness) * _dpiScale.Value.DpiScaleY));
355
+
356
+ try
357
+ {
358
+ RegionClickCanvas.Children.Remove(_selectBorder);
359
+ }
360
+ catch
361
+ {
362
+ // ignored
363
+ }
364
+
365
+ var absPosPoint = this.GetAbsolutePosition();
366
+
367
+ var thisCorrectedLeft = (int)absPosPoint.X + regionScaled.Left;
368
+ var thisCorrectedTop = (int)absPosPoint.Y + regionScaled.Top;
369
+
370
+ var correctedRegion = regionScaled with { X = thisCorrectedLeft, Y = thisCorrectedTop };
371
+ var bitmap = correctedRegion.GetRegionOfScreenAsBitmap();
372
+ CloseAllScreenGrabs();
373
+
374
+ _onImageCaptured?.Invoke(bitmap);
375
+ }
376
+
377
+ private void PanSelection(Point movingPoint)
378
+ {
379
+ if (!_isShiftDown)
380
+ {
381
+ _shiftPoint = movingPoint;
382
+ _selectLeft = Canvas.GetLeft(_selectBorder);
383
+ _selectTop = Canvas.GetTop(_selectBorder);
384
+ }
385
+
386
+ _isShiftDown = true;
387
+ _xShiftDelta = movingPoint.X - _shiftPoint.X;
388
+ _yShiftDelta = movingPoint.Y - _shiftPoint.Y;
389
+
390
+ var leftValue = _selectLeft + _xShiftDelta;
391
+ var topValue = _selectTop + _yShiftDelta;
392
+
393
+ if (CurrentScreen is not null && _dpiScale is not null)
394
+ {
395
+ double currentScreenLeft = 0;
396
+ double currentScreenTop = 0;
397
+ var currentScreenRight = CurrentScreen.Bounds.Width / _dpiScale.Value.DpiScaleX;
398
+ var currentScreenBottom = CurrentScreen.Bounds.Height / _dpiScale.Value.DpiScaleY;
399
+
400
+ leftValue = Clamp(leftValue, currentScreenLeft, currentScreenRight - _selectBorder.Width);
401
+ topValue = Clamp(topValue, currentScreenTop, currentScreenBottom - _selectBorder.Height);
402
+ }
403
+
404
+ ClippingGeometry.Rect = new Rect(
405
+ new Point(leftValue, topValue),
406
+ new Size(_selectBorder.Width - 2, _selectBorder.Height - 2));
407
+ Canvas.SetLeft(_selectBorder, leftValue - 1);
408
+ Canvas.SetTop(_selectBorder, topValue - 1);
409
+ }
410
+
411
+ private double Clamp(double value, double min, double max)
412
+ {
413
+ #if NETFRAMEWORK
414
+ if (value < min) return min;
415
+ if (value > max) return max;
416
+ return value;
417
+ #else
418
+ return Math.Clamp(value, min, max);
419
+ #endif
420
+ }
421
+
422
+ private void InputMethodOperate(bool isDisableInputMethod = true)
423
+ {
424
+ if (isDisableInputMethod)
425
+ {
426
+ // 禁用输入法编辑器
427
+ InputMethod.SetIsInputMethodEnabled(this, false);
428
+ // 设置输入法状态为英文
429
+ InputMethod.SetPreferredImeState(this, InputMethodState.Off);
430
+ // 设置为英文输入模式
431
+ InputMethod.SetPreferredImeConversionMode(this, ImeConversionModeValues.Alphanumeric);
432
+ }
433
+ else
434
+ {
435
+ // 恢复输入法状态
436
+ InputMethod.SetIsInputMethodEnabled(this, true);
437
+ InputMethod.SetPreferredImeState(this, InputMethodState.DoNotCare);
438
+ }
439
+ }
440
+
441
+ #endregion
442
+ }
src/ScreenGrab/ScreenGrabber.cs ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Drawing;
2
+ using System.Windows;
3
+ using Dapplo.Windows.User32;
4
+ using ScreenGrab.Extensions;
5
+
6
+ namespace ScreenGrab;
7
+
8
+ public abstract class ScreenGrabber
9
+ {
10
+ public static bool IsCapturing { get; private set; }
11
+
12
+ public static Action<Bitmap>? OnCaptured { get; set; }
13
+
14
+ public static void Capture(bool isAuxiliary = false)
15
+ {
16
+ if (IsCapturing) return;
17
+
18
+ IsCapturing = true;
19
+
20
+ var allDisplayInfos = DisplayInfo.AllDisplayInfos;
21
+ var allScreenGrab = Application.Current.Windows.OfType<ScreenGrabView>().ToList();
22
+ var numberOfScreenGrabWindowsToCreate = allDisplayInfos.Length - allScreenGrab.Count;
23
+
24
+ for (var i = 0; i < numberOfScreenGrabWindowsToCreate; i++)
25
+ {
26
+ var view = new ScreenGrabView(OnCaptured, isAuxiliary)
27
+ {
28
+ OnGrabClose = () => IsCapturing = false
29
+ };
30
+ allScreenGrab.Add(view);
31
+ }
32
+
33
+ const double sideLength = 40;
34
+
35
+ foreach (var (displayInfo, screenGrab) in allDisplayInfos.Zip(allScreenGrab,
36
+ (displayInfo, screenGrab) => (displayInfo, screenGrab)))
37
+ {
38
+ screenGrab.WindowStartupLocation = WindowStartupLocation.Manual;
39
+ screenGrab.Width = sideLength;
40
+ screenGrab.Height = sideLength;
41
+ screenGrab.WindowState = WindowState.Normal;
42
+
43
+ var screenCenterPoint = displayInfo.ScaledCenterPoint();
44
+ screenGrab.Left = screenCenterPoint.X - sideLength / 2;
45
+ screenGrab.Top = screenCenterPoint.Y - sideLength / 2;
46
+
47
+ screenGrab.Show();
48
+ screenGrab.Activate();
49
+ }
50
+ }
51
+ }
src/ScreenGrab/UcCharacter.xaml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <UserControl
2
+ d:DesignHeight="450"
3
+ d:DesignWidth="800"
4
+ mc:Ignorable="d"
5
+ x:Class="ScreenGrab.UcCharacter"
6
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
7
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
10
+ <Border
11
+ BorderBrush="White"
12
+ BorderThickness="1"
13
+ Margin="2,0"
14
+ MinWidth="26"
15
+ Padding="3">
16
+ <Border.Resources>
17
+ <Style TargetType="TextBlock">
18
+ <Setter Property="Foreground" Value="White" />
19
+ <Setter Property="FontSize" Value="14" />
20
+ <Setter Property="HorizontalAlignment" Value="Center" />
21
+ <Setter Property="VerticalAlignment" Value="Center" />
22
+ </Style>
23
+ </Border.Resources>
24
+ <TextBlock Text="{Binding DisplayChar, RelativeSource={RelativeSource AncestorType=UserControl}}" x:Name="CharacterText" />
25
+ </Border>
26
+ </UserControl>
src/ScreenGrab/UcCharacter.xaml.cs ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Windows;
2
+ using System.Windows.Controls;
3
+
4
+ namespace ScreenGrab;
5
+
6
+ public partial class UcCharacter : UserControl
7
+ {
8
+ public static readonly DependencyProperty DisplayCharProperty =
9
+ DependencyProperty.Register(
10
+ "DisplayChar",
11
+ typeof(string),
12
+ typeof(UcCharacter),
13
+ new PropertyMetadata(string.Empty));
14
+
15
+ public UcCharacter()
16
+ {
17
+ InitializeComponent();
18
+ }
19
+
20
+ public string DisplayChar
21
+ {
22
+ get => (string)GetValue(DisplayCharProperty);
23
+ set => SetValue(DisplayCharProperty, value);
24
+ }
25
+ }
src/ScreenGrab/Utilities/CursorClipper.cs ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Windows;
2
+
3
+ namespace ScreenGrab.Utilities;
4
+
5
+ /// <summary>
6
+ /// Functions to constrain the mouse cursor (typically used when dragging)
7
+ /// </summary>
8
+ public static class CursorClipper
9
+ {
10
+ /// <summary>
11
+ /// Constrain mouse cursor to the area of the specified UI element.
12
+ /// </summary>
13
+ /// <param name="element">Target UI element.</param>
14
+ /// <returns>True on success.</returns>
15
+ public static bool ClipCursor(FrameworkElement element)
16
+ {
17
+ const double dpi96 = 96.0;
18
+
19
+ var topLeft = element.PointToScreen(new Point(0, 0));
20
+
21
+ var source = PresentationSource.FromVisual(element);
22
+ if (source?.CompositionTarget == null) return false;
23
+
24
+ var dpiX = dpi96 * source.CompositionTarget.TransformToDevice.M11;
25
+ var dpiY = dpi96 * source.CompositionTarget.TransformToDevice.M22;
26
+
27
+ var width = (int)((element.ActualWidth + 1) * dpiX / dpi96);
28
+ var height = (int)((element.ActualHeight + 1) * dpiY / dpi96);
29
+
30
+ var rect = new OSInterop.RECT
31
+ {
32
+ left = (int)topLeft.X,
33
+ top = (int)topLeft.Y,
34
+ right = (int)topLeft.X + width,
35
+ bottom = (int)topLeft.Y + height
36
+ };
37
+
38
+ return OSInterop.ClipCursor(ref rect);
39
+ }
40
+
41
+ /// <summary>
42
+ /// Remove any mouse cursor constraint.
43
+ /// </summary>
44
+ /// <returns>True on success.</returns>
45
+ public static bool UnClipCursor()
46
+ {
47
+ return OSInterop.ClipCursor(IntPtr.Zero);
48
+ }
49
+ }
src/ScreenGrab/Utilities/WindowUtilities.cs ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Runtime.InteropServices;
2
+ using System.Windows;
3
+ using System.Windows.Media;
4
+ using ScreenGrab.Extensions;
5
+
6
+ namespace ScreenGrab.Utilities;
7
+
8
+ public static class WindowUtilities
9
+ {
10
+ public static bool GetMousePosition(out Point mousePosition)
11
+ {
12
+ if (GetCursorPos(out var point))
13
+ {
14
+ mousePosition = new Point(point.X, point.Y);
15
+ return true;
16
+ }
17
+
18
+ mousePosition = default;
19
+ return false;
20
+ }
21
+
22
+ public static bool IsMouseInWindow(this Window window)
23
+ {
24
+ GetMousePosition(out var mousePosition);
25
+
26
+ var dpi = VisualTreeHelper.GetDpi(window);
27
+ var absPosPoint = window.GetAbsolutePosition();
28
+ Rect windowRect = new(absPosPoint.X, absPosPoint.Y,
29
+ window.ActualWidth * dpi.DpiScaleX,
30
+ window.ActualHeight * dpi.DpiScaleY);
31
+ return windowRect.Contains(mousePosition);
32
+ }
33
+
34
+ #region Dll Import
35
+
36
+ [DllImport("user32.dll")]
37
+ [return: MarshalAs(UnmanagedType.Bool)]
38
+ private static extern bool GetCursorPos(out POINT lpPoint);
39
+
40
+ #endregion
41
+
42
+ #region Dll Helper Structures
43
+
44
+ [StructLayout(LayoutKind.Sequential)]
45
+ public struct POINT
46
+ {
47
+ /// <summary>
48
+ /// x coordinate of point.
49
+ /// </summary>
50
+ public int X;
51
+
52
+ /// <summary>
53
+ /// y coordinate of point.
54
+ /// </summary>
55
+ public int Y;
56
+
57
+ /// <summary>
58
+ /// Construct a point of coordinates (x,y).
59
+ /// </summary>
60
+ public POINT(int x, int y)
61
+ {
62
+ X = x;
63
+ Y = y;
64
+ }
65
+ }
66
+
67
+ #endregion
68
+ }
src/ScreenGrab/Utilities/WrappingStream.cs ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.IO;
2
+ using ScreenGrab.Models;
3
+
4
+ namespace ScreenGrab.Utilities;
5
+
6
+ /// <summary>
7
+ /// A <see cref="Stream" /> that wraps another stream. The major feature of <see cref="WrappingStream" /> is that it
8
+ /// does not dispose the
9
+ /// underlying stream when it is disposed; this is useful when using classes such as <see cref="BinaryReader" /> and
10
+ /// <see cref="System.Security.Cryptography.CryptoStream" /> that take ownership of the stream passed to their
11
+ /// constructors.
12
+ /// </summary>
13
+ public class WrappingStream : Stream
14
+ {
15
+ /// <summary>
16
+ /// Initializes a new instance of the <see cref="WrappingStream" /> class.
17
+ /// </summary>
18
+ /// <param name="streamBase">The wrapped stream.</param>
19
+ public WrappingStream(Stream streamBase)
20
+ {
21
+ // check parameters
22
+
23
+ if (streamBase == null)
24
+ throw new ArgumentNullException("streamBase");
25
+ WrappedStream = streamBase;
26
+ }
27
+
28
+ /// <summary>
29
+ /// Gets a value indicating whether the current stream supports reading.
30
+ /// </summary>
31
+ /// <returns><c>true</c> if the stream supports reading; otherwise, <c>false</c>.</returns>
32
+ public override bool CanRead => WrappedStream == null ? false : WrappedStream.CanRead;
33
+
34
+ /// <summary>
35
+ /// Gets a value indicating whether the current stream supports seeking.
36
+ /// </summary>
37
+ /// <returns><c>true</c> if the stream supports seeking; otherwise, <c>false</c>.</returns>
38
+ public override bool CanSeek => WrappedStream == null ? false : WrappedStream.CanSeek;
39
+
40
+ /// <summary>
41
+ /// Gets a value indicating whether the current stream supports writing.
42
+ /// </summary>
43
+ /// <returns><c>true</c> if the stream supports writing; otherwise, <c>false</c>.</returns>
44
+ public override bool CanWrite => WrappedStream == null ? false : WrappedStream.CanWrite;
45
+
46
+ /// <summary>
47
+ /// Gets the length in bytes of the stream.
48
+ /// </summary>
49
+ public override long Length
50
+ {
51
+ get
52
+ {
53
+ ThrowIfDisposed();
54
+ if (WrappedStream is not null)
55
+ return WrappedStream.Length;
56
+
57
+ return 0;
58
+ }
59
+ }
60
+
61
+ /// <summary>
62
+ /// Gets or sets the position within the current stream.
63
+ /// </summary>
64
+ public override long Position
65
+ {
66
+ get
67
+ {
68
+ ThrowIfDisposed();
69
+ if (WrappedStream is not null)
70
+ return WrappedStream.Position;
71
+ return 0;
72
+ }
73
+ set
74
+ {
75
+ ThrowIfDisposed();
76
+ if (WrappedStream is not null)
77
+ WrappedStream.Position = value;
78
+ }
79
+ }
80
+
81
+ /// <summary>
82
+ /// Gets the wrapped stream.
83
+ /// </summary>
84
+ /// <value>The wrapped stream.</value>
85
+ protected Stream? WrappedStream { get; private set; }
86
+
87
+ /// <summary>
88
+ /// Begins an asynchronous read operation.
89
+ /// </summary>
90
+ public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
91
+ {
92
+ ThrowIfDisposed();
93
+
94
+ if (WrappedStream is not null && callback is not null && state is not null)
95
+ return WrappedStream.BeginRead(buffer, offset, count, callback, state);
96
+
97
+ return new NullAsyncResult();
98
+ }
99
+
100
+ /// <summary>
101
+ /// Begins an asynchronous write operation.
102
+ /// </summary>
103
+ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback,
104
+ object? state)
105
+ {
106
+ ThrowIfDisposed();
107
+
108
+ if (WrappedStream is not null && callback is not null && state is not null)
109
+ return WrappedStream.BeginWrite(buffer, offset, count, callback, state);
110
+
111
+ return new NullAsyncResult();
112
+ }
113
+
114
+ /// <summary>
115
+ /// Waits for the pending asynchronous read to complete.
116
+ /// </summary>
117
+ public override int EndRead(IAsyncResult asyncResult)
118
+ {
119
+ ThrowIfDisposed();
120
+
121
+ if (WrappedStream is not null)
122
+ return WrappedStream.EndRead(asyncResult);
123
+
124
+ return 0;
125
+ }
126
+
127
+ /// <summary>
128
+ /// Ends an asynchronous write operation.
129
+ /// </summary>
130
+ public override void EndWrite(IAsyncResult asyncResult)
131
+ {
132
+ ThrowIfDisposed();
133
+
134
+ if (WrappedStream is not null)
135
+ WrappedStream.EndWrite(asyncResult);
136
+ }
137
+
138
+ /// <summary>
139
+ /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
140
+ /// </summary>
141
+ public override void Flush()
142
+ {
143
+ ThrowIfDisposed();
144
+
145
+ if (WrappedStream is not null)
146
+ WrappedStream.Flush();
147
+ }
148
+
149
+ /// <summary>
150
+ /// Reads a sequence of bytes from the current stream and advances the position
151
+ /// within the stream by the number of bytes read.
152
+ /// </summary>
153
+ public override int Read(byte[] buffer, int offset, int count)
154
+ {
155
+ ThrowIfDisposed();
156
+
157
+ if (WrappedStream is not null)
158
+ return WrappedStream.Read(buffer, offset, count);
159
+ return 0;
160
+ }
161
+
162
+ /// <summary>
163
+ /// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end
164
+ /// of the stream.
165
+ /// </summary>
166
+ public override int ReadByte()
167
+ {
168
+ ThrowIfDisposed();
169
+
170
+ if (WrappedStream is not null)
171
+ return WrappedStream.ReadByte();
172
+ return 0;
173
+ }
174
+
175
+ /// <summary>
176
+ /// Sets the position within the current stream.
177
+ /// </summary>
178
+ /// <param name="offset">A byte offset relative to the <paramref name="origin" /> parameter.</param>
179
+ /// <param name="origin">
180
+ /// A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to
181
+ /// obtain the new position.
182
+ /// </param>
183
+ /// <returns>The new position within the current stream.</returns>
184
+ public override long Seek(long offset, SeekOrigin origin)
185
+ {
186
+ ThrowIfDisposed();
187
+
188
+ if (WrappedStream is not null)
189
+ return WrappedStream.Seek(offset, origin);
190
+ return 0;
191
+ }
192
+
193
+ /// <summary>
194
+ /// Sets the length of the current stream.
195
+ /// </summary>
196
+ /// <param name="value">The desired length of the current stream in bytes.</param>
197
+ public override void SetLength(long value)
198
+ {
199
+ ThrowIfDisposed();
200
+
201
+ if (WrappedStream is not null)
202
+ WrappedStream.SetLength(value);
203
+ }
204
+
205
+ /// <summary>
206
+ /// Writes a sequence of bytes to the current stream and advances the current position
207
+ /// within this stream by the number of bytes written.
208
+ /// </summary>
209
+ public override void Write(byte[] buffer, int offset, int count)
210
+ {
211
+ ThrowIfDisposed();
212
+
213
+ if (WrappedStream is not null)
214
+ WrappedStream.Write(buffer, offset, count);
215
+ }
216
+
217
+ /// <summary>
218
+ /// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
219
+ /// </summary>
220
+ public override void WriteByte(byte value)
221
+ {
222
+ ThrowIfDisposed();
223
+
224
+ if (WrappedStream is not null)
225
+ WrappedStream.WriteByte(value);
226
+ }
227
+
228
+ /// <summary>
229
+ /// Releases the unmanaged resources used by the <see cref="WrappingStream" /> and optionally releases the managed
230
+ /// resources.
231
+ /// </summary>
232
+ /// <param name="disposing">
233
+ /// true to release both managed and unmanaged resources; false to release only unmanaged
234
+ /// resources.
235
+ /// </param>
236
+ protected override void Dispose(bool disposing)
237
+ {
238
+ // doesn't close the base stream, but just prevents access to it through this WrappingStream
239
+
240
+ if (disposing)
241
+ WrappedStream = null;
242
+ base.Dispose(disposing);
243
+ }
244
+
245
+ private void ThrowIfDisposed()
246
+ {
247
+ // throws an ObjectDisposedException if this object has been disposed
248
+
249
+ if (WrappedStream == null)
250
+ throw new ObjectDisposedException(GetType().Name);
251
+ }
252
+ }
src/WeChatOcrCpp/.vscode/settings.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "python-envs.defaultEnvManager": "ms-python.python:conda",
3
+ "python-envs.defaultPackageManager": "ms-python.python:conda",
4
+ "python-envs.pythonProjects": []
5
+ }
src/WpfEditor/ComicOcr.cs CHANGED
@@ -21,13 +21,7 @@ using Formatting = Newtonsoft.Json.Formatting;
21
  namespace WpfEditor
22
  {
23
 
24
- // see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyunV2.js
25
- // see huggingface\imradv3\src\WeChatOcrCpp\ocr_aliyun.py
26
-
27
- // // https://help.aliyun.com/zh/ocr/developer-reference/api-ocr-api-2021-07-07-recognizeadvanced#api-detail-35
28
- // https://github.com/aliyun/alibabacloud-csharp-sdk
29
- // https://github.com/aliyun/alibabacloud-csharp-sdk/blob/master/ocr-api-20210707/core/Client.cs
30
- // dotnet add package AlibabaCloud.SDK.Ocr-api20210707
31
 
32
  public static class ComicOcr
33
  {
 
21
  namespace WpfEditor
22
  {
23
 
24
+ // see huggingface/comicocr/main.py
 
 
 
 
 
 
25
 
26
  public static class ComicOcr
27
  {
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -38,6 +38,9 @@ using Window = System.Windows.Window;
38
  # 图片导出
39
  see huggingface/project/ocr/ocrClient
40
  # 图片压缩
 
 
 
41
  */
42
 
43
  namespace WpfEditor
 
38
  # 图片导出
39
  see huggingface/project/ocr/ocrClient
40
  # 图片压缩
41
+ see https://github.com/lellid/LediReader
42
+ # epub wpf显示
43
+
44
  */
45
 
46
  namespace WpfEditor