UserVerticalProgress.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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.Drawing.Drawing2D;
  10. namespace HslCommunication.Controls
  11. {
  12. /// <summary>
  13. /// 一个直立的进度条控件,满足不同的情况使用
  14. /// </summary>
  15. public partial class UserVerticalProgress : UserControl
  16. {
  17. /// <summary>
  18. /// 实例化一个对象
  19. /// </summary>
  20. public UserVerticalProgress()
  21. {
  22. InitializeComponent();
  23. m_borderPen = new Pen(Color.DimGray);
  24. m_backBrush = new SolidBrush(Color.Transparent);
  25. m_foreBrush = new SolidBrush(Color.Tomato);
  26. m_progressColor = Color.Tomato;
  27. m_borderColor = Color.DimGray;
  28. m_formatCenter = new StringFormat();
  29. m_formatCenter.Alignment = StringAlignment.Center;
  30. m_formatCenter.LineAlignment = StringAlignment.Center;
  31. m_UpdateAction = new Action(UpdateRender);
  32. hybirdLock = new Core.SimpleHybirdLock();
  33. DoubleBuffered = true;
  34. }
  35. private void UserVerticalProgress_Load(object sender, EventArgs e)
  36. {
  37. }
  38. private void UserVerticalProgress_Paint(object sender, PaintEventArgs e)
  39. {
  40. try
  41. {
  42. // 根据实际值来绘制图形
  43. Graphics g = e.Graphics;
  44. Rectangle rectangle = new Rectangle(0, 0, Width - 1, Height - 1);
  45. g.FillRectangle(m_backBrush, rectangle);
  46. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  47. switch(m_progressStyle)
  48. {
  49. case ProgressStyle.Vertical:
  50. {
  51. // 垂直方向的进度条
  52. int height = (int)(m_actual * 1L * (Height - 2) / m_Max);
  53. rectangle = new Rectangle(0, Height - 1 - height, Width - 1, height);
  54. g.FillRectangle(m_foreBrush, rectangle);
  55. //if (m_actual != m_value)
  56. //{
  57. // rectangle = new Rectangle(0, rectangle.Y - 5, Width - 1, 5);
  58. // using (Brush b = new LinearGradientBrush(rectangle, m_progressColor, BackColor, 270f))
  59. // {
  60. // g.FillRectangle(b, rectangle);
  61. // }
  62. //}
  63. break;
  64. }
  65. case ProgressStyle.Horizontal:
  66. {
  67. // 水平方向的进度条
  68. int width = (int)(m_actual * 1L * (Width - 2) / m_Max);
  69. rectangle = new Rectangle(0, 0, width + 1, Height - 1);
  70. g.FillRectangle(m_foreBrush, rectangle);
  71. //if (m_actual == m_value)
  72. //{
  73. // rectangle = new Rectangle(width + 1, 0, 5, Height - 1);
  74. // using (Brush b = new LinearGradientBrush(rectangle, m_progressColor, BackColor, 0f))
  75. // {
  76. // g.FillRectangle(b, rectangle);
  77. // }
  78. //}
  79. break;
  80. }
  81. }
  82. rectangle = new Rectangle(0, 0, Width - 1, Height - 1);
  83. if (m_isTextRender)
  84. {
  85. string str = (m_actual * 100L / m_Max) + "%";
  86. using (Brush brush = new SolidBrush(ForeColor))
  87. {
  88. g.DrawString(str, Font, brush, rectangle, m_formatCenter);
  89. }
  90. }
  91. g.DrawRectangle(m_borderPen, rectangle);
  92. }
  93. catch (Exception ex)
  94. {
  95. // BasicFramework.SoftBasic.ShowExceptionMessage(ex);
  96. }
  97. }
  98. private void UserVerticalProgress_SizeChanged(object sender, EventArgs e)
  99. {
  100. Invalidate();
  101. }
  102. #region Private Members
  103. private int m_version = 0; // 设置数据时的版本,用于更新时的版本验证
  104. private int m_Max = 100; // 默认的最大值
  105. private int m_value = 0; // 当前的设定值
  106. private int m_actual = 0; // 当前的实际值
  107. private Pen m_borderPen; // 边框的画笔
  108. private Color m_borderColor; // 边框的背景色
  109. private Brush m_backBrush; // 背景色
  110. private Brush m_foreBrush; // 前景色
  111. private Color m_progressColor; // 前景色颜色
  112. private StringFormat m_formatCenter; // 中间显示字符串的格式
  113. private bool m_isTextRender = true; // 是否显示文本信息
  114. private Action m_UpdateAction; // 更新界面的委托
  115. private Core.SimpleHybirdLock hybirdLock; // 数据的同步锁
  116. private int m_speed = 1; // 进度条的升降快慢
  117. private ProgressStyle m_progressStyle = ProgressStyle.Vertical; // 进度条的样式,指示水平的还是竖直的
  118. private bool m_UseAnimation = false; // 是否采用动画效果
  119. #endregion
  120. #region Public Properties
  121. /// <summary>
  122. /// 获取或设置光标在控件上显示的信息
  123. /// </summary>
  124. public override Cursor Cursor
  125. {
  126. get
  127. {
  128. return base.Cursor;
  129. }
  130. set
  131. {
  132. base.Cursor = value;
  133. }
  134. }
  135. /// <summary>
  136. /// 获取或设置控件的背景颜色值
  137. /// </summary>
  138. [Description("获取或设置进度条的背景色")]
  139. [Category("外观")]
  140. [Browsable(true)]
  141. public override Color BackColor
  142. {
  143. get
  144. {
  145. return base.BackColor;
  146. }
  147. set
  148. {
  149. base.BackColor = value;
  150. m_backBrush?.Dispose();
  151. m_backBrush = new SolidBrush(value);
  152. Invalidate();
  153. }
  154. }
  155. /// <summary>
  156. /// 获取或设置进度的颜色
  157. /// </summary>
  158. [Description("获取或设置进度条的前景色")]
  159. [Category("外观")]
  160. [Browsable(true)]
  161. [DefaultValue(typeof(Color),"Tomato")]
  162. public Color ProgressColor
  163. {
  164. get { return m_progressColor; }
  165. set
  166. {
  167. m_progressColor = value;
  168. m_foreBrush?.Dispose();
  169. m_foreBrush = new SolidBrush(value);
  170. Invalidate();
  171. }
  172. }
  173. /// <summary>
  174. /// 进度条的最大值,默认为100
  175. /// </summary>
  176. [Description("获取或设置进度条的最大值,默认为100")]
  177. [Category("外观")]
  178. [Browsable(true)]
  179. [DefaultValue(100)]
  180. public int Max
  181. {
  182. get { return m_Max; }
  183. set
  184. {
  185. if (value > 1)
  186. {
  187. m_Max = value;
  188. }
  189. if (m_value > m_Max) m_value = m_Max;
  190. Invalidate();
  191. }
  192. }
  193. /// <summary>
  194. /// 当前进度条的值,不能大于最大值或小于0
  195. /// </summary>
  196. [Description("获取或设置当前进度条的值")]
  197. [Category("外观")]
  198. [Browsable(true)]
  199. [DefaultValue(0)]
  200. public int Value
  201. {
  202. get { return m_value; }
  203. set
  204. {
  205. if (value >= 0 && value <= m_Max)
  206. {
  207. if (value != m_value)
  208. {
  209. m_value = value;
  210. if (UseAnimation)
  211. {
  212. int version = System.Threading.Interlocked.Increment(ref m_version);
  213. System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(
  214. ThreadPoolUpdateProgress), version);
  215. }
  216. else
  217. {
  218. m_actual = value;
  219. Invalidate();
  220. }
  221. }
  222. }
  223. }
  224. }
  225. /// <summary>
  226. /// 是否显示进度
  227. /// </summary>
  228. [Description("获取或设置是否显示进度文本")]
  229. [Category("外观")]
  230. [Browsable(true)]
  231. [DefaultValue(true)]
  232. public bool IsTextRender
  233. {
  234. get { return m_isTextRender; }
  235. set
  236. {
  237. m_isTextRender = value;
  238. Invalidate();
  239. }
  240. }
  241. /// <summary>
  242. /// 设置进度条的边框颜色
  243. /// </summary>
  244. [Description("获取或设置进度条的边框颜色")]
  245. [Category("外观")]
  246. [Browsable(true)]
  247. [DefaultValue(typeof(Color),"DimGray")]
  248. public Color BorderColor
  249. {
  250. get { return m_borderColor; }
  251. set
  252. {
  253. m_borderColor = value;
  254. m_borderPen?.Dispose();
  255. m_borderPen = new Pen(value);
  256. Invalidate();
  257. }
  258. }
  259. /// <summary>
  260. /// 设置进度变更的速度
  261. /// </summary>
  262. [Description("获取或设置进度条的变化进度")]
  263. [Category("外观")]
  264. [Browsable(true)]
  265. [DefaultValue(1)]
  266. public int ValueChangeSpeed
  267. {
  268. get{return m_speed;}
  269. set
  270. {
  271. if (value >= 1)
  272. {
  273. m_speed = value;
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// 获取或设置进度条变化的时候是否采用动画效果
  279. /// </summary>
  280. [Description("获取或设置进度条变化的时候是否采用动画效果")]
  281. [Category("外观")]
  282. [Browsable(true)]
  283. [DefaultValue(false)]
  284. public bool UseAnimation
  285. {
  286. get { return m_UseAnimation; }
  287. set { m_UseAnimation = value; }
  288. }
  289. /// <summary>
  290. /// 进度条的样式
  291. /// </summary>
  292. [Description("获取或设置进度条的样式")]
  293. [Category("外观")]
  294. [Browsable(true)]
  295. [DefaultValue(typeof(ProgressStyle),"Vertical")]
  296. public ProgressStyle ProgressStyle
  297. {
  298. get { return m_progressStyle; }
  299. set { m_progressStyle = value;Invalidate(); }
  300. }
  301. #endregion
  302. #region Update Progress
  303. private void ThreadPoolUpdateProgress(object obj)
  304. {
  305. try
  306. {
  307. // 开始计算更新细节
  308. int version = (int)obj;
  309. if (m_speed < 1) m_speed = 1;
  310. while (m_actual != m_value)
  311. {
  312. System.Threading.Thread.Sleep(17);
  313. if (version != m_version) break;
  314. hybirdLock.Enter();
  315. int newActual = 0;
  316. if (m_actual > m_value)
  317. {
  318. int offect = m_actual - m_value;
  319. if (offect > m_speed) offect = m_speed;
  320. newActual = m_actual - offect;
  321. }
  322. else
  323. {
  324. int offect = m_value - m_actual;
  325. if (offect > m_speed) offect = m_speed;
  326. newActual = m_actual + offect;
  327. }
  328. m_actual = newActual;
  329. hybirdLock.Leave();
  330. if (version == m_version)
  331. {
  332. if (IsHandleCreated) Invoke(m_UpdateAction);
  333. }
  334. else
  335. {
  336. break;
  337. }
  338. }
  339. }
  340. catch(Exception ex)
  341. {
  342. // BasicFramework.SoftBasic.ShowExceptionMessage(ex);
  343. }
  344. }
  345. private void UpdateRender()
  346. {
  347. Invalidate();
  348. }
  349. #endregion
  350. }
  351. /// <summary>
  352. /// 进度条的样式
  353. /// </summary>
  354. public enum ProgressStyle
  355. {
  356. /// <summary>
  357. /// 竖直的,纵向的进度条
  358. /// </summary>
  359. Vertical,
  360. /// <summary>
  361. /// 水平进度条
  362. /// </summary>
  363. Horizontal
  364. }
  365. }