-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormScreen.cs
More file actions
160 lines (146 loc) · 5.58 KB
/
FormScreen.cs
File metadata and controls
160 lines (146 loc) · 5.58 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AssistAnt
{
public partial class FormScreen : Form
{
private static FormScreen SingleForm = null;
private Action<Bitmap> Selected;
private Point SelectStartPoint;
private Point SelectEndPoint;
private bool MouseDown = false;
public static bool IsShow => SingleForm != null && SingleForm.Visible;
protected override bool ScaleChildren => false;
private Rectangle GetSelectArea()
{
var res = new Rectangle();
if (SelectEndPoint.X < SelectStartPoint.X)
{
res.X = SelectEndPoint.X;
res.Width = SelectStartPoint.X - SelectEndPoint.X;
}
else
{
res.X = SelectStartPoint.X;
res.Width = SelectEndPoint.X - SelectStartPoint.X;
}
if (SelectEndPoint.Y < SelectStartPoint.Y)
{
res.Y = SelectEndPoint.Y;
res.Height = SelectStartPoint.Y - SelectEndPoint.Y;
}
else
{
res.Y = SelectStartPoint.Y;
res.Height = SelectEndPoint.Y - SelectStartPoint.Y;
}
return res;
}
public FormScreen()
{
InitializeComponent();
}
public static void ScreenShow(Action<Bitmap> selected)
{
var area = new Rectangle();
int maxX = 0, maxY = 0;
foreach (var screen in Screen.AllScreens)
{
if (screen.Bounds.X < area.X) area.X = screen.Bounds.X;
if (screen.Bounds.Y < area.Y) area.Y = screen.Bounds.Y;
if (screen.Bounds.X + screen.Bounds.Width > maxX) maxX = screen.Bounds.X + screen.Bounds.Width;
if (screen.Bounds.Y + screen.Bounds.Height > maxY) maxY = screen.Bounds.Y + screen.Bounds.Height;
}
area.Width = maxX - area.X;
area.Height = maxY - area.Y;
var bm0 = new Bitmap(area.Width, area.Height);
Graphics GH = Graphics.FromImage(bm0);
GH.CopyFromScreen(area.X, area.Y, 0, 0, bm0.Size);
var bm1 = new Bitmap(area.Width, area.Height);
GH = Graphics.FromImage(bm1);
GH.DrawImage(bm0, 0, 0);
GH.FillRectangle(new SolidBrush(Color.FromArgb(100, 200, 200, 200)), 0, 0, area.Width, area.Height);
if (SingleForm == null) SingleForm = new FormScreen();
SingleForm.Top = area.Top;
SingleForm.Left = area.Left;
SingleForm.Width = area.Width;
SingleForm.Height = area.Height;
SingleForm.pictureBox1.Image = bm0;
SingleForm.pictureBox2.Image = bm1;
SingleForm.Selected = selected;
SingleForm.Show();
}
private void ScreenClose()
{
var area = GetSelectArea();
if (area.Width > 1 && area.Height > 1 && Selected != null)
{
var bm = new Bitmap(area.Width, area.Height);
Graphics GH = Graphics.FromImage(bm as Image);
GH.DrawImage(pictureBox1.Image, new Rectangle(0, 0, area.Width, area.Height), area, GraphicsUnit.Pixel);
Selected(bm);
}
pictureBox1.Image = null;
Hide();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
MouseDown = true;
SelectStartPoint = e.Location;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
MouseDown = false;
SelectEndPoint = e.Location;
ScreenClose();
}
private void FormScreen_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
SelectStartPoint = SelectEndPoint = new Point();
ScreenClose();
}
}
private void FormScreen_Leave(object sender, EventArgs e)
{
SelectStartPoint = SelectEndPoint = new Point();
ScreenClose();
}
private void FormScreen_Deactivate(object sender, EventArgs e)
{
SelectStartPoint = SelectEndPoint = new Point();
ScreenClose();
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (!MouseDown) return;
//восстанавливаем изображение на стром квадрате
var area = GetSelectArea();
var GH = Graphics.FromImage(SingleForm.pictureBox2.Image);
if (area.Width > 0 && area.Height > 0)
{
GH.DrawImage(SingleForm.pictureBox1.Image, area, area, GraphicsUnit.Pixel);
GH.FillRectangle(new SolidBrush(Color.FromArgb(100, 200, 200, 200)), area);
}
//рисуем новый квадрат
SelectEndPoint = e.Location;
area = GetSelectArea();
if (area.Width > 1 && area.Height > 1)
{
area.Width -= 1;
area.Height -= 1;
GH.DrawImage(SingleForm.pictureBox1.Image, area, area, GraphicsUnit.Pixel);
GH.DrawRectangle(new Pen(Color.FromArgb(41, 136, 126), 1), area);
}
SingleForm.pictureBox2.Image = SingleForm.pictureBox2.Image;
}
}
}