Widow – Blame information for rev 18

Subversion Repositories:
Rev:
Rev Author Line No. Line
13 office 1 using System;
2 using System.Drawing;
3 using System.Linq;
4 using System.Windows.Forms;
5  
6 namespace Widow
7 {
8 public partial class DrawOverlayForm : Form
9 {
10 public event EventHandler<WindowDrawnEventArgs> WindowDrawn;
11 #region Public Enums, Properties and Fields
12  
13 public bool IsMouseDown { get; set; }
14  
15 public Point StartLocation { get; set; }
16  
17 public Point StopLocation { get; set; }
18  
19 public SolidBrush DrawBrush { get; }
20  
21 public SolidBrush FormBrush { get; }
22  
23 #endregion
24  
25 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
26  
27 private Point CurrentLocation { get; set; }
28  
29 private volatile Bitmap _bitmap;
30  
31 #endregion
32  
33 #region Constructors, Destructors and Finalizers
34  
35 public DrawOverlayForm()
36 {
37 InitializeComponent();
38  
18 office 39 DrawBrush = new SolidBrush(Color.LightBlue);
13 office 40 FormBrush = new SolidBrush(Color.White);
41 }
42  
43 #endregion
44  
45 #region Event Handlers
46  
47 private void DrawOverlayForm_MouseDown(object sender, MouseEventArgs e)
48 {
49 IsMouseDown = true;
50 StartLocation = e.Location;
51 }
52  
53 private void DrawOverlayForm_MouseUp(object sender, MouseEventArgs e)
54 {
55 IsMouseDown = false;
56 StopLocation = e.Location;
57  
58 var left = Math.Min(StartLocation.X, StopLocation.X);
59 var top = Math.Min(StartLocation.Y, StopLocation.Y);
60 var width = Math.Abs(StopLocation.X - StartLocation.X);
61 var height = Math.Abs(StopLocation.Y - StartLocation.Y);
62  
63 WindowDrawn?.Invoke(this, new WindowDrawnEventArgs(left, top, width, height));
64  
65 Close();
66 }
67  
68 private void DrawOverlayForm_MouseMove(object sender, MouseEventArgs e)
69 {
70 CurrentLocation = e.Location;
71  
72 if (!IsMouseDown)
73 {
74 return;
75 }
76  
77 var selection = new Rectangle(
78 Math.Min(StartLocation.X, CurrentLocation.X),
79 Math.Min(StartLocation.Y, CurrentLocation.Y),
80 Math.Abs(StartLocation.X - CurrentLocation.X),
81 Math.Abs(StartLocation.Y - CurrentLocation.Y));
82  
83 _bitmap = new Bitmap(Width, Height);
84  
85 using (var canvas = Graphics.FromImage(_bitmap))
86 {
87 canvas.Clear(Color.White);
88 canvas.FillRectangle(DrawBrush, selection);
89 }
90  
91 Invalidate();
92 Update();
93 }
94  
95 private void DrawOverlayForm_Paint(object sender, PaintEventArgs e)
96 {
97 if (_bitmap == null)
98 {
99 return;
100 }
101  
102 var graphics = e.Graphics;
103  
104 graphics.DrawImage(_bitmap, Location.X, Location.Y, Width, Height);
105  
106 _bitmap?.Dispose();
107 _bitmap = null;
108 }
109  
110 #endregion
111 }
112 }