DoubleSlidingTrackbar.cs 12 KB

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