HSkinnedSlider.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 HSkinnedSlider : UtilityLibrary.WinControls.SkinnedSlider
  14. {
  15. #region Class variables
  16. #endregion
  17. #region Constructors
  18. public HSkinnedSlider()
  19. {
  20. }
  21. public HSkinnedSlider(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.X);
  50. trackerPos = e.X;
  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 trackerWidth = 0;
  57. if ( trackerImageList != null )
  58. trackerWidth = trackerImageList.ImageSize.Width;
  59. int middlePos = rc.Left + trackerWidth/2;
  60. offset = middlePos - e.X;
  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 trackerWidth = 0;
  94. if ( trackerImageList != null )
  95. trackerWidth = trackerImageList.ImageSize.Width;
  96. if ( e.X <= rc.Left + trackerWidth/2 ) offset = 0;
  97. if ( e.X >= rc.Right - trackerWidth/2 ) offset = 0;
  98. Value = GetValue(e.X + offset);
  99. trackerPos = e.X + offset;
  100. invalidate = true;
  101. }
  102.             
  103. if ( invalidate )
  104. Invalidate();
  105. }
  106. protected override void OnMouseUp(MouseEventArgs e)
  107. {
  108. base.OnMouseUp(e);
  109. if ( e.Button != MouseButtons.Left) 
  110. return;
  111. Rectangle rc = ClientRectangle;
  112. int trackerWidth = 0;
  113. if ( trackerImageList != null )
  114.                 trackerWidth = trackerImageList.ImageSize.Width;
  115. if ( e.X <= rc.Left + trackerWidth/2 ) offset = 0;
  116. if ( e.X >= rc.Right - trackerWidth/2 ) offset = 0;
  117. // Process left button down
  118. Capture = false;
  119. Value = GetValue(e.X + offset);
  120. trackerPos = e.X + offset;
  121. drawState = DrawState.Normal;
  122. Invalidate();
  123. }
  124. protected override void DrawTracker(Graphics g)
  125. {
  126. Rectangle rc = ClientRectangle;
  127. // If we have the needed image
  128. if ( trackerImageList != null &&  trackerImageList.Images.Count > (int)drawState )
  129. {
  130. int trackerWidth = trackerImageList.ImageSize.Width;
  131. int trackerHeight = trackerImageList.ImageSize.Height;
  132. int x = trackerPos;
  133. if ( x + trackerWidth/2 > rc.Width - 1 )
  134. {
  135. // Divide and multiply by 2 to eliminate rounding error
  136. x = rc.Right - (trackerWidth/2*2) - 1;
  137. }
  138. else if ( x - trackerWidth/2 < 0 )
  139. x = 0;
  140. else
  141. x -= trackerWidth/2;
  142. Point pt = new Point(x, rc.Top + (rc.Height-trackerHeight)/2);
  143. trackerRect = new Rectangle(pt.X, pt.Y, trackerWidth, trackerHeight);
  144. g.DrawImage(trackerImageList.Images[(int)drawState], trackerRect);
  145. }
  146. }
  147. protected override int GetValue(int position)
  148. {
  149. int trackerWidth = 0;
  150. if ( trackerImageList != null )
  151. trackerWidth = trackerImageList.ImageSize.Width;
  152. if ( position > ClientRectangle.Right-trackerWidth/2)
  153. position = ClientRectangle.Right-trackerWidth/2;
  154. else if ( position < ClientRectangle.Left+trackerWidth/2 )
  155. position = ClientRectangle.Left+trackerWidth/2;
  156. return (position-trackerWidth/2)*(max-min)/(ClientRectangle.Width-trackerWidth);
  157. }
  158. protected override int GetPosition(int _value)
  159. {
  160. int trackerWidth = 0;
  161. if ( trackerImageList != null )
  162. trackerWidth = trackerImageList.ImageSize.Width;
  163. return _value*(ClientRectangle.Width-trackerWidth)/(max-min) + trackerWidth/2;
  164. }
  165. protected override void ResizeSkinnedSlider()
  166. {
  167. // Size control to be the size size as 
  168. // the background bitmap so that it draws as it were transparent
  169. Image bm = BackgroundImage;
  170. if ( bm != null )
  171. {
  172. Rectangle rc = Bounds;
  173. Bounds = new Rectangle(rc.Left, rc.Top, rc.Width, bm.Height);
  174. }
  175. }
  176. #endregion
  177. #region Implementation
  178. #endregion
  179. }
  180. }