zoukankan      html  css  js  c++  java
  • C#自定义进度条,带百分比显示

    C#自带的进度条控件没有显示百分比,为此继承了ProgressBar,重写一个带进度百分比的控件;没有加载进度时可以默认显示文字描述,有进度时改为显示百分比。

    public partial class ProgressBarEx : ProgressBar
        {
            Font fontDefault = new Font("微软雅黑", 12, FontStyle.Regular, GraphicsUnit.Pixel);
            Brush b = new SolidBrush(Color.Black);
            private bool processing = false;
            private string defaultTips = "";
    
            public ProgressBarEx()
            {
                this.SetStyle(ControlStyles.UserPaint, true);
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            }
            /// <summary>
            /// 是否正在加载进度
            /// </summary>
            public bool Processing
            {
                get { return processing; }
                set
                {
                    processing = value;
                    if (processing)
                        ForeColor = Color.MediumSeaGreen;
                    else
                        ForeColor = Color.LightGray;
                }
            }
            /// <summary>
            /// 默认显示文字
            /// </summary>
            public string DefaultTips
            {
                get { return defaultTips; }
                set
                {
                    defaultTips = value;
                }
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                SolidBrush brush = null;
                Rectangle bounds = new Rectangle(0, 0, base.Width, base.Height);
                Bitmap bmp = new Bitmap(bounds.Width,bounds.Height);
                Graphics gbmp = Graphics.FromImage(bmp);
                gbmp.FillRectangle(new SolidBrush(this.BackColor), 1, 1, bounds.Width - 2, bounds.Height - 2);
                bounds.Height -= 4;
                bounds.Width = ((int)(bounds.Width * (((double)base.Value) / ((double)base.Maximum)))) - 4;
                brush = new SolidBrush(this.ForeColor);
                gbmp.FillRectangle(brush, 2, 2, bounds.Width, bounds.Height);
                brush.Dispose();
                string num = "";
                if (processing)
                    num = this.Value.ToString() + "%";
                else
                    num = defaultTips;
                var fsize = gbmp.MeasureString(num,fontDefault);
                gbmp.DrawString(num, fontDefault, b, new PointF(Width / 2 - fsize.Width / 2, Height / 2 - fsize.Height / 2));
                e.Graphics.DrawImage(bmp, 0, 0);
                gbmp.Dispose();
                bmp.Dispose();
            }
    }

     效果如下:

  • 相关阅读:
    1836Alignment
    JS日期格式化
    excle自编公式方法
    excle的公式说明
    小技巧之一 string[]合并
    Nunit的使用小问题
    Ajax中上传文件的方式
    VSS也有BUG?
    SQL Server中将时间型的转为yyyyMMddhhmmss
    给已经存在的PDF文件加水印
  • 原文地址:https://www.cnblogs.com/hejoy91/p/14158923.html
Copyright © 2011-2022 走看看