VSkinnedSlider.cs
上传用户:nnpulika
上传日期:2013-02-15
资源大小:597k
文件大小:5k
源码类别:

状态条

开发平台:

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. namespace UtilityLibrary.WinControls
  8. {
  9. /// <summary>
  10. /// 
  11. /// </summary>
  12. [ToolboxItem(false)] 
  13. public class VSkinnedSlider : UtilityLibrary.WinControls.SkinnedSlider
  14. {
  15. #region Class Variables
  16. #endregion
  17. #region Constructors
  18. public VSkinnedSlider()
  19. {
  20. }
  21. public VSkinnedSlider(Bitmap backgroundImage, ImageList trackerImageList) 
  22. : base(backgroundImage, trackerImageList)
  23. {
  24. }
  25. #endregion
  26. #region Overrides
  27. protected override void OnHandleCreated(EventArgs e)
  28. {
  29. base.OnHandleCreated(e);
  30. ResizeSkinnedSlider();
  31. }
  32. protected override void OnMouseDown(MouseEventArgs e)
  33. {
  34. base.OnMouseDown(e);
  35. if ( e.Button != MouseButtons.Left) 
  36. return;
  37. // Process left button down
  38. Capture = true;
  39. // use property to set the new value
  40. // so that the event Value change can be fired 
  41. // if it needs to
  42. Rectangle rc = trackerRect;
  43. if ( !(rc.Contains(new Point(e.X, e.Y))) )
  44. {
  45. // Only update the value when the mouse hit outside
  46. // the tracker, otherwise we get the strange effect
  47. // of the tracker value jumping to a new value when
  48. // we just want to start dragging the tracker
  49. Value = GetValue(e.Y);
  50. trackerPos = e.Y;
  51. }
  52. else
  53. {
  54. // If we hit the tracker, keep track of the offset
  55. // from the mouse hit to the middle of the tracker position
  56. int trackerHeight = 0;
  57. if ( trackerImageList != null )
  58. trackerHeight = trackerImageList.ImageSize.Height;
  59. int middlePos = rc.Top + trackerHeight/2;
  60. offset = middlePos - e.Y;
  61. }
  62. drawState = DrawState.Pressed;
  63. Invalidate();
  64. }
  65. protected override void OnMouseMove(MouseEventArgs e)
  66. {
  67. base.OnMouseMove(e);
  68. bool invalidate = false;
  69. Rectangle tracker = trackerRect;
  70. if ( !Capture )
  71. {
  72. if ( tracker.Contains(new Point(e.X, e.Y) ) )
  73. {
  74. drawState = DrawState.Hot;
  75. }
  76. else
  77. {
  78. drawState = DrawState.Normal;
  79. }
  80. invalidate = true;
  81. }
  82.                         
  83. if ( e.Button != MouseButtons.Left) 
  84. {
  85. if ( invalidate )
  86. Invalidate();
  87. return;
  88. }
  89. // Process left button down
  90. if ( Capture )
  91. {
  92. Rectangle rc = ClientRectangle;
  93. int trackerHeight = 0;
  94. if ( trackerImageList != null )
  95. trackerHeight = trackerImageList.ImageSize.Height;
  96. if ( e.Y <= rc.Top + trackerHeight/2 ) offset = 0;
  97. if ( e.Y >= rc.Bottom - trackerHeight/2 ) offset = 0;
  98. Value = GetValue(e.Y + offset);
  99. trackerPos = e.Y + offset;
  100. invalidate = true;
  101. }
  102. if ( invalidate )
  103. Invalidate();
  104. }
  105. protected override void OnMouseUp(MouseEventArgs e)
  106. {
  107. base.OnMouseUp(e);
  108. if ( e.Button != MouseButtons.Left) 
  109. return;
  110. Rectangle rc = ClientRectangle;
  111. int trackerHeight = 0;
  112. if ( trackerImageList != null )
  113. trackerHeight = trackerImageList.ImageSize.Height;
  114. if ( e.Y <= rc.Top + trackerHeight/2 ) offset = 0;
  115. if ( e.Y >= rc.Bottom - trackerHeight/2 ) offset = 0;
  116. // Process left button down
  117. Capture = false;
  118. Value = GetValue(e.Y + offset);
  119. trackerPos = e.Y + offset;
  120. drawState = DrawState.Normal;
  121. Invalidate();
  122. }
  123. protected override void DrawTracker(Graphics g)
  124. {
  125. Rectangle rc = ClientRectangle;
  126. // If we have all the needed bitmaps
  127. if ( trackerImageList != null &&  trackerImageList.Images.Count > (int)drawState )
  128. {
  129. int trackerWidth = trackerImageList.ImageSize.Width;
  130. int trackerHeight = trackerImageList.ImageSize.Height;
  131. int y = trackerPos;
  132. if ( y + trackerHeight/2 > rc.Height - 1 )
  133. {
  134. // Divide and multiply by 2 to eliminate rounding error
  135. y = rc.Bottom - (trackerHeight/2*2) - 1;
  136. }
  137. else if ( y - trackerHeight/2 < 0 )
  138. y = 0;
  139. else
  140. y -= trackerHeight/2;
  141. Point pt = new Point(rc.Left + (rc.Width-trackerWidth)/2, y );
  142. trackerRect = new Rectangle(pt.X, pt.Y, trackerWidth, trackerHeight);
  143. g.DrawImage(trackerImageList.Images[(int)drawState], trackerRect);
  144. }
  145. }
  146. protected override int GetValue(int position)
  147. {
  148. int trackerHeight = 0;
  149. if ( trackerImageList != null )
  150. trackerHeight = trackerImageList.ImageSize.Height;
  151. if ( position > ClientRectangle.Bottom-trackerHeight/2)
  152. position = ClientRectangle.Bottom-trackerHeight/2;
  153. else if ( position < ClientRectangle.Top+trackerHeight/2 )
  154. position = ClientRectangle.Top+trackerHeight/2;
  155. return (position-trackerHeight/2)*(max-min)/(ClientRectangle.Height-trackerHeight);
  156. }
  157. protected override int GetPosition(int _value)
  158. {
  159. int trackerHeight = 0;
  160. if ( trackerImageList != null )
  161. trackerHeight = trackerImageList.ImageSize.Height;
  162. return _value*(ClientRectangle.Height-trackerHeight)/(max-min) + trackerHeight/2;
  163. }
  164. protected override void ResizeSkinnedSlider()
  165. {
  166. // Size control to be the size size as 
  167. // the background bitmap so that it draws as it were transparent
  168. Image bm = BackgroundImage;
  169. if ( bm != null )
  170. {
  171. Rectangle rc = Bounds;
  172. Bounds = new Rectangle(rc.Left, rc.Top, bm.Width, rc.Height);
  173. }
  174. }
  175. #endregion
  176. #region Implementation
  177.         #endregion
  178. }
  179. }