借鉴了冰封一夏的控件重绘与Albin的DevExpress GridControl 单元格添加进度条(ProgressBar)完成,特别感谢
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { if (e.Column.FieldName == "name") { DrawProgressBar(e); e.Handled = true; DrawEditor(e); } }
//解决单元格只显示数值,而不显示进度条,(当点击单元格时数值会消失),那么需要我们再来手动的编写一段代码用来处理当单元格触发时一些操作.
private void DrawEditor(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { GridCellInfo cell = e.Cell as GridCellInfo; Point offset = cell.CellValueRect.Location; BaseEditPainter pb = cell.ViewInfo.Painter as BaseEditPainter; AppearanceObject style = cell.ViewInfo.PaintAppearance; if (!offset.IsEmpty) cell.ViewInfo.Offset(offset.X, offset.Y); try { pb.Draw(new ControlGraphicsInfoArgs(cell.ViewInfo, e.Cache, cell.Bounds)); } finally { if(!offset.IsEmpty) { cell.ViewInfo.Offset(-offset.X, -offset.Y); } } }
/// <summary> /// 进度条绘制 /// </summary> /// <param name="e"></param> public void DrawProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { Rectangle rec = new Rectangle(e.Bounds.X + 327, e.Bounds.Y+15, (this.Width /3)+50, (this.Height-500)-10); Graphics g = e.Graphics; g. SetGDIHigh(); RectangleF m_lineRectangle = new RectangleF(rec.X, rec.Y, rec.Width - 10 * 2, 10); GraphicsPath pathLine = Retc.CreateRoundedRectanglePaths(m_lineRectangle, 5); g.FillPath(new SolidBrush(Color.FromArgb(30, 42, 58)), pathLine); //进度绘制 string s = e.CellValue as string; string str = s; //s.Substring(0, e.CellValue.ToString().Length - 1); int ints = Convert.ToInt32(s); if (ints == 1) { ints += 1; }//不然单纯的1进度有点丑 decimal percent = Convert.ToDecimal(ints); int width = (int)(50 * Math.Abs(percent / 50) * e.Bounds.Width / 50); if (width >= rec.Width) { width = rec.Width; } Rectangle rect = new Rectangle(e.Bounds.X + 327, e.Bounds.Y+15 , width, (this.Height - 500)-10); SolidBrush b = new SolidBrush(Color.FromArgb(0, 224, 24)); if (percent > 20) { b = new SolidBrush(Color.FromArgb(230, 0, 0)); } else if (percent > 10) { b = new SolidBrush(Color.FromArgb(254, 208, 0)); } else if (percent == 0) { b = new SolidBrush(Color.Transparent); } GraphicsPath valueLine = Retc.CreateRoundedRectanglePaths(new RectangleF(rect.X, rect.Y, width - 10 * 2, 10), 5); g.FillPath(b, valueLine); }
public static GraphicsPath CreateRoundedRectanglePaths(this RectangleF rect, int cornerRadius) { GraphicsPath roundedRect = new GraphicsPath(); roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90); roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y); roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90); roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2); roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom); roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2); roundedRect.CloseFigure(); return roundedRect; }