|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Windows.Forms; |
| 4 | + |
| 5 | +namespace Measure |
| 6 | +{ |
| 7 | + class Overlay : Form |
| 8 | + { |
| 9 | + Point start; |
| 10 | + Point end; |
| 11 | + bool drawing; |
| 12 | + |
| 13 | + Pen linePen = new Pen(new SolidBrush(Color.FromArgb(255, Color.Magenta)), 1); |
| 14 | + Font Segoe = new Font("Segoe UI", 11, FontStyle.Regular); |
| 15 | + |
| 16 | + Bitmap screenshot = null; |
| 17 | + |
| 18 | + public Overlay() |
| 19 | + { |
| 20 | + this.WindowState = FormWindowState.Maximized; |
| 21 | + this.FormBorderStyle = FormBorderStyle.None; |
| 22 | + this.BackColor = Color.Black; |
| 23 | + this.DoubleBuffered = true; |
| 24 | + |
| 25 | + this.MouseDown += Overlay_MouseDown; |
| 26 | + this.MouseUp += Overlay_MouseUp; |
| 27 | + this.MouseMove += Overlay_MouseMove; |
| 28 | + this.Paint += Overlay_Paint; |
| 29 | + this.KeyUp += Overlay_KeyUp; |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + private void InitializeComponent() |
| 34 | + { |
| 35 | + this.Name = "Measure"; |
| 36 | + this.ShowInTaskbar = false; |
| 37 | + this.ResumeLayout(false); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Set start point as we initially click |
| 42 | + /// </summary> |
| 43 | + private void Overlay_MouseDown(object sender, MouseEventArgs e) |
| 44 | + { |
| 45 | + start = e.Location; |
| 46 | + drawing = true; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Stop drawing as we release the mouse. |
| 51 | + /// </summary> |
| 52 | + private void Overlay_MouseUp(object sender, MouseEventArgs e) |
| 53 | + { |
| 54 | + drawing = false; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// If we're drawing and moving the mouse, we set the end point to the current cursor location. |
| 59 | + /// </summary> |
| 60 | + private void Overlay_MouseMove(object sender, MouseEventArgs e) |
| 61 | + { |
| 62 | + if (drawing) |
| 63 | + { |
| 64 | + end = e.Location; |
| 65 | + // Cause active area of form to be redrawn |
| 66 | + this.Invalidate(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Draw |
| 72 | + /// </summary> |
| 73 | + /// <param name="sender"></param> |
| 74 | + /// <param name="e"></param> |
| 75 | + private void Overlay_Paint(object sender, PaintEventArgs e) |
| 76 | + { |
| 77 | + // Vertical line |
| 78 | + e.Graphics.DrawLine(linePen, start.X, start.Y, start.X, end.Y); |
| 79 | + |
| 80 | + // Horizontal line |
| 81 | + e.Graphics.DrawLine(linePen, start.X, end.Y, end.X, end.Y); |
| 82 | + |
| 83 | + // Diagonal line |
| 84 | + e.Graphics.DrawLine(linePen, start.X, start.Y, end.X, end.Y); |
| 85 | + |
| 86 | + // Boxes to show end points |
| 87 | + e.Graphics.FillPolygon(Brushes.LimeGreen, PointToBox(start)); |
| 88 | + e.Graphics.FillPolygon(Brushes.LimeGreen, PointToBox(end)); |
| 89 | + |
| 90 | + // Text coordinates of start- and endpoints. |
| 91 | + e.Graphics.DrawString(SimpleCoords(start), Segoe, Brushes.White, start); |
| 92 | + e.Graphics.DrawString(SimpleCoords(end), Segoe, Brushes.White, end); |
| 93 | + |
| 94 | + // Line lengths |
| 95 | + // Width |
| 96 | + int dx = end.X - start.X; |
| 97 | + e.Graphics.DrawString(Math.Abs(dx) + "px", Segoe, Brushes.White, start.X + dx / 2, end.Y); |
| 98 | + |
| 99 | + // Height |
| 100 | + int dy = end.Y - start.Y; |
| 101 | + e.Graphics.DrawString(Math.Abs(dy) + "px", Segoe, Brushes.White, start.X, start.Y + dy / 2); |
| 102 | + |
| 103 | + // Hypotenuse length |
| 104 | + double hyp = Math.Round(Math.Sqrt(dx * dx + dy * dy), 0, MidpointRounding.AwayFromZero); |
| 105 | + e.Graphics.DrawString(hyp + "px", Segoe, Brushes.White, start.X + dx / 2, start.Y + dy / 2); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Capture the escape or Q keys to exit. |
| 111 | + /// </summary> |
| 112 | + private void Overlay_KeyUp(object sender, KeyEventArgs e) |
| 113 | + { |
| 114 | + if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.Q) |
| 115 | + { |
| 116 | + this.DialogResult = DialogResult.OK; |
| 117 | + this.Close(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Convert a single point into a 5x5 pixel box |
| 123 | + /// </summary> |
| 124 | + /// <param name="point">Point to centre box on.</param> |
| 125 | + /// <returns>An array of points making a 5x5 rectangle centred on a specified point.</returns> |
| 126 | + private Point[] PointToBox(Point point) |
| 127 | + { |
| 128 | + return new Point[] { |
| 129 | + new Point(point.X - 2, point.Y - 2 ), |
| 130 | + new Point(point.X + 2, point.Y - 2 ), |
| 131 | + new Point(point.X + 2, point.Y + 2 ), |
| 132 | + new Point(point.X - 2, point.Y + 2 ) |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Override the background painting to fake a translucent form background. |
| 138 | + /// Draw instructions at this point to avoid redrawing every frame. |
| 139 | + /// </summary> |
| 140 | + protected override void OnPaintBackground(PaintEventArgs e) |
| 141 | + { |
| 142 | + // Take screenshot the first time. Won't update the screen, but otherwise it fades to black every frame as we take successively darker screenshots. |
| 143 | + if (screenshot == null) |
| 144 | + { |
| 145 | + using (Image image = new Bitmap(this.Width, this.Height)) |
| 146 | + { |
| 147 | + using (Graphics Surface = Graphics.FromImage(image)) |
| 148 | + { |
| 149 | + var BackgroundBrush = new SolidBrush(Color.FromArgb(128, Color.Black)); |
| 150 | + var DarkerBrush = new SolidBrush(Color.FromArgb(192, Color.Black)); |
| 151 | + |
| 152 | + Surface.CopyFromScreen(0, 0, 0, 0, this.Size); |
| 153 | + Surface.FillRectangle(BackgroundBrush, this.DisplayRectangle); |
| 154 | + |
| 155 | + // Measure and draw instructions. Draw a darker rectangle behind them. |
| 156 | + Size TextSize = ExpandSize(Size.Round(e.Graphics.MeasureString(Properties.Strings.Instructions, Segoe)), 5); |
| 157 | + Point TextOrigin = Point.Round(new PointF(this.Width / 2 - TextSize.Width / 2, this.Height - TextSize.Height - 10)); |
| 158 | + var TextBox = new Rectangle(TextOrigin, TextSize); |
| 159 | + Surface.FillRectangle(DarkerBrush, TextBox); |
| 160 | + Surface.DrawString(Properties.Strings.Instructions, Segoe, Brushes.White, TextBox); |
| 161 | + |
| 162 | + } |
| 163 | + screenshot = new Bitmap(image); |
| 164 | + } |
| 165 | + } |
| 166 | + e.Graphics.DrawImage(screenshot, 0, 0); |
| 167 | + } |
| 168 | + |
| 169 | + /// <summary> |
| 170 | + /// Get shorter version of the coordinates of a Point (or PointF). |
| 171 | + /// </summary> |
| 172 | + /// <param name="point">Point to return coords of.</param> |
| 173 | + /// <returns>Coordinates in the format "X,Y".</returns> |
| 174 | + public string SimpleCoords(PointF point) |
| 175 | + { |
| 176 | + return point.X + "," + point.Y; |
| 177 | + } |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Expand a given size by a given amount. |
| 181 | + /// </summary> |
| 182 | + /// <param name="size">Size to base our new size on.</param> |
| 183 | + /// <param name="expandBy">Amount to change dimensions by.</param> |
| 184 | + public Size ExpandSize(Size size, int expandBy) |
| 185 | + { |
| 186 | + return new Size(size.Width + expandBy, size.Height + expandBy); |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | +} |
0 commit comments