using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { internal class MyProcessBar : ProgressBar { private Color _foreColor = Color.Blue; private Color _backColor = Color.White; public MyProcessBar() { SetStyle(ControlStyles.UserPaint, true); } [System.ComponentModel.Category("Custom")] public Color ForeColor { get { return _foreColor; } set { _foreColor = value; Invalidate(); } } [System.ComponentModel.Category("Custom")] public Color BackColor { get { return _backColor; } set { _backColor = value; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Rectangle rect = ClientRectangle; rect.Width = (int)((double)rect.Width * (Value / (double)Maximum)); using (Brush backBrush = new SolidBrush(BackColor)) { e.Graphics.FillRectangle(backBrush, rect); } using (Brush foreBrush = new SolidBrush(ForeColor)) { rect.Width -= 1; // 为了避免绘制时超出边界,稍微减少宽度 e.Graphics.FillRectangle(foreBrush, new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 1, rect.Height - 1)); } } } }