C# 使用GDI制作垂直进度条(由下往上)

使用GDI+绘进度条的方式多种多样,可以封装一个UserControl,也可以直接使用一个控件来绘制(Label、Image、Panel等),甚至可以直接在winForm上画一个,关键代码没几行(这里是一个量杯的进度条为例):

          Graphics g = e.Graphics;
                SolidBrush brush = null;
                string strName = "";
                //由下至上的进度条需要开始时设置一个100%进度的背景色+一个白色层,使用时白色层根据进度减小高度
                brush = new SolidBrush(Color.White);
                //获取进度:已经倒入的重量/进度条高度*100
                Info.Progress = Info.ActualWeight / e.ClipRectangle.Height * 100;
                //画前色(这里的进度条高度是不固定的=总重量,所以直接高度-倒入的重量就可以了,如果是固定的高度,计算一下需要减少多少就行)
                e.Graphics.FillRectangle(brush, 0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height - (int)Info.ActualWeight);
                strName = Info.Name + ":" + Info.Progress.ToString("0.00") + "%";
                //得到写入的字符串像素宽度--居中用
                SizeF sf = g.MeasureString(strName, this.Font);
                //显示进度
                g.DrawString(strName, this.Font, new SolidBrush(ForeColor), e.ClipRectangle.Width / 2 - sf.Width / 2, e.ClipRectangle.Height / 2 - this.Font.Height / 2);
                

注意:进度条更新时需要调用   *.Invalidate();

原文地址:https://www.cnblogs.com/boyxzhen/p/8249933.html