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();
            }
    }

     效果如下:

  • 相关阅读:
    Java实现 蓝桥杯 历届试题 翻硬币
    后台管理UI推荐
    js跳转页面方法(转)
    Request常用方法 (总结)
    Eclipse项目 迁移到 Intellj IDEA
    由后端来类比前端设计的思考(转)
    数据库字段命名及设计规范(转)
    如何改变Myeclipse编辑区背景色(转)
    Myeclipse和windows调节成护眼色
    qt截获html请求(继承QNetworkAccessManager和QNetworkReply)
  • 原文地址:https://www.cnblogs.com/hejoy91/p/14158923.html
Copyright © 2011-2022 走看看