123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace PaintDotNet.Measurement
- {
- internal class ToleranceSliderControl
- : Control
- {
- private bool tracking = false;
- private bool hovering = false;
- private bool isValid;
- private string toleranceText;
- private string percentageFormat;
- private float tolerance;
- public float Tolerance
- {
- get
- {
- return tolerance;
- }
- set
- {
- if (tolerance != value)
- {
- tolerance = Utility.Clamp(value, 0, 1);
- OnToleranceChanged();
- }
- }
- }
- public EventHandler ToleranceChanged;
- protected void OnToleranceChanged()
- {
- this.isValid = false;
- this.Invalidate();
- this.Update();
- if (ToleranceChanged != null)
- {
- ToleranceChanged(this, EventArgs.Empty);
- }
- }
- public void PerformToleranceChanged()
- {
- OnToleranceChanged();
- }
- protected Bitmap buffer = null;
- protected Graphics bufferGraphics = null;
- protected void UpdateBitmap()
- {
- this.Invalidate();
- if (buffer == null || buffer.Width != this.ClientSize.Width || buffer.Height != this.ClientSize.Height)
- {
- if (buffer != null)
- {
- buffer.Dispose();
- buffer = null;
- }
- buffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
- if (bufferGraphics != null)
- {
- bufferGraphics.Dispose();
- bufferGraphics = null;
- }
- bufferGraphics = Graphics.FromImage(buffer);
- }
- bufferGraphics.Clear(this.BackColor);
- using (LinearGradientBrush lgb = new LinearGradientBrush(this.ClientRectangle, Color.Black, Color.White, 0, false))
- {
- bufferGraphics.FillRectangle(lgb, 0, 0, ClientSize.Width, ClientSize.Height);
- }
- bufferGraphics.FillRectangle(Brushes.DarkBlue, 0.0f, 0.0f, ClientRectangle.Width * tolerance, this.ClientRectangle.Height);
- bufferGraphics.DrawRectangle(hovering ? Pens.White : Pens.Black, 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
- bufferGraphics.SmoothingMode = SmoothingMode.HighQuality;
- bufferGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
- using (Font ourFont = new Font(this.Font.FontFamily, 8.0f, this.Font.Style))
- {
- Brush textBrush;
- if (hovering)
- {
- textBrush = Brushes.White;
- }
- else
- {
- textBrush = Brushes.White;
- }
- int number = (int)(tolerance * 100);
- string text = string.Format(percentageFormat, number);
- bufferGraphics.DrawString(text, ourFont, textBrush, 2, 1);
- }
- this.isValid = true;
- }
- protected override void OnPaintBackground(PaintEventArgs pevent)
- {
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- if (!isValid)
- {
- UpdateBitmap();
- }
- if (buffer != null)
- {
- Rectangle bounds = new Rectangle(0, 0, buffer.Width, buffer.Height);
- e.Graphics.DrawImage(buffer, bounds, bounds, GraphicsUnit.Pixel);
- }
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- if (!tracking && (e.Button & MouseButtons.Left) == MouseButtons.Left)
- {
- tracking = true;
- isValid = false;
- this.Invalidate();
- this.Update();
- OnMouseMove(e);
- }
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- if (tracking)
- {
- Tolerance = (float)e.X / this.ClientSize.Width;
- }
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- if (tracking && (e.Button & MouseButtons.Left) == MouseButtons.Left)
- {
- tracking = false;
- isValid = false;
- this.Invalidate();
- this.Update();
- }
- }
- protected override void OnMouseEnter(EventArgs e)
- {
- base.OnMouseEnter(e);
- this.hovering = true;
- this.UpdateBitmap();
- this.Update();
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- this.hovering = false;
- this.UpdateBitmap();
- this.Update();
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- if (bufferGraphics != null)
- {
- bufferGraphics.Dispose();
- bufferGraphics = null;
- }
- if (buffer != null)
- {
- buffer.Dispose();
- buffer = null;
- }
- }
- public ToleranceSliderControl()
- {
- InitializeComponent();
- this.tolerance = 0.5f;
- this.toleranceText = PdnResources.GetString("ToleranceSliderControl.Tolerance");
- this.percentageFormat = PdnResources.GetString("ToleranceSliderControl.Percentage.Format");
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (bufferGraphics != null)
- {
- bufferGraphics.Dispose();
- bufferGraphics = null;
- }
- if (buffer != null)
- {
- buffer.Dispose();
- buffer = null;
- }
- }
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- this.Name = "ToleranceSliderControl";
- }
- }
- }
|