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.

163 lines
4.6 KiB

  1. #if NETFX || NETCORE
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace Apewer.Surface
  7. {
  8. /// <summary>块状进度条。</summary>
  9. public partial class BlockProgress : BaseControl
  10. {
  11. private Color _normalborder = FormsUtility.GraceBorder;
  12. private Color _normalblock = FormsUtility.GraceBorder;
  13. private Color _hoverborder = FormsUtility.GraceSilver;
  14. private Color _hoverblock = FormsUtility.GraceBorder;
  15. private int _max = 1;
  16. private int _value = 0;
  17. private bool _hover = false;
  18. /// <summary></summary>
  19. public BlockProgress()
  20. {
  21. this.Size = new Size(200, 40);
  22. DrawProgress();
  23. this.Resize += Event_This_Resize;
  24. this.MouseMove += Event_This_MouseMove;
  25. this.MouseLeave += Event_This_MouseLeave;
  26. }
  27. private void Event_This_MouseLeave(object sender, EventArgs e)
  28. {
  29. Hover = false;
  30. }
  31. private void Event_This_MouseMove(object sender, MouseEventArgs e)
  32. {
  33. Hover = true;
  34. }
  35. private void Event_This_Resize(object sender, EventArgs e)
  36. {
  37. DrawProgress();
  38. }
  39. private bool Hover
  40. {
  41. get { return _hover; }
  42. set
  43. {
  44. if (_hover != value)
  45. {
  46. _hover = value;
  47. DrawProgress();
  48. }
  49. }
  50. }
  51. /// <summary>当前进度值。</summary>
  52. public int Value
  53. {
  54. get { return _value; }
  55. set
  56. {
  57. _value = (value < 0) ? 0 : ((value > _max) ? _max : value);
  58. DrawProgress();
  59. }
  60. }
  61. /// <summary>最大进度值。</summary>
  62. public int Max
  63. {
  64. get { return _max; }
  65. set
  66. {
  67. _max = (value > 0) ? value : 1;
  68. if (_value >= _max) _value = value;
  69. DrawProgress();
  70. }
  71. }
  72. /// <summary>增加当前进度值,不会超过最大进度值。</summary>
  73. public void Plus()
  74. {
  75. Plus(1);
  76. }
  77. /// <summary>增加当前进度值,不会超过最大进度值。</summary>
  78. /// <param name="step">进度步长值。</param>
  79. public void Plus(int step)
  80. {
  81. Value = Value + 1;
  82. }
  83. private void DrawProgress()
  84. {
  85. if (this.IsHandleCreated)
  86. {
  87. this.BeginInvoke(new Invoker(delegate () { DrawImage(); }));
  88. }
  89. else
  90. {
  91. DrawImage();
  92. }
  93. }
  94. private void DrawImage()
  95. {
  96. if (this.Width < 1) return;
  97. if (this.Height < 1) return;
  98. var vbitmap = new Bitmap(this.Width, this.Height);
  99. using (var vgraphic = Graphics.FromImage(vbitmap))
  100. {
  101. vgraphic.SmoothingMode = SmoothingMode.HighSpeed;
  102. vgraphic.CompositingMode = CompositingMode.SourceCopy;
  103. if ((this.Width >= 3) && (this.Height >= 3))
  104. {
  105. vgraphic.Clear(Color.White);
  106. using (var vpen = new Pen(Hover ? _hoverborder : _normalborder))
  107. {
  108. vgraphic.DrawRectangle(vpen, 0, 0, this.Width - 1, this.Height - 1);
  109. }
  110. if ((this.Width >= 5) && (this.Height >= 5))
  111. {
  112. int vwidth;
  113. if (Value == 0) vwidth = 0;
  114. else
  115. {
  116. if (Value == Max) vwidth = this.Width - 4;
  117. else vwidth = (int)(((float)Value / (float)Max) * (float)(this.Width - 4));
  118. }
  119. using (var vbrush = new SolidBrush(Hover ? _hoverblock : _normalblock))
  120. {
  121. vgraphic.FillRectangle(vbrush, 2, 2, vwidth, this.Height - 4);
  122. }
  123. }
  124. }
  125. else
  126. {
  127. vgraphic.Clear(Hover ? _hoverblock : _normalblock);
  128. }
  129. }
  130. ProgressImage = vbitmap;
  131. }
  132. private Image ProgressImage
  133. {
  134. set
  135. {
  136. if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
  137. this.BackgroundImage = value;
  138. }
  139. }
  140. }
  141. }
  142. #endif