#if NETFX || NETCORE using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace Apewer.Surface { /// 块状进度条。 public partial class BlockProgress : BaseControl { private Color _normalborder = FormsUtility.GraceBorder; private Color _normalblock = FormsUtility.GraceBorder; private Color _hoverborder = FormsUtility.GraceSilver; private Color _hoverblock = FormsUtility.GraceBorder; private int _max = 1; private int _value = 0; private bool _hover = false; /// public BlockProgress() { this.Size = new Size(200, 40); DrawProgress(); this.Resize += Event_This_Resize; this.MouseMove += Event_This_MouseMove; this.MouseLeave += Event_This_MouseLeave; } private void Event_This_MouseLeave(object sender, EventArgs e) { Hover = false; } private void Event_This_MouseMove(object sender, MouseEventArgs e) { Hover = true; } private void Event_This_Resize(object sender, EventArgs e) { DrawProgress(); } private bool Hover { get { return _hover; } set { if (_hover != value) { _hover = value; DrawProgress(); } } } /// 当前进度值。 public int Value { get { return _value; } set { _value = (value < 0) ? 0 : ((value > _max) ? _max : value); DrawProgress(); } } /// 最大进度值。 public int Max { get { return _max; } set { _max = (value > 0) ? value : 1; if (_value >= _max) _value = value; DrawProgress(); } } /// 增加当前进度值,不会超过最大进度值。 public void Plus() { Plus(1); } /// 增加当前进度值,不会超过最大进度值。 /// 进度步长值。 public void Plus(int step) { Value = Value + 1; } private void DrawProgress() { if (this.IsHandleCreated) { this.BeginInvoke(new Invoker(delegate () { DrawImage(); })); } else { DrawImage(); } } private void DrawImage() { if (this.Width < 1) return; if (this.Height < 1) return; var vbitmap = new Bitmap(this.Width, this.Height); using (var vgraphic = Graphics.FromImage(vbitmap)) { vgraphic.SmoothingMode = SmoothingMode.HighSpeed; vgraphic.CompositingMode = CompositingMode.SourceCopy; if ((this.Width >= 3) && (this.Height >= 3)) { vgraphic.Clear(Color.White); using (var vpen = new Pen(Hover ? _hoverborder : _normalborder)) { vgraphic.DrawRectangle(vpen, 0, 0, this.Width - 1, this.Height - 1); } if ((this.Width >= 5) && (this.Height >= 5)) { int vwidth; if (Value == 0) vwidth = 0; else { if (Value == Max) vwidth = this.Width - 4; else vwidth = (int)(((float)Value / (float)Max) * (float)(this.Width - 4)); } using (var vbrush = new SolidBrush(Hover ? _hoverblock : _normalblock)) { vgraphic.FillRectangle(vbrush, 2, 2, vwidth, this.Height - 4); } } } else { vgraphic.Clear(Hover ? _hoverblock : _normalblock); } } ProgressImage = vbitmap; } private Image ProgressImage { set { if (this.BackgroundImage != null) this.BackgroundImage.Dispose(); this.BackgroundImage = value; } } } } #endif