You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
11 KiB

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