-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlCrop.cs
354 lines (300 loc) · 10.7 KB
/
ControlCrop.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Cropper
{
public class ControlCrop
{
public Rectangle rect;
public bool allowDeformingDuringMovement = false;
private bool mIsClick = false;
private bool mMove = false;
private int oldX;
private int oldY;
private int sizeNodeRect = 8;
private Bitmap mBmp = null;
private PosSizableRect nodeSelected = PosSizableRect.None;
private bool m_Visible = true;
private Color m_CropColor;
public Control m_Control { get; set; }
public bool Visible
{
get { return m_Visible; }
set
{
m_Visible = value;
{
switch (value)
{
case true: m_CropColor = m_Control.ForeColor; break;
case false: m_CropColor = Color.Transparent; break;
default:
break;
}
m_Control.Invalidate();
}
}
}
private void Visible_Changed(object sender, EventArgs e)
{
this.Visible = m_Visible;
}
//public Color CropColor { get; set m_CropColor; } = Color.Black;
public Color CropColor
{
get { return m_Control.ForeColor; }
set
{
m_CropColor = value;
m_Control.ForeColor = m_CropColor;
}
}
//public static Boolean ShowDragHandles { get; set; }
public ControlCrop()
{
}
public ControlCrop(Control control, Boolean isVisible)
{
m_Control = control;
//Set the default crop color
m_Control.ForeColor = Color.Black;
mIsClick = false;
//Make the crop area less than the full control
rect = new Rectangle(30, 30, control.Width - 60, control.Height - 60);
}
private enum PosSizableRect
{
UpMiddle, //Top
LeftMiddle, //Left
LeftBottom,//BottomLeft
LeftUp, //TopLeft
RightUp, //TopRight
RightMiddle, //Right
RightBottom,//BottomRight
BottomMiddle, //Bottom
None
};
public void Draw(Graphics g)
{
g.DrawRectangle(new Pen(m_CropColor), rect);
foreach (PosSizableRect pos in Enum.GetValues(typeof(PosSizableRect)))
{
g.DrawRectangle(new Pen(m_CropColor), GetRect(pos));
}
}
//public void SetBitmapFile(string filename)
//{
// this.mBmp = new Bitmap(filename);
//}
//public void SetBitmap(Bitmap bmp)
//{
// this.mBmp = bmp;
//}
public void SetControl(Control p)
{
this.m_Control = p;
m_Control.MouseDown += new MouseEventHandler(Control_MouseDown);
m_Control.MouseUp += new MouseEventHandler(Control_MouseUp);
m_Control.MouseMove += new MouseEventHandler(Control_MouseMove);
m_Control.Paint += new PaintEventHandler(Control_Paint);
m_Control.SizeChanged += new EventHandler(SizeChanged_Changed);
m_Control.VisibleChanged += new EventHandler(Visible_Changed);
m_Control.ForeColorChanged += new EventHandler(ForeColor_Changed);
}
private void ForeColor_Changed(object sender, EventArgs e)
{
CropColor = m_Control.ForeColor;
m_Control.Invalidate();
}
private void SizeChanged_Changed(object sender, EventArgs e)
{
//Update rectangle and grab handles to reflect new size
rect = new Rectangle(sizeNodeRect, sizeNodeRect, m_Control.Width - 20, m_Control.Height - 20);
}
private void Control_Paint(object sender, PaintEventArgs e)
{
try
{
Draw(e.Graphics);
}
catch (Exception exp)
{
System.Console.WriteLine(exp.Message);
}
}
private void Control_MouseDown(object sender, MouseEventArgs e)
{
mIsClick = true;
nodeSelected = PosSizableRect.None;
nodeSelected = GetNodeSelectable(e.Location);
if (rect.Contains(new Point(e.X, e.Y)))
{
mMove = true;
}
oldX = e.X;
oldY = e.Y;
}
private void Control_MouseUp(object sender, MouseEventArgs e)
{
mIsClick = false;
mMove = false;
}
private void Control_MouseMove(object sender, MouseEventArgs e)
{
ChangeCursor(e.Location);
if (mIsClick == false)
{
return;
}
Rectangle backupRect = rect;
switch (nodeSelected)
{
case PosSizableRect.LeftUp:
rect.X += e.X - oldX;
rect.Width -= e.X - oldX;
rect.Y += e.Y - oldY;
rect.Height -= e.Y - oldY;
break;
case PosSizableRect.LeftMiddle:
rect.X += e.X - oldX;
rect.Width -= e.X - oldX;
break;
case PosSizableRect.LeftBottom:
rect.Width -= e.X - oldX;
rect.X += e.X - oldX;
rect.Height += e.Y - oldY;
break;
case PosSizableRect.BottomMiddle:
rect.Height += e.Y - oldY;
break;
case PosSizableRect.RightUp:
rect.Width += e.X - oldX;
rect.Y += e.Y - oldY;
rect.Height -= e.Y - oldY;
break;
case PosSizableRect.RightBottom:
rect.Width += e.X - oldX;
rect.Height += e.Y - oldY;
break;
case PosSizableRect.RightMiddle:
rect.Width += e.X - oldX;
break;
case PosSizableRect.UpMiddle:
rect.Y += e.Y - oldY;
rect.Height -= e.Y - oldY;
break;
default:
if (mMove)
{
rect.X = rect.X + e.X - oldX;
rect.Y = rect.Y + e.Y - oldY;
}
break;
}
oldX = e.X;
oldY = e.Y;
if (rect.Width < 5 || rect.Height < 5)
{
rect = backupRect;
}
TestIfRectInsideArea();
m_Control.Invalidate();
}
private void TestIfRectInsideArea()
{
// Test if rectangle still inside the area.
if (rect.X < 0) rect.X = 0;
if (rect.Y < 0) rect.Y = 0;
if (rect.Width <= 0) rect.Width = 1;
if (rect.Height <= 0) rect.Height = 1;
if (rect.X + rect.Width > m_Control.Width)
{
rect.Width = m_Control.Width - rect.X - 1; // -1 to be still show
if (allowDeformingDuringMovement == false)
{
mIsClick = false;
}
}
if (rect.Y + rect.Height > m_Control.Height)
{
rect.Height = m_Control.Height - rect.Y - 1;// -1 to be still show
if (allowDeformingDuringMovement == false)
{
mIsClick = false;
}
}
}
private Rectangle CreateRectSizableNode(int x, int y)
{
return new Rectangle(x - sizeNodeRect / 2, y - sizeNodeRect / 2, sizeNodeRect, sizeNodeRect);
}
private Rectangle GetRect(PosSizableRect p)
{
switch (p)
{
case PosSizableRect.LeftUp:
return CreateRectSizableNode(rect.X, rect.Y);
case PosSizableRect.LeftMiddle:
return CreateRectSizableNode(rect.X, rect.Y + +rect.Height / 2);
case PosSizableRect.LeftBottom:
return CreateRectSizableNode(rect.X, rect.Y + rect.Height);
case PosSizableRect.BottomMiddle:
return CreateRectSizableNode(rect.X + rect.Width / 2, rect.Y + rect.Height);
case PosSizableRect.RightUp:
return CreateRectSizableNode(rect.X + rect.Width, rect.Y);
case PosSizableRect.RightBottom:
return CreateRectSizableNode(rect.X + rect.Width, rect.Y + rect.Height);
case PosSizableRect.RightMiddle:
return CreateRectSizableNode(rect.X + rect.Width, rect.Y + rect.Height / 2);
case PosSizableRect.UpMiddle:
return CreateRectSizableNode(rect.X + rect.Width / 2, rect.Y);
default:
return new Rectangle();
}
}
private PosSizableRect GetNodeSelectable(Point p)
{
foreach (PosSizableRect r in Enum.GetValues(typeof(PosSizableRect)))
{
if (GetRect(r).Contains(p))
{
return r;
}
}
return PosSizableRect.None;
}
private void ChangeCursor(Point p)
{
m_Control.Cursor = GetCursor(GetNodeSelectable(p));
}
/// <summary>
/// Get cursor for the handle
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
private Cursor GetCursor(PosSizableRect p)
{
switch (p)
{
case PosSizableRect.LeftUp:
return Cursors.SizeNWSE;
case PosSizableRect.LeftMiddle:
return Cursors.SizeWE;
case PosSizableRect.LeftBottom:
return Cursors.SizeNESW;
case PosSizableRect.BottomMiddle:
return Cursors.SizeNS;
case PosSizableRect.RightUp:
return Cursors.SizeNESW;
case PosSizableRect.RightBottom:
return Cursors.SizeNWSE;
case PosSizableRect.RightMiddle:
return Cursors.SizeWE;
case PosSizableRect.UpMiddle:
return Cursors.SizeNS;
default:
return Cursors.Default;
}
}
}
}