This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
frm_Snipper.cs
112 lines (88 loc) · 3.27 KB
/
frm_Snipper.cs
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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace hyperdesktop2
{
public partial class frm_Snipper : Form
{
public static Rectangle get_region()
{
var snipper = new frm_Snipper();
if (snipper.ShowDialog() == DialogResult.OK)
return snipper.rect;
return new Rectangle(0, 0, 0, 0);
}
public frm_Snipper()
{
InitializeComponent();
this.TopMost = true;
this.ShowInTaskbar = false;
this.DoubleBuffered = true;
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.Manual;
this.Cursor = Cursors.Cross;
this.TransparencyKey = Color.White;
this.BackColor = Color.White;
this.Opacity = 0.70;
this.Location = new Point(Screen_Bounds.bounds.Left, Screen_Bounds.bounds.Top);
this.Size = new Size(Screen_Bounds.bounds.Width, Screen_Bounds.bounds.Height);
}
public Rectangle rect { get; set; }
private Rectangle select = new Rectangle();
private Rectangle previous_select = new Rectangle();
private Point start;
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
start = e.Location;
select = new Rectangle(e.Location, new Size(0, 0));
this.Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
int x1 = Math.Min(e.X, start.X);
int y1 = Math.Min(e.Y, start.Y);
int x2 = Math.Max(e.X, start.X);
int y2 = Math.Max(e.Y, start.Y);
Invalidate(previous_select);
previous_select = select;
select = new Rectangle(x1, y1, x2 - x1, y2 - y1);
Invalidate(select);
// Invalidating the whole created the delay
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (select.Width <= 0 || select.Height <= 0)
return;
rect = new Rectangle(
Screen_Bounds.bounds.Left + select.Left,
Screen_Bounds.bounds.Top + select.Top,
select.Width,
select.Height
);
DialogResult = DialogResult.OK;
}
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(Color.Red);
protected override void OnPaint(PaintEventArgs e)
{
using (Region region = new Region(new Rectangle(0, 0, this.Width, this.Height)))
{
region.Exclude(select);
region.Intersect(e.ClipRectangle);
e.Graphics.FillRegion(brush, region);
e.Graphics.DrawRectangle(pen, select.X, select.Y, select.Width - 1, select.Height - 1);
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys key)
{
if (key == Keys.Escape)
this.DialogResult = DialogResult.Cancel;
return base.ProcessCmdKey(ref msg, key);
}
}
}