UCStep.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-17-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCStep.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.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using System.Drawing.Drawing2D;
  25. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCStep.
  29. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. [DefaultEvent("IndexChecked")]
  33. public partial class UCStep : UserControl
  34. {
  35. /// <summary>
  36. /// Occurs when [index checked].
  37. /// </summary>
  38. [Description("步骤更改事件"), Category("自定义")]
  39. public event EventHandler IndexChecked;
  40. /// <summary>
  41. /// The m step back color
  42. /// </summary>
  43. private Color m_stepBackColor = Color.FromArgb(189, 189, 189);
  44. /// <summary>
  45. /// 步骤背景色
  46. /// </summary>
  47. /// <value>The color of the step back.</value>
  48. [Description("步骤背景色"), Category("自定义")]
  49. public Color StepBackColor
  50. {
  51. get { return m_stepBackColor; }
  52. set
  53. {
  54. m_stepBackColor = value;
  55. Refresh();
  56. }
  57. }
  58. /// <summary>
  59. /// The m step fore color
  60. /// </summary>
  61. private Color m_stepForeColor = Color.FromArgb(255, 77, 59);
  62. /// <summary>
  63. /// 步骤前景色
  64. /// </summary>
  65. /// <value>The color of the step fore.</value>
  66. [Description("步骤前景色"), Category("自定义")]
  67. public Color StepForeColor
  68. {
  69. get { return m_stepForeColor; }
  70. set
  71. {
  72. m_stepForeColor = value;
  73. Refresh();
  74. }
  75. }
  76. /// <summary>
  77. /// The m step font color
  78. /// </summary>
  79. private Color m_stepFontColor = Color.White;
  80. /// <summary>
  81. /// 步骤文字颜色
  82. /// </summary>
  83. /// <value>The color of the step font.</value>
  84. [Description("步骤文字景色"), Category("自定义")]
  85. public Color StepFontColor
  86. {
  87. get { return m_stepFontColor; }
  88. set
  89. {
  90. m_stepFontColor = value;
  91. Refresh();
  92. }
  93. }
  94. /// <summary>
  95. /// The m step width
  96. /// </summary>
  97. private int m_stepWidth = 35;
  98. /// <summary>
  99. /// 步骤宽度
  100. /// </summary>
  101. /// <value>The width of the step.</value>
  102. [Description("步骤宽度景色"), Category("自定义")]
  103. public int StepWidth
  104. {
  105. get { return m_stepWidth; }
  106. set
  107. {
  108. m_stepWidth = value;
  109. Refresh();
  110. }
  111. }
  112. /// <summary>
  113. /// The m steps
  114. /// </summary>
  115. private string[] m_steps = new string[] { "step1", "step2", "step3" };
  116. /// <summary>
  117. /// Gets or sets the steps.
  118. /// </summary>
  119. /// <value>The steps.</value>
  120. [Description("步骤"), Category("自定义")]
  121. public string[] Steps
  122. {
  123. get { return m_steps; }
  124. set
  125. {
  126. if (m_steps == null || m_steps.Length <= 1)
  127. return;
  128. m_steps = value;
  129. Refresh();
  130. }
  131. }
  132. /// <summary>
  133. /// The m step index
  134. /// </summary>
  135. private int m_stepIndex = 0;
  136. /// <summary>
  137. /// Gets or sets the index of the step.
  138. /// </summary>
  139. /// <value>The index of the step.</value>
  140. [Description("步骤位置"), Category("自定义")]
  141. public int StepIndex
  142. {
  143. get { return m_stepIndex; }
  144. set
  145. {
  146. if (value > Steps.Length)
  147. return;
  148. m_stepIndex = value;
  149. Refresh();
  150. if (IndexChecked != null)
  151. {
  152. IndexChecked(this, null);
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// The m line width
  158. /// </summary>
  159. private int m_lineWidth = 2;
  160. /// <summary>
  161. /// Gets or sets the width of the line.
  162. /// </summary>
  163. /// <value>The width of the line.</value>
  164. [Description("连接线宽度,最小2"), Category("自定义")]
  165. public int LineWidth
  166. {
  167. get { return m_lineWidth; }
  168. set
  169. {
  170. if (value < 2)
  171. return;
  172. m_lineWidth = value;
  173. Refresh();
  174. }
  175. }
  176. /// <summary>
  177. /// The m img completed
  178. /// </summary>
  179. private Image m_imgCompleted = null;
  180. /// <summary>
  181. /// Gets or sets the img completed.
  182. /// </summary>
  183. /// <value>The img completed.</value>
  184. [Description("已完成步骤图片,当不为空时,已完成步骤将不再显示数字,建议24*24大小"), Category("自定义")]
  185. public Image ImgCompleted
  186. {
  187. get { return m_imgCompleted; }
  188. set
  189. {
  190. m_imgCompleted = value;
  191. Refresh();
  192. }
  193. }
  194. /// <summary>
  195. /// The m LST cache rect
  196. /// </summary>
  197. List<Rectangle> m_lstCacheRect = new List<Rectangle>();
  198. /// <summary>
  199. /// Initializes a new instance of the <see cref="UCStep" /> class.
  200. /// </summary>
  201. public UCStep()
  202. {
  203. InitializeComponent();
  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 += UCStep_MouseDown;
  211. }
  212. /// <summary>
  213. /// Handles the MouseDown event of the UCStep control.
  214. /// </summary>
  215. /// <param name="sender">The source of the event.</param>
  216. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  217. void UCStep_MouseDown(object sender, MouseEventArgs e)
  218. {
  219. var index = m_lstCacheRect.FindIndex(p => p.Contains(e.Location));
  220. if (index >= 0)
  221. {
  222. StepIndex = index + 1;
  223. }
  224. }
  225. /// <summary>
  226. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  227. /// </summary>
  228. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  229. protected override void OnPaint(PaintEventArgs e)
  230. {
  231. base.OnPaint(e);
  232. var g = e.Graphics;
  233. g.SetGDIHigh();
  234. if (m_steps != null && m_steps.Length > 0)
  235. {
  236. System.Drawing.SizeF sizeFirst = g.MeasureString(m_steps[0], this.Font);
  237. int y = (this.Height - m_stepWidth - 10 - (int)sizeFirst.Height) / 2;
  238. if (y < 0)
  239. y = 0;
  240. int intTxtY = y + m_stepWidth + 10;
  241. int intLeft = 0;
  242. if (sizeFirst.Width > m_stepWidth)
  243. {
  244. intLeft = (int)(sizeFirst.Width - m_stepWidth) / 2 + 1;
  245. }
  246. int intRight = 0;
  247. System.Drawing.SizeF sizeEnd = g.MeasureString(m_steps[m_steps.Length - 1], this.Font);
  248. if (sizeEnd.Width > m_stepWidth)
  249. {
  250. intRight = (int)(sizeEnd.Width - m_stepWidth) / 2 + 1;
  251. }
  252. int intSplitWidth = 20;
  253. intSplitWidth = (this.Width - m_steps.Length - (m_steps.Length * m_stepWidth) - intRight - intLeft) / (m_steps.Length - 1);
  254. if (intSplitWidth < 20)
  255. intSplitWidth = 20;
  256. m_lstCacheRect = new List<Rectangle>();
  257. for (int i = 0; i < m_steps.Length; i++)
  258. {
  259. #region 画圆,横线
  260. Rectangle rectEllipse = new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth), y), new Size(m_stepWidth, m_stepWidth));
  261. m_lstCacheRect.Add(rectEllipse);
  262. g.FillEllipse(new SolidBrush(m_stepBackColor), rectEllipse);
  263. if (m_stepIndex > i)
  264. {
  265. g.FillEllipse(new SolidBrush(m_stepForeColor), new Rectangle(new Point(intLeft + i * (m_stepWidth + intSplitWidth) + 2, y + 2), new Size(m_stepWidth - 4, m_stepWidth - 4)));
  266. }
  267. if (m_stepIndex > i && m_imgCompleted != null)
  268. {
  269. g.DrawImage(m_imgCompleted, new Rectangle(new Point((intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - 24) / 2), y + (m_stepWidth - 24) / 2), new Size(24, 24)), 0, 0, m_imgCompleted.Width, m_imgCompleted.Height, GraphicsUnit.Pixel, null);
  270. }
  271. else
  272. {
  273. System.Drawing.SizeF _numSize = g.MeasureString((i + 1).ToString(), this.Font);
  274. g.DrawString((i + 1).ToString(), Font, new SolidBrush(m_stepFontColor), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)_numSize.Width) / 2 + 1, y + (m_stepWidth - (int)_numSize.Height) / 2 + 1));
  275. }
  276. #endregion
  277. System.Drawing.SizeF sizeTxt = g.MeasureString(m_steps[i], this.Font);
  278. g.DrawString(m_steps[i], Font, new SolidBrush(m_stepIndex > i ? m_stepForeColor : m_stepBackColor), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + (m_stepWidth - (int)sizeTxt.Width) / 2 + 1, intTxtY));
  279. }
  280. for (int i = 0; i < m_steps.Length; i++)
  281. {
  282. if (m_stepIndex > i)
  283. {
  284. if (i != m_steps.Length - 1)
  285. {
  286. if (m_stepIndex == i + 1)
  287. {
  288. g.DrawLine(new Pen(m_stepForeColor, m_lineWidth), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth - 3, y + ((m_stepWidth) / 2)), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth + intSplitWidth / 2, y + ((m_stepWidth) / 2)));
  289. g.DrawLine(new Pen(m_stepBackColor, m_lineWidth), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth + intSplitWidth / 2, y + ((m_stepWidth) / 2)), new Point(intLeft + (i + 1) * (m_stepWidth + intSplitWidth) + 10, y + ((m_stepWidth) / 2)));
  290. }
  291. else
  292. {
  293. g.DrawLine(new Pen(m_stepForeColor, m_lineWidth), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth - 3, y + ((m_stepWidth) / 2)), new Point(intLeft + (i + 1) * (m_stepWidth + intSplitWidth) + 10, y + ((m_stepWidth) / 2)));
  294. }
  295. }
  296. }
  297. else
  298. {
  299. if (i != m_steps.Length - 1)
  300. {
  301. g.DrawLine(new Pen(m_stepBackColor, m_lineWidth), new Point(intLeft + i * (m_stepWidth + intSplitWidth) + m_stepWidth - 3, y + ((m_stepWidth) / 2)), new Point(intLeft + (i + 1) * (m_stepWidth + intSplitWidth) + 10, y + ((m_stepWidth) / 2)));
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. }