/// <summary> /// 重写绘制单元格函数。 /// 首先调用DataGridViewTextBoxCell基类实现, /// 删除错误图标和前景内容,这两个部分由自定义实现, /// 在这个实例中通过调用Control.DrawToBitmap来实现,但这并不是最高性能的方式。 /// 另外一种是绘制图片的形式 /// </summary> /// <param name="graphics"></param> /// <param name="clipBounds"></param> /// <param name="cellBounds"></param> /// <param name="rowIndex"></param> /// <param name="cellState"></param> /// <param name="value"></param> /// <param name="formattedValue"></param> /// <param name="errorText"></param> /// <param name="cellStyle"></param> /// <param name="advancedBorderStyle"></param> /// <param name="paintParts"></param> 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) { if (this.DataGridView == null) { return; } //首先绘制单元格的边框和背景 base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts & ~(DataGridViewPaintParts.ErrorIcon | DataGridViewPaintParts.ContentForeground)); Point ptCurrentCell = this.DataGridView.CurrentCellAddress; bool cellCurrent = ptCurrentCell.X == this.ColumnIndex && ptCurrentCell.Y == rowIndex; bool cellEdited = cellCurrent && this.DataGridView.EditingControl != null; //如果单元格处于编辑模式则不绘制任何东西 if (!cellEdited) { if (PartPainted(paintParts, DataGridViewPaintParts.ContentForeground)) { //绘制TrackBar,并将边界也考虑其中 Rectangle borderWidths = BorderWidths(advancedBorderStyle);//获取边框跨距宽度 Rectangle valBounds = cellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; //将填充也考虑在内 if (cellStyle.Padding != Padding.Empty) { if (this.DataGridView.RightToLeft == RightToLeft.Yes) { valBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { valBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } valBounds.Width -= cellStyle.Padding.Horizontal; valBounds.Height -= cellStyle.Padding.Vertical; } // valBounds = GetAdjustedEditingControlBounds(valBounds, cellStyle); bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0; if (renderingBitmap.Width < valBounds.Width || renderingBitmap.Height < valBounds.Height) { //图像太小,需重新绘制一个大点的 renderingBitmap.Dispose(); renderingBitmap = new Bitmap(valBounds.Width, valBounds.Height); } //确定TrackBar控件是一个可见的父控件 if (paintingTrackBar.Parent == null || !paintingTrackBar.Parent.Visible) { paintingTrackBar.Parent = this.DataGridView; } //设置相关属性 paintingTrackBar.Width = valBounds.Width; paintingTrackBar.Height = valBounds.Height; paintingTrackBar.RightToLeft = this.DataGridView.RightToLeft; paintingTrackBar.Location = new Point(0, -paintingTrackBar.Height - 100); paintingTrackBar.Value = (int)formattedValue; //Color backColor; //if (PartPainted(paintParts, DataGridViewPaintParts.SelectionBackground) && cellSelected) //{ // backColor = cellStyle.SelectionBackColor; //} //else { // backColor = cellStyle.BackColor; //} //if (PartPainted(paintParts, DataGridViewPaintParts.Background)) { // if (backColor.A < 255) { // //TrackBar不知吃透明的背景颜色 // backColor = Color.FromArgb(255,backColor); // } // //TrackBar.DefaultBackColor = backColor; //} //最后绘制TrackBar控件 Rectangle srcRect = new Rectangle(0, 0, valBounds.Width, valBounds.Height); if (srcRect.Width > 0 && srcRect.Height > 0) { paintingTrackBar.DrawToBitmap(renderingBitmap, srcRect); graphics.DrawImage(renderingBitmap, new Rectangle(valBounds.Location, valBounds.Size), srcRect, GraphicsUnit.Pixel); } if (PartPainted(paintParts, DataGridViewPaintParts.ErrorIcon)) { //在TrackBar上面绘制潜在的错误图标 base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.ErrorIcon); } } } }