-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullScreenMode.cs
524 lines (416 loc) · 16.7 KB
/
FullScreenMode.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ZeroitDevSolutionEditor
{
public partial class FullScreenMode : FormStyler
{
#region Private Fields
private bool canPaint = false;
private Graphics g;
private Color penColor = Color.Cyan;
private float penSize = 1f;
private int? prevX = null;
private int? prevY = null;
List<PictureBox> pages = new List<PictureBox>();
List<Bitmap> screenShotImages = new List<Bitmap>();
List<Label> numbering = new List<Label>()
{
new Label()
};
Bitmap screenShotImage = null;
private bool fullScreen = true;
#endregion
public bool FullScreen
{
get { return fullScreen; }
}
public Bitmap ScreenShot
{
get { return screenShotImage; }
}
public FullScreenMode()
{
InitializeComponent();
}
public FullScreenMode(Image image)
{
InitializeComponent();
mainBoard.Image = image;
mainBoard.SizeMode = PictureBoxSizeMode.Zoom;
this.DrawBorder = false;
}
#region Screen Shot
private Bitmap MakeScreenshot(Rectangle rect)
{
// Capture an rectangle area from the screen
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
#region Old Code
using (Graphics g = Graphics.FromImage(bmp))
{
// The standard C# screen capturing
g.CopyFromScreen(rect.Location, new Point(rect.Location.X, rect.Location.Y), bmp.Size);
//this.Refresh();
}
#endregion
//this.Refresh();
return bmp;
}
private Bitmap MakeScreenshot(Control control)
{
// Capture an rectangle area from the screen
Bitmap bmp = new Bitmap(control.Width, control.Height);
#region Old Code
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(
control.PointToScreen(control.ClientRectangle.Location),
new Point(0, 0), control.ClientRectangle.Size
);
}
#endregion
//this.Refresh();
return bmp;
}
#region Windows API
/// <summary>
/// This private class is holding all used Win32 API calls and constants
/// </summary>
private static class W32
{
/// <summary>
/// Copy the source with BitBlt
/// </summary>
public const int SRCCOPY = 0x00CC0020;
/// <summary>
/// Copy alpha / layered with BitBlt
/// </summary>
public const int CAPTUREBLT = 0x40000000;
/// <summary>
/// BitBlt method from the GDI API
/// </summary>
/// <param name="hDestDC">Destination DC handle</param>
/// <param name="x">Destination x</param>
/// <param name="y">Destination y</param>
/// <param name="nWidth">Destination width</param>
/// <param name="nHeight">Destination height</param>
/// <param name="hSrcDC">Source DC handle</param>
/// <param name="xSrc">Source x</param>
/// <param name="ySrc">Source y</param>
/// <param name="dwRop">Options</param>
/// <returns>Result</returns>
[DllImport("gdi32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = false)]
public static extern UInt64 BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, System.Int32 dwRop);
/// <summary>
/// Get the desktop window handle
/// </summary>
/// <returns>Desktop window handle</returns>
[DllImport("user32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = false)]
public static extern IntPtr GetDesktopWindow();
/// <summary>
/// Get a window DC
/// </summary>
/// <param name="hwnd">Window handle</param>
/// <returns>Window DC</returns>
[DllImport("user32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = false)]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
/// <summary>
/// Release a DC
/// </summary>
/// <param name="hwnd">Window handle</param>
/// <param name="dc">Window DC</param>
/// <returns>Result</returns>
[DllImport("user32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = false)]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr dc);
}
#endregion
#endregion
#region Clear Board
private void Clear()
{
g.Clear(Color.Transparent);
mainBoard.BackColor = Color.Transparent;
mainBoard.Image = null;
Invalidate();
}
#endregion
#region Drawing
private void DrawingMouseDown(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) != 0)
{
canPaint = true;
}
}
private void DrawingMouseUp(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) != 0)
{
canPaint = false;
prevX = null;
prevY = null;
screenShotImage = MakeScreenshot(mainBoard);
}
}
private void DrawingMouseMove(object sender, MouseEventArgs e)
{
g = mainBoard.CreateGraphics();
if ((e.Button & MouseButtons.Left) != 0)
{
if (canPaint)
{
Pen pen = new Pen(penColor, penSize);
g.DrawLine(pen, new Point(prevX ?? e.X, prevY ?? e.Y), new Point(e.X, e.Y));
prevX = e.X;
prevY = e.Y;
}
}
}
#region Drawing on main form (To Be Used Later)
//protected override void OnMouseDown(MouseEventArgs e)
//{
// if ((e.Button & MouseButtons.Left) != 0)
// {
// canPaint = true;
// }
//}
//protected override void OnMouseMove(MouseEventArgs e)
//{
// g = this.CreateGraphics();
// if ((e.Button & MouseButtons.Left) != 0)
// {
// if (canPaint)
// {
// Pen pen = new Pen(penColor, penSize);
// g.DrawLine(pen, new Point(prevX ?? e.X, prevY ?? e.Y), new Point(e.X, e.Y));
// prevX = e.X;
// prevY = e.Y;
// }
// }
//}
//protected override void OnMouseUp(MouseEventArgs e)
//{
// if ((e.Button & MouseButtons.Left) != 0)
// {
// canPaint = false;
// prevX = null;
// prevY = null;
// }
//}
//protected override void OnPaint(PaintEventArgs e)
//{
// Graphics g = e.Graphics;
// g.SmoothingMode = SmoothingMode.HighQuality;
// DrawImage(g, Bounds);
// base.OnPaint(e);
//}
#endregion
#endregion
#region Image Designer
#region Include in paint method
/////////////////////////////////////////////////////////////////////////////////////////////////
// //
// ------------------------Add this to the Paint Method ------------------------ //
// //
// Rectangle R1 = new Rectangle(0, 0, Width, Height); //
// //
// PointF ipt = ImageLocation(GetStringFormat(ImageAlign), Size, ImageSize); //
// //
// if ((Image == null)) //
// { //
// G.DrawString(Text, Font, new SolidBrush(ForeColor), R1, new StringFormat //
// { //
// Alignment = _TextAlignment, //
// LineAlignment = StringAlignment.Center //
// }); //
// } //
// else //
// { //
// G.DrawImage(_Image, ipt.X, ipt.Y, ImageSize.Width, ImageSize.Height); //
// G.DrawString(Text, Font, new SolidBrush(ForeColor), R1, new StringFormat //
// { //
// Alignment = _TextAlignment, //
// LineAlignment = StringAlignment.Center //
// }); //
// } //
// //
/////////////////////////////////////////////////////////////////////////////////////////////////
#endregion
#region Include in Private Fields
private Image _Image;
private Size _ImageSize;
private ContentAlignment _ImageAlign = ContentAlignment.MiddleCenter;
private StringAlignment _TextAlignment = StringAlignment.Center;
private bool showText = false;
#endregion
#region Include in Public Properties
public Image Image
{
get { return _Image; }
set
{
if (value == null)
{
_ImageSize = Size.Empty;
}
else
{
_ImageSize = value.Size;
}
_Image = value;
Invalidate();
}
}
protected Size ImageSize
{
get { return _ImageSize; }
}
public ContentAlignment ImageAlign
{
get { return _ImageAlign; }
set
{
_ImageAlign = value;
Invalidate();
}
}
public bool ShowText
{
get { return showText; }
set
{
showText = value;
Invalidate();
}
}
public StringAlignment TextAlign
{
get { return _TextAlignment; }
set
{
_TextAlignment = value;
Invalidate();
}
}
#endregion
#region Include in Private Methods
private static PointF ImageLocation(StringFormat SF, SizeF Area, SizeF ImageArea)
{
PointF MyPoint = default(PointF);
switch (SF.Alignment)
{
case StringAlignment.Center:
MyPoint.X = Convert.ToSingle((Area.Width - ImageArea.Width) / 2);
break;
case StringAlignment.Near:
MyPoint.X = 2;
break;
case StringAlignment.Far:
MyPoint.X = Area.Width - ImageArea.Width - 2;
break;
}
switch (SF.LineAlignment)
{
case StringAlignment.Center:
MyPoint.Y = Convert.ToSingle((Area.Height - ImageArea.Height) / 2);
break;
case StringAlignment.Near:
MyPoint.Y = 2;
break;
case StringAlignment.Far:
MyPoint.Y = Area.Height - ImageArea.Height - 2;
break;
}
return MyPoint;
}
private StringFormat GetStringFormat(ContentAlignment _ContentAlignment)
{
StringFormat SF = new StringFormat();
switch (_ContentAlignment)
{
case ContentAlignment.MiddleCenter:
SF.LineAlignment = StringAlignment.Center;
SF.Alignment = StringAlignment.Center;
break;
case ContentAlignment.MiddleLeft:
SF.LineAlignment = StringAlignment.Center;
SF.Alignment = StringAlignment.Near;
break;
case ContentAlignment.MiddleRight:
SF.LineAlignment = StringAlignment.Center;
SF.Alignment = StringAlignment.Far;
break;
case ContentAlignment.TopCenter:
SF.LineAlignment = StringAlignment.Near;
SF.Alignment = StringAlignment.Center;
break;
case ContentAlignment.TopLeft:
SF.LineAlignment = StringAlignment.Near;
SF.Alignment = StringAlignment.Near;
break;
case ContentAlignment.TopRight:
SF.LineAlignment = StringAlignment.Near;
SF.Alignment = StringAlignment.Far;
break;
case ContentAlignment.BottomCenter:
SF.LineAlignment = StringAlignment.Far;
SF.Alignment = StringAlignment.Center;
break;
case ContentAlignment.BottomLeft:
SF.LineAlignment = StringAlignment.Far;
SF.Alignment = StringAlignment.Near;
break;
case ContentAlignment.BottomRight:
SF.LineAlignment = StringAlignment.Far;
SF.Alignment = StringAlignment.Far;
break;
}
return SF;
}
private void DrawImage(Graphics G, Rectangle R1)
{
//Rectangle R1 = new Rectangle(0, 0, Width, Height);
G.SmoothingMode = SmoothingMode.HighQuality;
PointF ipt = ImageLocation(GetStringFormat(ImageAlign), Size, ImageSize);
if ((Image == null))
{
if (ShowText)
G.DrawString(Text, Font, new SolidBrush(ForeColor), R1, new StringFormat
{
Alignment = _TextAlignment,
LineAlignment = StringAlignment.Center
});
}
else
{
G.DrawImage(_Image, ipt.X, ipt.Y, ImageSize.Width, ImageSize.Height);
if (ShowText)
G.DrawString(Text, Font, new SolidBrush(ForeColor), R1, new StringFormat
{
Alignment = _TextAlignment,
LineAlignment = StringAlignment.Center
});
}
}
#endregion
#endregion
private void exitFullScreenToolStripMenuItem_Click(object sender, EventArgs e)
{
//this.Close();
DialogResult = DialogResult.OK;
fullScreen = false;
}
private void clearBoardToolStripMenuItem_Click(object sender, EventArgs e)
{
Clear();
}
}
}