-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAreaSelectorWindow.xaml.cs
More file actions
57 lines (52 loc) · 2.09 KB
/
AreaSelectorWindow.xaml.cs
File metadata and controls
57 lines (52 loc) · 2.09 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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
namespace OverlayScope
{
public partial class AreaSelectorWindow : Window
{
private Point _startPoint;
public Rect SelectedArea { get; private set; }
public AreaSelectorWindow()
{
InitializeComponent();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(this);
SelectionRectangle.SetValue(Canvas.LeftProperty, _startPoint.X);
SelectionRectangle.SetValue(Canvas.TopProperty, _startPoint.Y);
SelectionRectangle.Width = 0;
SelectionRectangle.Height = 0;
SelectionRectangle.Visibility = Visibility.Visible;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point currentPoint = e.GetPosition(this);
double x = Math.Min(_startPoint.X, currentPoint.X);
double y = Math.Min(_startPoint.Y, currentPoint.Y);
double width = Math.Abs(_startPoint.X - currentPoint.X);
double height = Math.Abs(_startPoint.Y - currentPoint.Y);
SelectionRectangle.SetValue(Canvas.LeftProperty, x);
SelectionRectangle.SetValue(Canvas.TopProperty, y);
SelectionRectangle.Width = width;
SelectionRectangle.Height = height;
}
}
private void Window_MouseUp(object sender, MouseButtonEventArgs e)
{
if (SelectionRectangle.Width > 0 && SelectionRectangle.Height > 0)
{
double x = (double)SelectionRectangle.GetValue(Canvas.LeftProperty);
double y = (double)SelectionRectangle.GetValue(Canvas.TopProperty);
SelectedArea = new Rect(x, y, SelectionRectangle.Width, SelectionRectangle.Height);
}
this.DialogResult = true;
this.Close();
}
}
}