private void FlashlightForm_Load(object sender, EventArgs e) { // Set the bounds. this.Bounds = MainForm.GetScreenBounds(); }
void PictureBoxPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (mouseDown || capturingWindows) { Rectangle screenbounds = GetScreenBounds(); captureRect.Intersect(screenbounds); // crop what is outside the screen Rectangle fixedRect = new Rectangle(captureRect.X, captureRect.Y, captureRect.Width, captureRect.Height); fixedRect.X += Math.Abs(screenbounds.X); fixedRect.Y += Math.Abs(screenbounds.Y); g.FillRectangle(OverlayBrush, fixedRect); g.DrawRectangle(OverlayPen, fixedRect); // rulers int dist = 8; Pen rulerPen = new Pen(Color.SeaGreen); Brush bgBrush = new SolidBrush(Color.FromArgb(200, 217, 240, 227)); Font f = new Font(FontFamily.GenericSansSerif, 8); int hSpace = TextRenderer.MeasureText(captureRect.Width.ToString(), f).Width + 3; int vSpace = TextRenderer.MeasureText(captureRect.Height.ToString(), f).Height + 3; // horizontal ruler if (fixedRect.Width > hSpace + 3) { GraphicsPath p = Drawing.RoundedRectangle.Create2( fixedRect.X + (fixedRect.Width / 2 - hSpace / 2) + 3, fixedRect.Y - dist - 7, TextRenderer.MeasureText(captureRect.Width.ToString(), f).Width - 3, TextRenderer.MeasureText(captureRect.Width.ToString(), f).Height, 3); g.FillPath(bgBrush, p); g.DrawPath(rulerPen, p); g.DrawString(captureRect.Width.ToString(), f, rulerPen.Brush, fixedRect.X + (fixedRect.Width / 2 - hSpace / 2) + 3, fixedRect.Y - dist - 7); g.DrawLine(rulerPen, fixedRect.X, fixedRect.Y - dist, fixedRect.X + (fixedRect.Width / 2 - hSpace / 2), fixedRect.Y - dist); g.DrawLine(rulerPen, fixedRect.X + (fixedRect.Width / 2 + hSpace / 2), fixedRect.Y - dist, fixedRect.X + fixedRect.Width, fixedRect.Y - dist); g.DrawLine(rulerPen, fixedRect.X, fixedRect.Y - dist - 3, fixedRect.X, fixedRect.Y - dist + 3); g.DrawLine(rulerPen, fixedRect.X + fixedRect.Width, fixedRect.Y - dist - 3, fixedRect.X + fixedRect.Width, fixedRect.Y - dist + 3); } // vertical ruler if (fixedRect.Height > vSpace + 3) { GraphicsPath p = Drawing.RoundedRectangle.Create2( fixedRect.X - (TextRenderer.MeasureText(captureRect.Height.ToString(), f).Width) + 1, fixedRect.Y + (fixedRect.Height / 2 - vSpace / 2) + 2, TextRenderer.MeasureText(captureRect.Height.ToString(), f).Width - 3, TextRenderer.MeasureText(captureRect.Height.ToString(), f).Height - 1, 3); g.FillPath(bgBrush, p); g.DrawPath(rulerPen, p); g.DrawString(captureRect.Height.ToString(), f, rulerPen.Brush, fixedRect.X - (TextRenderer.MeasureText(captureRect.Height.ToString(), f).Width) + 1, fixedRect.Y + (fixedRect.Height / 2 - vSpace / 2) + 2); g.DrawLine(rulerPen, fixedRect.X - dist, fixedRect.Y, fixedRect.X - dist, fixedRect.Y + (fixedRect.Height / 2 - vSpace / 2)); g.DrawLine(rulerPen, fixedRect.X - dist, fixedRect.Y + (fixedRect.Height / 2 + vSpace / 2), fixedRect.X - dist, fixedRect.Y + fixedRect.Height); g.DrawLine(rulerPen, fixedRect.X - dist - 3, fixedRect.Y, fixedRect.X - dist + 3, fixedRect.Y); g.DrawLine(rulerPen, fixedRect.X - dist - 3, fixedRect.Y + fixedRect.Height, fixedRect.X - dist + 3, fixedRect.Y + fixedRect.Height); } // Display size of selected rectangle // Prepare the font and text. f = new Font(FontFamily.GenericSansSerif, 12); string t = captureRect.Width + " x " + captureRect.Height; // Calculate the scaled font size. SizeF extent = g.MeasureString(t, f); float hRatio = captureRect.Height / (extent.Height * 2); float wRatio = captureRect.Width / (extent.Width * 2); float ratio = (hRatio < wRatio ? hRatio : wRatio); float newSize = f.Size * ratio; if (newSize >= 4) // Only show if 4pt or larger. { if (newSize > 20) { newSize = 20; } // Draw the size. f = new Font(FontFamily.GenericSansSerif, newSize, FontStyle.Bold); g.DrawString(t, f, Brushes.LightSeaGreen, new PointF(fixedRect.X + ( captureRect.Width / 2) - (TextRenderer.MeasureText(t, f).Width / 2), fixedRect.Y + (captureRect.Height / 2) - (f.GetHeight() / 2))); } } else { if (cursorPos.X > 0 || cursorPos.Y > 0) { Pen p = new Pen(Color.LightSeaGreen); p.DashStyle = DashStyle.Dot; Rectangle b = MainForm.GetScreenBounds(); g.DrawLine(p, cursorPos.X, b.Y, cursorPos.X, b.Height); g.DrawLine(p, b.X, cursorPos.Y, b.Width, cursorPos.Y); Font f = new Font(FontFamily.GenericSansSerif, 8); string xy = cursorPos.X.ToString() + " x " + cursorPos.Y.ToString(); Brush bgBrush = new SolidBrush(Color.FromArgb(200, 217, 240, 227)); GraphicsPath gp = Drawing.RoundedRectangle.Create2( cursorPos.X + 5, cursorPos.Y + 5, TextRenderer.MeasureText(xy, f).Width - 3, TextRenderer.MeasureText(xy, f).Height, 3); g.FillPath(bgBrush, gp); g.DrawPath(new Pen(Color.SeaGreen), gp); g.DrawString(xy, f, new Pen(Color.SeaGreen).Brush, cursorPos.X + 5, cursorPos.Y + 5); } } }