DoubleSlidingTrackbar.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing.Drawing2D;
  11. namespace SmartCoalApplication.Core.CustomControl
  12. {
  13. /// <summary>
  14. /// 自定义的双滑块的拖动条
  15. /// </summary>
  16. public partial class DoubleSlidingTrackbar : BaseUserControl
  17. {
  18. /// <summary>
  19. /// 控件长度
  20. /// </summary>
  21. private int length = 0;
  22. /// <summary>
  23. /// 绘制左侧滑动块的点集合
  24. /// </summary>
  25. private List<Point> LeftPoints = new List<Point>();
  26. /// <summary>
  27. /// 绘制右侧滑动块的点集合
  28. /// </summary>
  29. private List<Point> RightPoints = new List<Point>();
  30. /// <summary>
  31. /// 鼠标按下状态
  32. /// </summary>
  33. private bool isPress = false;
  34. /// <summary>
  35. /// 鼠标按下时选中的滑块 1左 2右
  36. /// </summary>
  37. private int choise = -1;
  38. /// <summary>
  39. /// X轴的位移初始位置
  40. /// </summary>
  41. private int oldX = -1;
  42. /// <summary>
  43. /// 最大值
  44. /// </summary>
  45. public decimal maxValue = 0;
  46. /// <summary>
  47. /// 左侧滑块拖动事件
  48. /// </summary>
  49. public event EventHandler LeftHandlerDrag;
  50. private void OnLeftHandlerDrag()
  51. {
  52. if (LeftHandlerDrag != null)
  53. {
  54. LeftHandlerDrag(this, new EventArgs());
  55. }
  56. }
  57. /// <summary>
  58. /// 右侧滑块拖动事件
  59. /// </summary>
  60. public EventHandler RightHandlerDrag;
  61. private void OnRightHandlerDrag()
  62. {
  63. if (RightHandlerDrag != null)
  64. {
  65. RightHandlerDrag(this, new EventArgs());
  66. }
  67. }
  68. /// <summary>
  69. /// 控件长度
  70. /// </summary>
  71. /// <param name="length"></param>
  72. public DoubleSlidingTrackbar(int length = 220)
  73. {
  74. SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
  75. this.length = length;
  76. InitializeComponent();
  77. this.Width = this.length;
  78. LeftPoints.Add(new Point(10, 5));
  79. LeftPoints.Add(new Point(16, 5));
  80. LeftPoints.Add(new Point(16, 20));
  81. LeftPoints.Add(new Point(10, 15));
  82. RightPoints.Add(new Point(16, 5));
  83. RightPoints.Add(new Point(22, 5));
  84. RightPoints.Add(new Point(22, 15));
  85. RightPoints.Add(new Point(16, 20));
  86. this.Paint += new PaintEventHandler(this.DoubleSlidingTrackbar_Paint);
  87. this.MouseDown += new MouseEventHandler(this.DoubleSlidingTrackbar_MouseDown);
  88. this.MouseMove += new MouseEventHandler(this.DoubleSlidingTrackbar_MouseMove);
  89. this.MouseUp += new MouseEventHandler(this.DoubleSlidingTrackbar_MouseUp);
  90. }
  91. private void DoubleSlidingTrackbar_MouseDown(object sender, MouseEventArgs e)
  92. {
  93. GraphicsPath leftPath = new GraphicsPath();
  94. leftPath.AddPolygon(LeftPoints.ToArray());
  95. Region leftRegion = new Region(leftPath);
  96. GraphicsPath rightPath = new GraphicsPath();
  97. rightPath.AddPolygon(RightPoints.ToArray());
  98. Region rightRegion = new Region(rightPath);
  99. if (leftRegion.IsVisible(new Point(e.X, e.Y)))
  100. {
  101. choise = 1;
  102. isPress = true;
  103. oldX = e.X;
  104. }
  105. if (rightRegion.IsVisible(new Point(e.X, e.Y)))
  106. {
  107. choise = 2;
  108. isPress = true;
  109. oldX = e.X;
  110. }
  111. }
  112. private void DoubleSlidingTrackbar_MouseMove(object sender, MouseEventArgs e)
  113. {
  114. if (isPress)
  115. {
  116. switch (choise)
  117. {
  118. //左
  119. case 1:
  120. //if(e.X <= RightPoints[0].X)//e.X >= 10 &&
  121. {
  122. /*if(this.LeftPoints[1].X + e.X - oldX <= this.RightPoints[0].X
  123. && this.LeftPoints[0].X + e.X - oldX >= 10)
  124. {
  125. for (int i = 0; i < this.LeftPoints.Count; i++)
  126. {
  127. Point point = this.LeftPoints[i];
  128. point.X += e.X - oldX;
  129. if (point.X < 10)
  130. break;
  131. else
  132. this.LeftPoints[i] = point;
  133. }
  134. }*/
  135. if (this.LeftPoints[0].X + e.X - oldX >= 10)
  136. {
  137. if (this.LeftPoints[1].X + e.X - oldX <= this.RightPoints[0].X)
  138. {
  139. for (int i = 0; i < this.LeftPoints.Count; i++)
  140. {
  141. Point point = this.LeftPoints[i];
  142. point.X += e.X - oldX;
  143. if (point.X < 10)
  144. {
  145. break;
  146. }
  147. else
  148. {
  149. this.LeftPoints[i] = point;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. LeftPoints.Clear();
  156. LeftPoints.Add(new Point(RightPoints[0].X - 6, 5));
  157. LeftPoints.Add(new Point(RightPoints[0].X, 5));
  158. LeftPoints.Add(new Point(RightPoints[0].X, 20));
  159. LeftPoints.Add(new Point(RightPoints[0].X - 6, 15));
  160. }
  161. OnLeftHandlerDrag();
  162. }
  163. }
  164. break;
  165. //右
  166. case 2:
  167. //if (e.X > LeftPoints[1].X)//e.X <= 210 &&
  168. {
  169. /*if (this.RightPoints[1].X + e.X - oldX <= 210
  170. && this.RightPoints[0].X + e.X - oldX>= LeftPoints[1].X)
  171. {
  172. for (int i = 0; i < this.RightPoints.Count; i++)
  173. {
  174. Point point = this.RightPoints[i];
  175. point.X += e.X - oldX;
  176. if (point.X > 210)
  177. break;
  178. else
  179. this.RightPoints[i] = point;
  180. }
  181. }*/
  182. if (this.RightPoints[1].X + e.X - oldX <= this.length - 10)
  183. {
  184. if (this.RightPoints[0].X + e.X - oldX >= LeftPoints[1].X)
  185. {
  186. for (int i = 0; i < this.RightPoints.Count; i++)
  187. {
  188. Point point = this.RightPoints[i];
  189. point.X += e.X - oldX;
  190. if (point.X > 210)
  191. {
  192. break;
  193. }
  194. else
  195. {
  196. this.RightPoints[i] = point;
  197. }
  198. }
  199. }
  200. else
  201. {
  202. RightPoints.Clear();
  203. RightPoints.Add(new Point(LeftPoints[1].X, 5));
  204. RightPoints.Add(new Point(LeftPoints[1].X + 6, 5));
  205. RightPoints.Add(new Point(LeftPoints[1].X + 6, 15));
  206. RightPoints.Add(new Point(LeftPoints[1].X, 20));
  207. }
  208. OnRightHandlerDrag();
  209. }
  210. }
  211. break;
  212. }
  213. oldX = e.X;
  214. this.Refresh();
  215. }
  216. }
  217. private void DoubleSlidingTrackbar_MouseUp(object sender, MouseEventArgs e)
  218. {
  219. oldX = -1;
  220. isPress = false;
  221. }
  222. private void DoubleSlidingTrackbar_Paint(object sender, PaintEventArgs e)
  223. {
  224. //绘制横线
  225. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(214,214,214)), new Rectangle(16, 9, this.length - 32, 3));
  226. //绘制第一个滑块
  227. e.Graphics.FillPolygon(new SolidBrush(Color.FromArgb(0, 120, 215)), this.LeftPoints.ToArray());
  228. //e.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(10, 5, 6, 10));
  229. //绘制第二个滑块
  230. e.Graphics.FillPolygon(new SolidBrush(Color.FromArgb(36, 171, 242)), this.RightPoints.ToArray());
  231. //e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), new Rectangle(16, 5, 6, 10));
  232. }
  233. public double GetLeftSliderValue()
  234. {
  235. decimal unitLenght = this.maxValue / (this.length - 32);
  236. return (double)((LeftPoints[1].X-16) * unitLenght);
  237. }
  238. public double GetRightSliderValue()
  239. {
  240. decimal unitLenght = this.maxValue / (this.length - 32);
  241. return (double)((RightPoints[0].X - 16) * unitLenght);
  242. }
  243. public void SetLeftSliderPos(double left)
  244. {
  245. if (!isPress)
  246. {
  247. decimal unitLenght = (this.length - 32) / this.maxValue;
  248. int pos = (int)(left * (double)unitLenght);
  249. LeftPoints.Clear();
  250. LeftPoints.Add(new Point(10 + pos, 5));
  251. LeftPoints.Add(new Point(16 + pos, 5));
  252. LeftPoints.Add(new Point(16 + pos, 20));
  253. LeftPoints.Add(new Point(10 + pos, 15));
  254. this.Refresh();
  255. }
  256. }
  257. public void SetRightSliderPos(double right)
  258. {
  259. if(!isPress)
  260. {
  261. decimal unitLenght = (this.length - 32) / this.maxValue;
  262. int pos = (int)(right * (double)unitLenght);
  263. RightPoints.Clear();
  264. RightPoints.Add(new Point(16+pos, 5));
  265. RightPoints.Add(new Point(22+pos, 5));
  266. RightPoints.Add(new Point(22+pos, 15));
  267. RightPoints.Add(new Point(16+pos, 20));
  268. this.Refresh();
  269. }
  270. }
  271. public void InitLeftRightPoints()
  272. {
  273. LeftPoints.Clear();
  274. LeftPoints.Add(new Point(10, 5));
  275. LeftPoints.Add(new Point(16, 5));
  276. LeftPoints.Add(new Point(16, 20));
  277. LeftPoints.Add(new Point(10, 15));
  278. RightPoints.Clear();
  279. RightPoints.Add(new Point(16, 5));
  280. RightPoints.Add(new Point(22, 5));
  281. RightPoints.Add(new Point(22, 15));
  282. RightPoints.Add(new Point(16, 20));
  283. this.Refresh();
  284. }
  285. }
  286. }