ProgressBar颜色设定及绘制文字显示

来源:互联网 发布:慧思达软件多少钱 编辑:程序博客网 时间:2024/06/05 08:27
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            var progressBar = new MyProgressBar()
            {
                Location = new Point(20, 20),
                Size = new Size(100, 14),
                Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
                ForeColor = Color.Blue
            };
            var progressBar2 = new MyProgressBar()
            {
                Location = new Point(20, 60),
                Size = new Size(100, 14),
                Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
                ForeColor = Color.Red
            };
        this.Controls.Add(progressBar);
        this.Controls.Add(progressBar2);
 
        var timer = new Timer {Interval = 100};
        timer.Tick += (s, e) => { progressBar.Value = progressBar.Value % 100 + 2; progressBar2.Value = progressBar2.Value % 100 + 2; };
        timer.Start();
        }
    }


    public class MyProgressBar : ProgressBar
    {
        public MyProgressBar()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            SolidBrush brush = null;


            Rectangle bounds = new Rectangle(0, 0, base.Width, base.Height);
            //...
            //e.Graphics.FillRectangle(new SolidBrush(Color.Black), 1, 1, bounds.Width - 2, bounds.Height - 2);


            ProgressBarRenderer.DrawHorizontalBar(e.Graphics, bounds);


            bounds.Height -= 4;
            bounds.Width = ((int)(bounds.Width * (((double)base.Value) / ((double)base.Maximum)))) - 4;
            brush = new SolidBrush(this.ForeColor);
            e.Graphics.FillRectangle(brush, 2, 2, bounds.Width, bounds.Height);








            Rectangle rect = ClientRectangle;
            Graphics g = e.Graphics;


            //ProgressBarRenderer.DrawHorizontalBar(g, rect);
            //rect.Inflate(-3, -3);
            //if (Value > 0)
            //{
            //    var clip = new Rectangle(rect.X, rect.Y, (int)((float)Value / Maximum * rect.Width), rect.Height);
            //    ProgressBarRenderer.DrawHorizontalChunks(g, clip);
            //}


            string text = Value + "%";
            using (var font = new Font(FontFamily.GenericSerif, 10))
            {
                SizeF sz = g.MeasureString(text, font);
                var location = new PointF(rect.Width / 2 - sz.Width / 2, rect.Height / 2 - sz.Height / 2 + 2);
                g.DrawString(text, font, Brushes.Red, location);
            }
        }
    }
}
0 0
原创粉丝点击