SkinnedSlider.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 7k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using System.ComponentModel;
  6. using System.Windows.Forms;
  7. using UtilityLibrary.WinControls;
  8. namespace UtilityLibrary.WinControls
  9. {
  10. /// <summary>
  11. /// 
  12. /// </summary>
  13. [ToolboxItem(false)]
  14. public class SkinnedSlider : System.Windows.Forms.Control
  15. {
  16. #region Events
  17. public event EventHandler ValueChanged;
  18. #endregion
  19. #region Class Variables
  20. // Tracker images
  21. protected ImageList trackerImageList = null;
  22. // Property backing variables
  23. protected  int _value = 0;
  24. protected  int previousValue = 0;
  25. protected  int min = 0;
  26. protected  int max = 100;
  27. // Miscelaneous
  28. protected int trackerPos = 0;
  29. protected int offset = 0;
  30. protected DrawState drawState = DrawState.Normal;
  31. protected Rectangle trackerRect = Rectangle.Empty;
  32. #endregion
  33. #region Constructors
  34. public SkinnedSlider()
  35. {
  36. Initialize(null, null);
  37. }
  38. public SkinnedSlider(Bitmap backgroundImage, ImageList trackerImageList)
  39. {
  40. Initialize(backgroundImage, trackerImageList);
  41. }
  42. void Initialize(Bitmap backgroundImage, ImageList trackerImageList)
  43. {
  44. // We are going to do all of the painting so better setup the control
  45. // to use double buffering
  46. SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.ResizeRedraw
  47. | ControlStyles.Opaque |
  48. ControlStyles.UserPaint|ControlStyles.DoubleBuffer, true);
  49.             BackgroundImage = backgroundImage;
  50. this.trackerImageList = trackerImageList;
  51. }
  52. #endregion
  53. #region Overrides
  54. protected override void OnPaint(PaintEventArgs pe)
  55. {
  56. base.OnPaint(pe);
  57. Graphics g = pe.Graphics;
  58. // paint background
  59. DrawBackground(g);
  60. // The little tracker
  61. DrawTracker(g);
  62. }
  63. protected override void OnHandleCreated(EventArgs e)
  64. {
  65. base.OnHandleCreated(e);
  66. // Initalize the position value
  67. trackerPos = GetPosition(_value);
  68. }
  69. protected override void OnMouseEnter(EventArgs e)
  70. {
  71. // Set state to hot
  72. base.OnMouseEnter(e);
  73. Point pos = Control.MousePosition;
  74. pos = PointToClient(pos);
  75. if ( trackerRect.Contains(pos) )
  76. {
  77. drawState = DrawState.Hot;
  78. Invalidate();
  79. }
  80. }
  81. protected override void OnMouseLeave(EventArgs e)
  82. {
  83. // Set state to Normal
  84. base.OnMouseLeave(e);
  85. drawState = DrawState.Normal;
  86. Invalidate();
  87. }
  88.         #endregion
  89. #region Virtuals
  90. // Some of these virtual need to be implemented by the derived class
  91. // --They should have been abstract if it was not for the IDE desinger 
  92. // throwing an assertion if the class was abstract
  93. protected virtual void DrawBackground(Graphics g)
  94. {
  95. Rectangle rc = ClientRectangle;
  96. // If we have a background bitmap
  97. if ( BackgroundImage != null )
  98. {
  99. int imageWidth = BackgroundImage.Size.Width;
  100. int imageHeight =  BackgroundImage.Size.Height;
  101. // Paint background color first to allow transparent bitmaps
  102. using ( Brush b = new SolidBrush(BackColor) )
  103. {
  104. // Don't strech the bitmap height to avoid distortion
  105. // when using a horizontal slider, the width if using
  106. // a vertical slider
  107.                     HSkinnedSlider tempSlider = this as HSkinnedSlider;
  108. bool bHorizontal = tempSlider != null;
  109.                     
  110. // We assume that the bitmap is always same size of smaller
  111. // than the control size
  112. int top = rc.Top;
  113. int left = rc.Left;
  114. if ( bHorizontal ) 
  115. {
  116. if ( rc.Height > imageHeight)
  117. {
  118. top = rc.Top + (rc.Height - imageHeight)/2;
  119. imageWidth = rc.Width;
  120. }
  121. }
  122. else
  123. {
  124. if ( rc.Width > imageWidth)
  125. {
  126. left = rc.Left + (rc.Width - imageWidth)/2;
  127. imageHeight = rc.Height;
  128. }
  129. }
  130. // Draw background 
  131. g.FillRectangle(b, rc.Left, rc.Top, rc.Width, rc.Height);
  132. // We need to draw the background in chunks to avoid distorting the image corner
  133. // in case the image has rounded corner. We are assuming a corner area of 10 pixels
  134. // on both size, the rest will be the area we strech
  135. Image image = BackgroundImage;
  136. if ( bHorizontal )
  137. {
  138. // Draw start corner
  139. g.DrawImage(image, new Rectangle(left, top, 10, imageHeight), 0, 0, 10, imageHeight, GraphicsUnit.Pixel);
  140. // Draw middle part
  141. g.DrawImage(image, new Rectangle(left+10, top, rc.Width-20, imageHeight), 10, 0, image.Width-20, imageHeight, GraphicsUnit.Pixel);
  142. // Draw end corner
  143. g.DrawImage(image, new Rectangle(rc.Right-10, top, 10, imageHeight), image.Width-10, 0, 10, imageHeight, GraphicsUnit.Pixel);
  144. }
  145. else
  146. {
  147. g.DrawImage(image, new Rectangle(left, top, imageWidth, 10), 0, 0, imageWidth, 10, GraphicsUnit.Pixel);
  148. // Draw middle part
  149. g.DrawImage(image, new Rectangle(left, top+10, imageWidth, rc.Height-20), 0, 10, imageWidth, image.Height - 20, GraphicsUnit.Pixel);
  150. // Draw end corner
  151. g.DrawImage(image, new Rectangle(left, rc.Bottom-10, imageWidth, 10), 0, image.Height-10, imageWidth, 10, GraphicsUnit.Pixel);
  152. }
  153. }
  154. }
  155. }
  156. protected virtual void DrawTracker(Graphics g)
  157. {
  158. }
  159. protected virtual int GetValue(int position)
  160. {
  161. return 0;
  162. }
  163. protected virtual int GetPosition(int _value)
  164. {
  165. return 0;
  166. }
  167. protected virtual void ResizeSkinnedSlider()
  168. {
  169. // Let the base classes implement this functionality
  170. }
  171. #endregion
  172. #region Properties
  173. public int Value
  174. {
  175. set 
  176. {
  177. previousValue = _value;
  178. // Make sure we don't get out of bounds
  179. if ( value > max )
  180. _value = max;
  181. else if ( value < min )
  182.                     _value = min;
  183. else
  184. _value = value;
  185. // If value is actually changing
  186. if ( previousValue != value )
  187. {
  188. trackerPos = GetPosition(_value);
  189. Invalidate();
  190. FireValueChanged();
  191. }
  192. }
  193. get { return _value;}
  194. }
  195. public int PreviousValue
  196. {
  197. get { return previousValue;}
  198. }
  199. public ImageList TrackerImageList
  200. {
  201. set { trackerImageList = value; }
  202. get { return trackerImageList; }
  203. }
  204. public int Minimum
  205. {
  206. set { min = value; }
  207. get { return min; }
  208. }
  209. public int Maximum
  210. {
  211. set { max = value; }
  212. get { return max; }
  213. }
  214. public new Image BackgroundImage
  215. {
  216. set 
  217. {
  218. base.BackgroundImage = value;
  219. ResizeSkinnedSlider();
  220. }
  221. get { return base.BackgroundImage; }
  222. }
  223. #endregion
  224. #region Methods
  225. #endregion
  226. #region Implementation
  227. void FireValueChanged()
  228. {
  229. if ( ValueChanged != null )
  230. ValueChanged(this, EventArgs.Empty);
  231. }
  232.        
  233. #endregion
  234. }
  235. }