https://blog.csdn.net/qqqqqqqq188/article/details/43528959?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf

 

 

public class DataGridViewProgressBarXwithTextColumn:DataGridViewProgressBarXColumn
    {
        public DataGridViewProgressBarXwithTextColumn():base()
        {
            this.CellTemplate = new DataGridViewProgressBarXwithTextCell();
        }
    }


public class DataGridViewProgressBarXwithTextCell : DataGridViewProgressBarXCell
    {

        public DataGridViewProgressBarXwithTextCell()
        {

        }

        //设置进度条的背景色;
        public DataGridViewProgressBarXwithTextCell(Color progressBarColor)
            : base()
        {
            ProgressBarColor = progressBarColor;
        }

        protected Color ProgressBarColor = Color.CornflowerBlue; //进度条的默认背景颜色

        protected override void Paint(Graphics graphics, Rectangle clipBounds,
            Rectangle cellBounds, int rowIndex,
            DataGridViewElementStates cellState,
            object value, object formattedValue,
            string errorText, DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            using (SolidBrush backBrush = new SolidBrush(cellStyle.BackColor))
            {
                graphics.FillRectangle(backBrush, cellBounds);
            }
            base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
            

            using (SolidBrush brush = new SolidBrush(ProgressBarColor))
            {
                if (value != null)
                {
                    if (!string.IsNullOrEmpty(value.ToString()))
                    {
                        int num = Convert.ToInt32(value);
                        float percent = num / 100F;

                        graphics.FillRectangle(brush, cellBounds.X, cellBounds.Y + 1, cellBounds.Width * percent, cellBounds.Height - 3);

                        string text = string.Format("{0}%", num);
                        SizeF rf = graphics.MeasureString(text, cellStyle.Font);
                        float x = cellBounds.X + (cellBounds.Width - rf.Width) / 2f;
                        float y = cellBounds.Y + (cellBounds.Height - rf.Height) / 2f;
                        graphics.DrawString(text, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), x, y);
                    }
                }
            }
        }
    }

 

posted on 2020-10-21 15:00  Manuel  阅读(682)  评论(0编辑  收藏  举报