UIProgress.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using Microsoft.Drawing;
  5. using Microsoft.Windows.Forms.Animate;
  6. namespace Microsoft.Windows.Forms
  7. {
  8. /// <summary>
  9. /// 虚拟进度条控件
  10. /// </summary>
  11. public class UIProgress : UIControl
  12. {
  13. private const int DEFAULT_FRAME_INTERVAL = 10; //定时器间隔(毫秒)
  14. private Timer m_FrameTimer = new Timer(); //动画定时器
  15. private UILinearAnimation m_Animation = new UILinearAnimation(); //动画对象
  16. private Color m_ProgressColor = DefaultTheme.LightTransparent;
  17. /// <summary>
  18. /// 获取或设置进度条颜色
  19. /// </summary>
  20. public virtual Color ProgressColor
  21. {
  22. get
  23. {
  24. return this.m_ProgressColor;
  25. }
  26. set
  27. {
  28. if (value != this.m_ProgressColor)
  29. {
  30. this.m_ProgressColor = value;
  31. this.Invalidate();
  32. }
  33. }
  34. }
  35. private Color m_BorderColor = DefaultTheme.LightLightTransparent;
  36. /// <summary>
  37. /// 获取或设置进度条边框颜色
  38. /// </summary>
  39. public virtual Color BorderColor
  40. {
  41. get
  42. {
  43. return this.m_BorderColor;
  44. }
  45. set
  46. {
  47. if (value != this.m_BorderColor)
  48. {
  49. this.m_BorderColor = value;
  50. this.Invalidate();
  51. }
  52. }
  53. }
  54. private int m_Percentage = 0;
  55. /// <summary>
  56. /// 获取或设置进度条颜色
  57. /// </summary>
  58. public virtual int Percentage
  59. {
  60. get
  61. {
  62. return this.m_Percentage;
  63. }
  64. set
  65. {
  66. if (value != this.m_Percentage)
  67. {
  68. this.m_Percentage = value;
  69. this.m_FrameTimer.Start();
  70. this.m_Animation.Continue(this.m_Percentage);
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// 构造函数
  76. /// </summary>
  77. public UIProgress()
  78. {
  79. this.BackColor = DefaultTheme.DarkDarkTransparent;
  80. this.m_FrameTimer.Interval = DEFAULT_FRAME_INTERVAL;
  81. this.m_FrameTimer.Tick += (sender, e) =>
  82. {
  83. this.Invalidate();
  84. if (this.m_Animation.Stopped)
  85. this.m_FrameTimer.Stop();
  86. };
  87. }
  88. /// <summary>
  89. /// 渲染控件
  90. /// </summary>
  91. /// <param name="e">数据</param>
  92. protected override void RenderSelf(PaintEventArgs e)
  93. {
  94. //准备
  95. Graphics g = e.Graphics;
  96. Rectangle rect = RectangleEx.Subtract(this.ClientRectangle, this.Padding);
  97. //已完成进度
  98. int maxWidth = rect.Width - 2;
  99. int width = (int)(maxWidth * this.m_Animation.Current / 100d);
  100. Rectangle rcProgress = new Rectangle(rect.Left + 1, rect.Top + 1, Math.Min(width, maxWidth), rect.Height - 2);
  101. //未完成进度
  102. Rectangle rcBlank = new Rectangle(rcProgress.Right, rcProgress.Top, rect.Width - 2 - rcProgress.Width, rcProgress.Height);
  103. //渲染已完成进度
  104. this.Sprite.BorderVisibleStyle = BorderVisibleStyle.None;
  105. this.Sprite.BackColor = this.ProgressColor;
  106. this.Sprite.State = this.State;
  107. this.Sprite.BeginRender(g);
  108. this.Sprite.RenderBackColor(rcProgress);
  109. this.Sprite.EndRender();
  110. //渲染未完成进度
  111. this.Sprite.BackColor = this.BackColor;
  112. this.Sprite.BeginRender(g);
  113. this.Sprite.RenderBackColor(rcBlank);
  114. this.Sprite.EndRender();
  115. //渲染边框
  116. this.Sprite.BorderVisibleStyle = BorderVisibleStyle.All;
  117. this.Sprite.BorderColor = this.BorderColor;
  118. this.Sprite.BeginRender(g);
  119. this.Sprite.RenderBorder(rect);
  120. this.Sprite.EndRender();
  121. }
  122. /// <summary>
  123. /// 释放资源
  124. /// </summary>
  125. /// <param name="disposing">释放托管资源为true,否则为false</param>
  126. protected override void Dispose(bool disposing)
  127. {
  128. if (this.m_FrameTimer != null)
  129. {
  130. this.m_FrameTimer.Dispose();
  131. this.m_FrameTimer = null;
  132. }
  133. if (this.m_Animation != null)
  134. {
  135. this.m_Animation.Dispose();
  136. this.m_Animation = null;
  137. }
  138. base.Dispose(disposing);
  139. }
  140. }
  141. }