-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenRegionSelectionWindow.cs
More file actions
115 lines (96 loc) · 3.55 KB
/
ScreenRegionSelectionWindow.cs
File metadata and controls
115 lines (96 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Forms = System.Windows.Forms;
namespace KemTranslate
{
internal sealed class ScreenRegionSelectionWindow : Window
{
private readonly Canvas _root;
private readonly Rectangle _selectionRectangle;
private Point _startPoint;
private bool _isSelecting;
public ScreenRegionSelectionWindow()
{
var virtualScreen = Forms.SystemInformation.VirtualScreen;
Left = virtualScreen.Left;
Top = virtualScreen.Top;
Width = virtualScreen.Width;
Height = virtualScreen.Height;
WindowStyle = WindowStyle.None;
ResizeMode = ResizeMode.NoResize;
AllowsTransparency = true;
Background = new SolidColorBrush(Color.FromArgb(70, 0, 0, 0));
ShowInTaskbar = false;
Topmost = true;
Cursor = Cursors.Cross;
_root = new Canvas();
_selectionRectangle = new Rectangle
{
Stroke = Brushes.White,
StrokeThickness = 2,
Fill = new SolidColorBrush(Color.FromArgb(40, 255, 255, 255)),
Visibility = Visibility.Collapsed
};
_root.Children.Add(_selectionRectangle);
Content = _root;
PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown;
PreviewMouseMove += OnPreviewMouseMove;
PreviewMouseLeftButtonUp += OnPreviewMouseLeftButtonUp;
PreviewKeyDown += OnPreviewKeyDown;
}
public Rect SelectedBounds { get; private set; }
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(this);
_isSelecting = true;
_selectionRectangle.Visibility = Visibility.Visible;
UpdateSelection(_startPoint);
CaptureMouse();
}
private void OnPreviewMouseMove(object sender, MouseEventArgs e)
{
if (!_isSelecting)
return;
UpdateSelection(e.GetPosition(this));
}
private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!_isSelecting)
return;
_isSelecting = false;
ReleaseMouseCapture();
UpdateSelection(e.GetPosition(this));
if (SelectedBounds.Width < 2 || SelectedBounds.Height < 2)
{
DialogResult = false;
Close();
return;
}
DialogResult = true;
Close();
}
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Escape)
return;
DialogResult = false;
Close();
}
private void UpdateSelection(Point currentPoint)
{
double x = Math.Min(_startPoint.X, currentPoint.X);
double y = Math.Min(_startPoint.Y, currentPoint.Y);
double width = Math.Abs(currentPoint.X - _startPoint.X);
double height = Math.Abs(currentPoint.Y - _startPoint.Y);
Canvas.SetLeft(_selectionRectangle, x);
Canvas.SetTop(_selectionRectangle, y);
_selectionRectangle.Width = width;
_selectionRectangle.Height = height;
SelectedBounds = new Rect(Left + x, Top + y, width, height);
}
}
}