ToleranceSliderControl.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. namespace PaintDotNet.Measurement
  6. {
  7. internal class ToleranceSliderControl
  8. : Control
  9. {
  10. private bool tracking = false;
  11. private bool hovering = false;
  12. private bool isValid;
  13. private string toleranceText;
  14. private string percentageFormat;
  15. private float tolerance;
  16. public float Tolerance
  17. {
  18. get
  19. {
  20. return tolerance;
  21. }
  22. set
  23. {
  24. if (tolerance != value)
  25. {
  26. tolerance = Utility.Clamp(value, 0, 1);
  27. OnToleranceChanged();
  28. }
  29. }
  30. }
  31. public EventHandler ToleranceChanged;
  32. protected void OnToleranceChanged()
  33. {
  34. this.isValid = false;
  35. this.Invalidate();
  36. this.Update();
  37. if (ToleranceChanged != null)
  38. {
  39. ToleranceChanged(this, EventArgs.Empty);
  40. }
  41. }
  42. public void PerformToleranceChanged()
  43. {
  44. OnToleranceChanged();
  45. }
  46. protected Bitmap buffer = null;
  47. protected Graphics bufferGraphics = null;
  48. protected void UpdateBitmap()
  49. {
  50. this.Invalidate();
  51. if (buffer == null || buffer.Width != this.ClientSize.Width || buffer.Height != this.ClientSize.Height)
  52. {
  53. if (buffer != null)
  54. {
  55. buffer.Dispose();
  56. buffer = null;
  57. }
  58. buffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
  59. if (bufferGraphics != null)
  60. {
  61. bufferGraphics.Dispose();
  62. bufferGraphics = null;
  63. }
  64. bufferGraphics = Graphics.FromImage(buffer);
  65. }
  66. bufferGraphics.Clear(this.BackColor);
  67. using (LinearGradientBrush lgb = new LinearGradientBrush(this.ClientRectangle, Color.Black, Color.White, 0, false))
  68. {
  69. bufferGraphics.FillRectangle(lgb, 0, 0, ClientSize.Width, ClientSize.Height);
  70. }
  71. bufferGraphics.FillRectangle(Brushes.DarkBlue, 0.0f, 0.0f, ClientRectangle.Width * tolerance, this.ClientRectangle.Height);
  72. bufferGraphics.DrawRectangle(hovering ? Pens.White : Pens.Black, 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
  73. bufferGraphics.SmoothingMode = SmoothingMode.HighQuality;
  74. bufferGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
  75. using (Font ourFont = new Font(this.Font.FontFamily, 8.0f, this.Font.Style))
  76. {
  77. Brush textBrush;
  78. if (hovering)
  79. {
  80. textBrush = Brushes.White;
  81. }
  82. else
  83. {
  84. textBrush = Brushes.White;
  85. }
  86. int number = (int)(tolerance * 100);
  87. string text = string.Format(percentageFormat, number);
  88. bufferGraphics.DrawString(text, ourFont, textBrush, 2, 1);
  89. }
  90. this.isValid = true;
  91. }
  92. protected override void OnPaintBackground(PaintEventArgs pevent)
  93. {
  94. }
  95. protected override void OnPaint(PaintEventArgs e)
  96. {
  97. base.OnPaint(e);
  98. if (!isValid)
  99. {
  100. UpdateBitmap();
  101. }
  102. if (buffer != null)
  103. {
  104. Rectangle bounds = new Rectangle(0, 0, buffer.Width, buffer.Height);
  105. e.Graphics.DrawImage(buffer, bounds, bounds, GraphicsUnit.Pixel);
  106. }
  107. }
  108. protected override void OnMouseDown(MouseEventArgs e)
  109. {
  110. base.OnMouseDown(e);
  111. if (!tracking && (e.Button & MouseButtons.Left) == MouseButtons.Left)
  112. {
  113. tracking = true;
  114. isValid = false;
  115. this.Invalidate();
  116. this.Update();
  117. OnMouseMove(e);
  118. }
  119. }
  120. protected override void OnMouseMove(MouseEventArgs e)
  121. {
  122. base.OnMouseMove(e);
  123. if (tracking)
  124. {
  125. Tolerance = (float)e.X / this.ClientSize.Width;
  126. }
  127. }
  128. protected override void OnMouseUp(MouseEventArgs e)
  129. {
  130. base.OnMouseUp(e);
  131. if (tracking && (e.Button & MouseButtons.Left) == MouseButtons.Left)
  132. {
  133. tracking = false;
  134. isValid = false;
  135. this.Invalidate();
  136. this.Update();
  137. }
  138. }
  139. protected override void OnMouseEnter(EventArgs e)
  140. {
  141. base.OnMouseEnter(e);
  142. this.hovering = true;
  143. this.UpdateBitmap();
  144. this.Update();
  145. }
  146. protected override void OnMouseLeave(EventArgs e)
  147. {
  148. base.OnMouseLeave(e);
  149. this.hovering = false;
  150. this.UpdateBitmap();
  151. this.Update();
  152. }
  153. protected override void OnResize(EventArgs e)
  154. {
  155. base.OnResize(e);
  156. if (bufferGraphics != null)
  157. {
  158. bufferGraphics.Dispose();
  159. bufferGraphics = null;
  160. }
  161. if (buffer != null)
  162. {
  163. buffer.Dispose();
  164. buffer = null;
  165. }
  166. }
  167. public ToleranceSliderControl()
  168. {
  169. InitializeComponent();
  170. this.tolerance = 0.5f;
  171. this.toleranceText = PdnResources.GetString("ToleranceSliderControl.Tolerance");
  172. this.percentageFormat = PdnResources.GetString("ToleranceSliderControl.Percentage.Format");
  173. }
  174. /// <summary>
  175. /// Clean up any resources being used.
  176. /// </summary>
  177. protected override void Dispose(bool disposing)
  178. {
  179. if (disposing)
  180. {
  181. if (bufferGraphics != null)
  182. {
  183. bufferGraphics.Dispose();
  184. bufferGraphics = null;
  185. }
  186. if (buffer != null)
  187. {
  188. buffer.Dispose();
  189. buffer = null;
  190. }
  191. }
  192. base.Dispose(disposing);
  193. }
  194. private void InitializeComponent()
  195. {
  196. this.Name = "ToleranceSliderControl";
  197. }
  198. }
  199. }