VerticalProgressBar.cs 1.3 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


public class VerticalProgressBar : ProgressBar
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            //cp.Style |= 0x04;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }
    //===变为垂直显示方法1:
    public VerticalProgressBar()
    {
        base.SetStyle(ControlStyles.UserPaint, true);
    }
    //重写OnPaint方法
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Rectangle rect = new Rectangle(base.ClientRectangle.Location, new Size(base.Width, base.Height));
        ProgressBarRenderer.DrawVerticalBar(e.Graphics, rect);
        //纯色填充
        Brush lb = new SolidBrush(base.ForeColor);
        var len = base.Height * base.Value / base.Maximum;
        e.Graphics.FillRectangle(lb, new Rectangle(0, base.Height - len, base.Width, len));

        Font font = new Font(FontFamily.GenericSansSerif, 10);
        SolidBrush black = new SolidBrush(Color.Black);
        e.Graphics.DrawString($"{Value}/{Maximum}", font, black, 0, 0);
    }
}