UCTrackBar.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-29-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCTrackBar.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.ComponentModel;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCTrackBar.
  28. /// Implements the <see cref="System.Windows.Forms.Control" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.Control" />
  31. [DefaultEvent("ValueChanged")]
  32. public class UCTrackBar : Control
  33. {
  34. /// <summary>
  35. /// Occurs when [value changed].
  36. /// </summary>
  37. [Description("值改变事件"), Category("自定义")]
  38. public event EventHandler ValueChanged;
  39. /// <summary>
  40. /// The dcimal digits
  41. /// </summary>
  42. private int dcimalDigits = 0;
  43. /// <summary>
  44. /// Gets or sets the dcimal digits.
  45. /// </summary>
  46. /// <value>The dcimal digits.</value>
  47. [Description("值小数精确位数"), Category("自定义")]
  48. public int DcimalDigits
  49. {
  50. get { return dcimalDigits; }
  51. set { dcimalDigits = value; }
  52. }
  53. /// <summary>
  54. /// The line width
  55. /// </summary>
  56. private float lineWidth = 10;
  57. /// <summary>
  58. /// Gets or sets the width of the line.
  59. /// </summary>
  60. /// <value>The width of the line.</value>
  61. [Description("线宽度"), Category("自定义")]
  62. public float LineWidth
  63. {
  64. get { return lineWidth; }
  65. set { lineWidth = value; }
  66. }
  67. /// <summary>
  68. /// The minimum value
  69. /// </summary>
  70. private float minValue = 0;
  71. /// <summary>
  72. /// Gets or sets the minimum value.
  73. /// </summary>
  74. /// <value>The minimum value.</value>
  75. [Description("最小值"), Category("自定义")]
  76. public float MinValue
  77. {
  78. get { return minValue; }
  79. set
  80. {
  81. if (minValue > m_value)
  82. return;
  83. minValue = value;
  84. this.Refresh();
  85. }
  86. }
  87. /// <summary>
  88. /// The maximum value
  89. /// </summary>
  90. private float maxValue = 100;
  91. /// <summary>
  92. /// Gets or sets the maximum value.
  93. /// </summary>
  94. /// <value>The maximum value.</value>
  95. [Description("最大值"), Category("自定义")]
  96. public float MaxValue
  97. {
  98. get { return maxValue; }
  99. set
  100. {
  101. if (value < m_value)
  102. return;
  103. maxValue = value;
  104. this.Refresh();
  105. }
  106. }
  107. /// <summary>
  108. /// The m value
  109. /// </summary>
  110. private float m_value = 0;
  111. /// <summary>
  112. /// Gets or sets the value.
  113. /// </summary>
  114. /// <value>The value.</value>
  115. [Description("值"), Category("自定义")]
  116. public float Value
  117. {
  118. get { return this.m_value; }
  119. set
  120. {
  121. if (value > maxValue || value < minValue)
  122. return;
  123. var v = (float)Math.Round((double)value, dcimalDigits);
  124. if (m_value == v)
  125. return;
  126. this.m_value = v;
  127. this.Refresh();
  128. if (ValueChanged != null)
  129. {
  130. ValueChanged(this, null);
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// The m line color
  136. /// </summary>
  137. private Color m_lineColor = Color.FromArgb(228, 231, 237);
  138. /// <summary>
  139. /// Gets or sets the color of the line.
  140. /// </summary>
  141. /// <value>The color of the line.</value>
  142. [Description("线颜色"), Category("自定义")]
  143. public Color LineColor
  144. {
  145. get { return m_lineColor; }
  146. set
  147. {
  148. m_lineColor = value;
  149. this.Refresh();
  150. }
  151. }
  152. /// <summary>
  153. /// The m value color
  154. /// </summary>
  155. private Color m_valueColor = Color.FromArgb(255, 77, 59);
  156. /// <summary>
  157. /// Gets or sets the color of the value.
  158. /// </summary>
  159. /// <value>The color of the value.</value>
  160. [Description("值颜色"), Category("自定义")]
  161. public Color ValueColor
  162. {
  163. get { return m_valueColor; }
  164. set
  165. {
  166. m_valueColor = value;
  167. this.Refresh();
  168. }
  169. }
  170. /// <summary>
  171. /// The is show tips
  172. /// </summary>
  173. private bool isShowTips = true;
  174. /// <summary>
  175. /// Gets or sets a value indicating whether this instance is show tips.
  176. /// </summary>
  177. /// <value><c>true</c> if this instance is show tips; otherwise, <c>false</c>.</value>
  178. [Description("点击滑动时是否显示数值提示"), Category("自定义")]
  179. public bool IsShowTips
  180. {
  181. get { return isShowTips; }
  182. set { isShowTips = value; }
  183. }
  184. /// <summary>
  185. /// Gets or sets the tips format.
  186. /// </summary>
  187. /// <value>The tips format.</value>
  188. [Description("显示数值提示的格式化形式"), Category("自定义")]
  189. public string TipsFormat { get; set; }
  190. /// <summary>
  191. /// The m line rectangle
  192. /// </summary>
  193. RectangleF m_lineRectangle;
  194. /// <summary>
  195. /// The m track rectangle
  196. /// </summary>
  197. RectangleF m_trackRectangle;
  198. /// <summary>
  199. /// Initializes a new instance of the <see cref="UCTrackBar" /> class.
  200. /// </summary>
  201. public UCTrackBar()
  202. {
  203. this.Size = new Size(250, 30);
  204. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  205. this.SetStyle(ControlStyles.DoubleBuffer, true);
  206. this.SetStyle(ControlStyles.ResizeRedraw, true);
  207. this.SetStyle(ControlStyles.Selectable, true);
  208. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  209. this.SetStyle(ControlStyles.UserPaint, true);
  210. this.MouseDown += UCTrackBar_MouseDown;
  211. this.MouseMove += UCTrackBar_MouseMove;
  212. this.MouseUp += UCTrackBar_MouseUp;
  213. }
  214. /// <summary>
  215. /// The BLN down
  216. /// </summary>
  217. bool blnDown = false;
  218. /// <summary>
  219. /// Handles the MouseDown event of the UCTrackBar control.
  220. /// </summary>
  221. /// <param name="sender">The source of the event.</param>
  222. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  223. void UCTrackBar_MouseDown(object sender, MouseEventArgs e)
  224. {
  225. if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location))
  226. {
  227. blnDown = true;
  228. Value = minValue+((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
  229. ShowTips();
  230. }
  231. }
  232. /// <summary>
  233. /// Handles the MouseMove event of the UCTrackBar control.
  234. /// </summary>
  235. /// <param name="sender">The source of the event.</param>
  236. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  237. void UCTrackBar_MouseMove(object sender, MouseEventArgs e)
  238. {
  239. if (blnDown)
  240. {
  241. Value = minValue + ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
  242. ShowTips();
  243. }
  244. }
  245. /// <summary>
  246. /// Handles the MouseUp event of the UCTrackBar control.
  247. /// </summary>
  248. /// <param name="sender">The source of the event.</param>
  249. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  250. void UCTrackBar_MouseUp(object sender, MouseEventArgs e)
  251. {
  252. blnDown = false;
  253. if (frmTips != null && !frmTips.IsDisposed)
  254. {
  255. frmTips.Close();
  256. frmTips = null;
  257. }
  258. }
  259. /// <summary>
  260. /// The FRM tips
  261. /// </summary>
  262. Forms.FrmAnchorTips frmTips = null;
  263. /// <summary>
  264. /// Shows the tips.
  265. /// </summary>
  266. private void ShowTips()
  267. {
  268. if (isShowTips)
  269. {
  270. string strValue = Value.ToString();
  271. if (!string.IsNullOrEmpty(TipsFormat))
  272. {
  273. try
  274. {
  275. strValue = Value.ToString(TipsFormat);
  276. }
  277. catch { }
  278. }
  279. var p = this.PointToScreen(new Point((int)m_trackRectangle.X, (int)m_trackRectangle.Y));
  280. if (frmTips == null || frmTips.IsDisposed || !frmTips.Visible)
  281. {
  282. frmTips = Forms.FrmAnchorTips.ShowTips(new Rectangle(p.X, p.Y, (int)m_trackRectangle.Width, (int)m_trackRectangle.Height), strValue, Forms.AnchorTipsLocation.TOP, ValueColor, autoCloseTime: -1);
  283. }
  284. else
  285. {
  286. frmTips.RectControl = new Rectangle(p.X, p.Y, (int)m_trackRectangle.Width, (int)m_trackRectangle.Height);
  287. frmTips.StrMsg = strValue;
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// Handles the <see cref="E:Paint" /> event.
  293. /// </summary>
  294. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  295. protected override void OnPaint(PaintEventArgs e)
  296. {
  297. base.OnPaint(e);
  298. Graphics g = e.Graphics;
  299. g.SetGDIHigh();
  300. m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, this.Size.Width - lineWidth * 2, lineWidth);
  301. GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, 5);
  302. g.FillPath(new SolidBrush(m_lineColor), pathLine);
  303. GraphicsPath valueLine = ControlHelper.CreateRoundedRectanglePath(new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, ((float)(m_value - minValue) / (float)(maxValue - minValue)) * m_lineRectangle.Width, lineWidth), 5);
  304. g.FillPath(new SolidBrush(m_valueColor), valueLine);
  305. m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)(m_value - minValue) / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * 2)), (this.Size.Height - lineWidth * 2) / 2, lineWidth * 2, lineWidth * 2);
  306. g.FillEllipse(new SolidBrush(m_valueColor), m_trackRectangle);
  307. g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / 4, m_trackRectangle.Y + m_trackRectangle.Height / 4, m_trackRectangle.Width / 2, m_trackRectangle.Height / 2));
  308. }
  309. }
  310. }