HuProcessBar.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.Windows.Forms;
  9. using System.Threading;
  10. using System.Drawing.Drawing2D;
  11. namespace BasicFramework
  12. {
  13. /// <summary>
  14. /// 自定义的进度条控件
  15. /// </summary>
  16. public partial class HuProcessBar : UserControl
  17. {
  18. /// <summary>
  19. /// 实例化一个进度条控件
  20. /// </summary>
  21. public HuProcessBar()
  22. {
  23. InitializeComponent();
  24. }
  25. #region 属性设置区
  26. private bool m_HasBorder = false;
  27. /// <summary>
  28. /// 边框是否有无
  29. /// </summary>
  30. [Description("边框的有无")]
  31. [DefaultValue(false)]
  32. public bool HasBorder
  33. {
  34. get { return m_HasBorder; }
  35. set { m_HasBorder = value; Invalidate(); }
  36. }
  37. //==============================================================
  38. private Color m_HuBorderColor = Color.Gray;
  39. [Description("边框的颜色")]
  40. [DefaultValue(typeof(Color),"Gray")]
  41. public Color HuBorderColor
  42. {
  43. get { return m_HuBorderColor; }
  44. set { m_HuBorderColor = value; Invalidate(); }
  45. }
  46. //==============================================================
  47. private int m_Subsections = 1;
  48. [Description("分割的段数,默认不分割")]
  49. [DefaultValue(1)]
  50. public int Subsections
  51. {
  52. get { return m_Subsections; }
  53. set { m_Subsections = value; Invalidate(); }
  54. }
  55. //==============================================================
  56. private Color m_FillColorStart = Color.Tomato;
  57. [Description("填充开始颜色")]
  58. [DefaultValue(typeof(Color), "Tomato")]
  59. public Color FillColorStart
  60. {
  61. get { return m_FillColorStart; }
  62. set { m_FillColorStart = value; Invalidate(); }
  63. }
  64. //==============================================================
  65. private Color m_FillColorEnd = Color.LimeGreen;
  66. [Description("填充结束颜色")]
  67. [DefaultValue(typeof(Color), "LimeGreen")]
  68. public Color FillColorEnd
  69. {
  70. get { return m_FillColorEnd; }
  71. set { m_FillColorEnd = value; Invalidate(); }
  72. }
  73. //=============================================================
  74. private double m_HuSetValue = 0;
  75. [Description("设定值")]
  76. [DefaultValue(0)]
  77. public double HuSetValue
  78. {
  79. get { return m_HuSetValue; }
  80. set
  81. {
  82. if (Math.Abs(value - m_HuSetValue) < 0.002) return;
  83. if (value < 0 || value > 1) return;
  84. m_HuSetValue = value;
  85. ActiualPosition = (int)(value * ActiualMaxPosition);
  86. Invalidate();
  87. }
  88. }
  89. //=============================================================
  90. private int m_ColorChangeStyle = 1;
  91. /// <summary>
  92. /// 进度条变化的颜色
  93. /// </summary>
  94. [Description("指定进度条颜色的变化值")]
  95. [DefaultValue(1)]
  96. public int ColorChangeStyle
  97. {
  98. get { return m_ColorChangeStyle; }
  99. set
  100. {
  101. if (value != 1 && value != 2) return;
  102. m_ColorChangeStyle = value;
  103. Invalidate();
  104. }
  105. }
  106. #endregion
  107. /// <summary>
  108. /// 实际画图的点位
  109. /// </summary>
  110. private int ActiualPosition { get; set; } = 0;
  111. private int ActiualMaxPosition { get; set; } = 0;
  112. private Thread ThreadCalculation;
  113. private bool IsQuit { get; set; } = false;
  114. private void HuProcessBar_Load(object sender, EventArgs e)
  115. {
  116. ActiualMaxPosition = Width;
  117. Disposed += HuProcessBar_Disposed;
  118. SizeChanged += HuProcessBar_SizeChanged;
  119. // 开启双缓冲
  120. //SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
  121. // Enable the OnNotifyMessage event so we get a chance to filter out
  122. // Windows messages before they get to the form's WndProc
  123. //SetStyle(ControlStyles.EnableNotifyMessage, true);
  124. }
  125. private void HuProcessBar_SizeChanged(object sender, EventArgs e)
  126. {
  127. ActiualMaxPosition = Width;
  128. Invalidate();
  129. }
  130. private void HuProcessBar_Disposed(object sender, EventArgs e)
  131. {
  132. IsQuit = true;
  133. }
  134. private delegate void MethodVoidDelegate();
  135. private void HuProcessBar_Paint(object sender, PaintEventArgs e)
  136. {
  137. Brush MyBrush = new SolidBrush(BackColor);
  138. Rectangle Rec = new Rectangle(0, 0, Width - 1, Height - 1);
  139. e.Graphics.FillRectangle(MyBrush, Rec);
  140. if (ActiualPosition > 0 && ActiualMaxPosition > 0)
  141. {
  142. Rectangle Rec1 = new Rectangle(0, 0, ActiualPosition, Height - 1);
  143. if (ColorChangeStyle == 1)
  144. {
  145. byte Color_R = FillColorStart.R;
  146. byte Color_G = FillColorStart.G;
  147. byte Color_B = FillColorStart.B;
  148. Color_R += (byte)((FillColorEnd.R - FillColorStart.R) * ActiualPosition / ActiualMaxPosition);
  149. Color_G += (byte)((FillColorEnd.G - FillColorStart.G) * ActiualPosition / ActiualMaxPosition);
  150. Color_B += (byte)((FillColorEnd.B - FillColorStart.B) * ActiualPosition / ActiualMaxPosition);
  151. Color MyColor = Color.FromArgb(Color_R, Color_G, Color_B);
  152. Brush HuBrush = new SolidBrush(MyColor);
  153. e.Graphics.FillRectangle(HuBrush, Rec1);
  154. HuBrush.Dispose();
  155. }
  156. else
  157. {
  158. LinearGradientBrush linear = new LinearGradientBrush(Rec, FillColorStart, FillColorEnd, 0f);
  159. e.Graphics.FillRectangle(linear, Rec1);
  160. linear.Dispose();
  161. }
  162. }
  163. if (HasBorder)
  164. {
  165. using (Pen pen1 = new Pen(HuBorderColor))
  166. {
  167. e.Graphics.DrawRectangle(pen1, Rec);
  168. }
  169. }
  170. if (Subsections > 1)
  171. {
  172. for (int i = 1; i < Subsections; i++)
  173. {
  174. int x = i * ActiualMaxPosition / Subsections;
  175. e.Graphics.DrawLine(Pens.White, x, 0, x, Height - 1);
  176. //e.Graphics.DrawLine(Pens.White, x + 1, 0, x + 1, Height - 1);
  177. }
  178. }
  179. MyBrush.Dispose();
  180. }
  181. }
  182. }