public void OnCellPainting (object sender, DataGridViewCellPaintingEventArgs e) { Console.WriteLine(e.CellBounds); Console.WriteLine(e.RowIndex); Console.WriteLine(e.ColumnIndex); foreach (DataGridViewCell cell in dgv.Rows[e.RowIndex].Cells) { Console.WriteLine("\t" + cell.RowIndex); Console.WriteLine("\t" + cell.ColumnIndex); Console.WriteLine("\t" + cell.ContentBounds); } //Console.WriteLine(dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ContentBounds); Console.WriteLine("----------------------"); Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width / 2, e.CellBounds.Height / 2); using ( Brush gridBrush = new SolidBrush(dgv.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { using (Pen gridLinePen = new Pen(gridBrush)) { // Erase the cell. e.Graphics.FillRectangle(backColorBrush, e.CellBounds); // Draw the grid lines (only the right and bottom lines; // DataGridView takes care of the others). /*e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); */ e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); // Draw the inset highlight box. e.Graphics.DrawRectangle(Pens.Blue, newRect); // Draw the text content of the cell, ignoring alignment. if (e.Value != null) { e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault); } e.Handled = true; } } }
private void ActivityGridCellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex < 0) return; if (e.RowIndex >= 0) { // var date = (DateTime?) grdActivity[e.ColumnIndex, e.RowIndex].Value; // if (!date.HasValue || date < dtpStart.Value || date > dtpEnd.Value) e.CellStyle.BackColor = cNone.Color; return; } e.CellStyle.BackColor = e.CellStyle.SelectionBackColor = _headerBrush.Color; e.PaintBackground(e.ClipBounds, false); // DrawHeaderCell(e.Graphics, e.ClipBounds, e.CellBounds, e.CellStyle.Font, grdActivity.Columns[e.ColumnIndex].HeaderText); e.Handled = true; }
private void metroGrid2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { }
private void dgvChiTiet_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { xl.grd_CellPainting(sender, e, this.dgvChiTiet, Font); }
private void Data_View_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { }
private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // override cell border painting to allow custom borders for door locations // source: http://stackoverflow.com/questions/32154847/how-do-you-draw-a-border-around-a-datagridview-cell-while-its-being-edited if (e.ColumnIndex > -1 & e.RowIndex > -1) { // set door pen options Color DOOR_COLOUR = Color.Black; int DOOR_BORDER_WIDTH = 5; // Pen for left and top borders using (var backGroundPen = new Pen(e.CellStyle.BackColor, 1)) // Pen for bottom and right borders using (var gridlinePen = new Pen(dgv.GridColor, 1)) // Pen for selected cell borders using (var selectedPen = new Pen(DOOR_COLOUR, DOOR_BORDER_WIDTH)) { var topLeftPoint = new Point(e.CellBounds.Left, e.CellBounds.Top); var topRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Top); var bottomRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); var bottomleftPoint = new Point(e.CellBounds.Left, e.CellBounds.Bottom - 1); string doorLocations = ""; if (home.Rooms != null && home.Rooms[e.ColumnIndex, e.RowIndex] != null) { doorLocations = home.Rooms[e.ColumnIndex, e.RowIndex].DoorLocations; } bool north = doorLocations.Contains("N"); bool east = doorLocations.Contains("E"); bool south = doorLocations.Contains("S"); bool west = doorLocations.Contains("W"); // Paint all parts except borders. e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border); // Top border of first row cells should be in background color if (e.RowIndex == 0) { e.Graphics.DrawLine(backGroundPen, topLeftPoint, topRightPoint); } else { // Top border of non-first row cells should be in gridLine color, and they should be drawn here after right border Pen borderPen = (north) ? selectedPen : gridlinePen; if (e.RowIndex > 0) { e.Graphics.DrawLine(borderPen, topLeftPoint, topRightPoint); } } // Right border of last column cells should be in gridLine color if (e.ColumnIndex == dgv.ColumnCount - 1) { e.Graphics.DrawLine(backGroundPen, bottomRightPoint, topRightPoint); } else { //Right border of non-last column cells should be in background color Pen borderPen = (east) ? selectedPen : gridlinePen; e.Graphics.DrawLine(borderPen, bottomRightPoint, topRightPoint); } // Bottom border of last row cells should be in gridLine color if (e.RowIndex == dgv.RowCount - 1) { e.Graphics.DrawLine(backGroundPen, bottomRightPoint, bottomleftPoint); } else { // Bottom border of non-last row cells should be in background color Pen borderPen = (south) ? selectedPen : gridlinePen; e.Graphics.DrawLine(borderPen, bottomleftPoint, bottomRightPoint); } // Left border of first column cells should be in background color if (e.ColumnIndex == 0) { e.Graphics.DrawLine(backGroundPen, topLeftPoint, bottomleftPoint); } else { // Left border of non-first column cells should be in gridLine color, and they should be drawn here after bottom border Pen borderPen = (west) ? selectedPen : gridlinePen; if (e.ColumnIndex > 0) { e.Graphics.DrawLine(borderPen, topLeftPoint, bottomleftPoint); } } // We handled painting for this cell, Stop default rendering. e.Handled = true; } } }
private void DrawCell(DataGridViewCellPaintingEventArgs e) { if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet) { e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } Brush gridBrush = new SolidBrush(this.GridColor); SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor); SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor); Pen gridLinePen = new Pen(gridBrush); #region 合并行单元格 if (this.MergeRows.Contains(e.RowIndex) && e.ColumnIndex > 0) { int cellHeight = e.CellBounds.Height; int leftCol = e.ColumnIndex, rightCol = this.ColumnCount - leftCol; if (this.Rows[e.RowIndex].Selected) { backBrush.Color = e.CellStyle.SelectionBackColor; fontBrush.Color = e.CellStyle.SelectionForeColor; } //以背景色填充 e.Graphics.FillRectangle(backBrush, e.CellBounds); PaintingFont(e, leftCol, 0, rightCol, 1); //最右侧边线 if (rightCol == 1) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); } // 底边线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); e.Handled = true; return; } #endregion #region 合并列单元格 int cellWidth = e.CellBounds.Width; int DownRows = 0, UpRows = 0, count = 0; string value = e.Value.ToString().Trim(); if (value != "") { for (int i = e.RowIndex; i < this.Rows.Count; i++) { if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Trim().Equals(value)) { DownRows++; } else { break; } } for (int i = e.RowIndex - 1; i >= 0; i--) { if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Trim().Equals(value)) { UpRows++; } else { break; } } count = DownRows + UpRows; if (count < 2) { return; } if (this.Rows[e.RowIndex].Selected) { backBrush.Color = e.CellStyle.SelectionBackColor; fontBrush.Color = e.CellStyle.SelectionForeColor; } //以背景色填充 e.Graphics.FillRectangle(backBrush, e.CellBounds); //画字符串 PaintingFont(e, 0, UpRows, 1, DownRows); if (DownRows == 1) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); count = 0; } // 画右边线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); e.Handled = true; } #endregion }
protected override void OnPaintCell(DataGridView grid, DataGridViewCellPaintingEventArgs e, SlotModel slotModel) { e.Handled = false; }
protected static void DrawLeftJustifiedText(DataGridViewCellPaintingEventArgs e, Color color, string value) { var pt = new Point(e.CellBounds.X + TextPadding, e.CellBounds.Y + 2); TextRenderer.DrawText(e.Graphics, value, e.CellStyle.Font, pt, color); }
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == -1 && e.ColumnIndex != -1) { using (GraphicsPath path = new GraphicsPath()) { IRenderer renderer = _palette.GetRenderer(); Rectangle rect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height); Rectangle rect2 = new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width + 2, e.CellBounds.Height + 1); path.AddRectangle(rect); Color gradStartColor = _palette.ColorTable.ToolStripGradientBegin; Color gradMiddleColor = _palette.ColorTable.MenuStripGradientEnd; using (RenderContext context = new RenderContext(this, e.Graphics, rect, renderer)) { if (e.State == DataGridViewElementStates.Selected) { _paletteBack.Style = PaletteBackStyle.HeaderPrimary; _paletteBorder.Style = PaletteBorderStyle.HeaderPrimary; _paletteContent.Style = PaletteContentStyle.HeaderPrimary; } else { _paletteBack.Style = PaletteBackStyle.HeaderPrimary; _paletteBorder.Style = PaletteBorderStyle.HeaderPrimary; _paletteContent.Style = PaletteContentStyle.HeaderPrimary; } //_mementoBack = renderer.RenderStandardBack.DrawBack(context, e.CellBounds, path, _paletteBack, VisualOrientation.Top, PaletteState.Normal, _mementoBack); //Empty Area e.Graphics.FillRectangle(new SolidBrush(Color.White), rect); //Fill Gradient using (LinearGradientBrush brush = new LinearGradientBrush(rect2, gradStartColor, gradMiddleColor, LinearGradientMode.Vertical)) { e.Graphics.FillRectangle(brush, rect); } renderer.RenderStandardBorder.DrawBorder(context, new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width + 2, e.CellBounds.Height + 1), _paletteBorder, VisualOrientation.Top, PaletteState.Normal); } } e.Paint(e.CellBounds, (DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Border)); e.Handled = true; } else if (e.RowIndex >= 0 && e.ColumnIndex > -1 && ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)) { using (GraphicsPath path = new GraphicsPath()) { IRenderer renderer = _palette.GetRenderer(); path.AddRectangle(new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)); using (RenderContext context = new RenderContext(this, e.Graphics, e.ClipBounds, renderer)) { _paletteBack.Style = PaletteBackStyle.ButtonListItem; _paletteBorder.Style = PaletteBorderStyle.ButtonNavigatorStack; _mementoBack = renderer.RenderStandardBack.DrawBack(context, new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height), path, _paletteBack, VisualOrientation.Top, PaletteState.Tracking, _mementoBack); renderer.RenderStandardBorder.DrawBorder(context, new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height), _paletteBorder, VisualOrientation.Top, PaletteState.Normal); } } e.Paint(e.CellBounds, (DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Border)); e.Handled = true; } else if (e.ColumnIndex == -1) { using (GraphicsPath path = new GraphicsPath()) { IRenderer renderer = _palette.GetRenderer(); path.AddRectangle(new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)); Color gradStartColor = _palette.ColorTable.ToolStripGradientBegin; Color gradMiddleColor = _palette.ColorTable.MenuStripGradientEnd; using (RenderContext context = new RenderContext(this, e.Graphics, e.ClipBounds, renderer)) { if (e.State == DataGridViewElementStates.Selected) { _paletteBack.Style = PaletteBackStyle.HeaderPrimary; _paletteBorder.Style = PaletteBorderStyle.HeaderPrimary; } else { _paletteBack.Style = PaletteBackStyle.HeaderPrimary; _paletteBorder.Style = PaletteBorderStyle.HeaderPrimary; } //_mementoBack = renderer.RenderStandardBack.DrawBack(context, e.CellBounds, path, _paletteBack, VisualOrientation.Top, PaletteState.Normal, _mementoBack); renderer.RenderStandardBorder.DrawBorder(context, new Rectangle(e.CellBounds.X, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height + 1), _paletteBorder, VisualOrientation.Top, PaletteState.Normal); //Empty Area e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds); //Fill Gradient using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, gradStartColor, gradMiddleColor, LinearGradientMode.Vertical)) { e.Graphics.FillRectangle(brush, e.CellBounds); } } } e.Paint(new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height - 1), (DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Border)); e.Handled = true; } else { base.OnCellPainting(e); } }
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { try { //@ dataGridView.Rows[e.RowIndex].Height = 120 + (e.RowIndex % 10) * 20; bool _isDrawThumbnail; Graphics _g = e.Graphics; Post _post = m_postBS[e.RowIndex] as Post; bool _selected = ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected); //Color _fcolor = (_selected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor); //Color _bcolor = (_selected ? e.CellStyle.SelectionBackColor : e.CellStyle.BackColor); int _X = e.CellBounds.Left + e.CellStyle.Padding.Left; int _Y = e.CellBounds.Top + e.CellStyle.Padding.Top; int _W = e.CellBounds.Width - (e.CellStyle.Padding.Left + e.CellStyle.Padding.Right); int _H = e.CellBounds.Height - (e.CellStyle.Padding.Top + e.CellStyle.Padding.Bottom); Rectangle _cellRect = new Rectangle(_X, _Y, _W, _H); // Draw background if (_selected) { _g.FillRectangle(m_bgSelectedBrush, e.CellBounds); } else { if (Main.Current.RT.CurrentGroupHaveReadPosts.Contains(_post.post_id)) { _g.FillRectangle(m_bgReadBrush, e.CellBounds); } else { _g.FillRectangle(m_bgUnReadBrush, e.CellBounds); } } _g.DrawRectangle(Pens.White, e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 2, e.CellBounds.Height - 2); Rectangle _timeRect = DrawPostTime(_g, m_fontPostTime, _cellRect, _post); DrawFavoriteIcon(_g, _post, e.CellBounds); Rectangle _thumbnailRect = new Rectangle(_X + 4, _Y + 8, PicWidth, PicHeight); _isDrawThumbnail = DrawThumbnail(_g, _thumbnailRect, _post); int _offsetThumbnail_W = (_isDrawThumbnail ? _thumbnailRect.Width : 0); switch (_post.type) { case "text": Draw_Text_Post(_g, _post, _cellRect, _timeRect.Height, m_fontText); break; case "rtf": Draw_RichText_Post(_g, _post, _cellRect, _timeRect.Height, m_fontText, _thumbnailRect.Width); break; case "image": case "doc": Draw_Photo_Doc_Post(_g, _post, _cellRect, _timeRect.Height, m_fontPhotoInfo, m_fontText, _thumbnailRect.Width, _selected); break; case "link": Draw_Link(_g, _post, _cellRect, _timeRect.Height, m_fontPhotoInfo, _thumbnailRect.Width, _selected); break; } } catch (Exception _e) { NLogUtility.Exception(s_logger, _e, "dataGridView_CellPainting"); e.Handled = false; return; } // Let them know we handled it e.Handled = true; }
private void cluesDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // first paint over the submit button if (e.RowIndex <= numberOfAllowedGuesses - 1 - guessHistory.Count) { Brush bg = new SolidBrush(backgroundColor); e.Graphics.FillRectangle(bg, e.ClipBounds); bg.Dispose(); e.PaintContent(e.ClipBounds); e.Handled = true; } else if (e.RowIndex > numberOfAllowedGuesses - 1 - guessHistory.Count && guessHistory.Count > 0) { int guessHistoryIndex = numberOfAllowedGuesses - e.RowIndex - 1; Guess guessAtRow = guessHistory.ElementAt(guessHistoryIndex); int rightColorRightPlace = guessAtRow.getRightColorRightPlace(); int rightColorWrongPlace = guessAtRow.getRightColorWrongPlace(); Color c = backgroundColor; String direction = ""; if (e.ColumnIndex < rightColorRightPlace) { c = rightColorRightPlaceMarker; direction = "up"; } else if (numberOfSpaces - e.ColumnIndex <= rightColorWrongPlace) { c = rightColorWrongPlaceMarker; direction = "down"; } else { e.Handled = true; return; } e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; int rectangleLocationX = e.CellBounds.X + 3; int rectangleLocationY = e.CellBounds.Y + 3; int rectangleWidth = Math.Min(e.CellBounds.Height, e.CellBounds.Width) - 10; int rectangleHeight = rectangleWidth; System.Drawing.Rectangle spotRectangle = new System.Drawing.Rectangle(rectangleLocationX, rectangleLocationY, rectangleWidth, rectangleHeight); System.Drawing.Rectangle gradientBigRectangle = new System.Drawing.Rectangle(rectangleLocationX, rectangleLocationY, rectangleWidth, rectangleHeight * 3); System.Drawing.Rectangle gradientBiggerRectangle = new System.Drawing.Rectangle(rectangleLocationX - 10, rectangleLocationY - 10, rectangleWidth + 20, rectangleHeight * 3); float filledSpotGradientAngle = (float)220.0; float emptySpotGradientAngle = (float)180.0; float anotherGradientAngle = (float)90.0; System.Drawing.Drawing2D.LinearGradientBrush normalPenBrush = new LinearGradientBrush(gradientBiggerRectangle, Color.LightGray, Color.Black, emptySpotGradientAngle, false); System.Drawing.Pen normalPen = new System.Drawing.Pen(normalPenBrush, 1.0f); System.Drawing.Drawing2D.LinearGradientBrush br; br = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.Black, c, filledSpotGradientAngle, false); System.Drawing.Drawing2D.LinearGradientBrush br2; br2 = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.LightGray, c, emptySpotGradientAngle, false); System.Drawing.Drawing2D.LinearGradientBrush br3; br3 = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.White, c, anotherGradientAngle, false); Point[] triangle = DrawTriangle(spotRectangle, direction); Point[][] pyramid = DrawPyramid(spotRectangle, direction); e.Graphics.FillPolygon(br, pyramid[0]); e.Graphics.FillPolygon(br2, pyramid[1]); e.Graphics.FillPolygon(br3, pyramid[2]); e.Graphics.DrawPolygon(normalPen, triangle); e.PaintContent(e.ClipBounds); e.Handled = true; normalPen.Dispose(); br.Dispose(); } }
private void guessHistoryDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; int ellipseLocationX = e.CellBounds.X + 6; int ellipseLocationY = e.CellBounds.Y + 6; int ellipseWidth = Math.Min(e.CellBounds.Height, e.CellBounds.Width) - 10; int ellipseHeight = ellipseWidth; System.Drawing.Rectangle spotEllipseRectangle = new System.Drawing.Rectangle(ellipseLocationX, ellipseLocationY, ellipseWidth, ellipseHeight); System.Drawing.Rectangle gradientBigRectangle = new System.Drawing.Rectangle(ellipseLocationX, ellipseLocationY, ellipseWidth, ellipseHeight * 2); float filledSpotGradientAngle = (float)220.0; float emptySpotGradientAngle = (float)70.0; System.Drawing.Drawing2D.LinearGradientBrush highlightPenBrush = new LinearGradientBrush(gradientBigRectangle, Color.DarkGray, selectionBackgroundColor, emptySpotGradientAngle, false); System.Drawing.Drawing2D.LinearGradientBrush normalPenBrush = new LinearGradientBrush(spotEllipseRectangle, Color.LightGray, Color.Black, emptySpotGradientAngle, false); System.Drawing.Pen normalPen = new System.Drawing.Pen(normalPenBrush, 4.0f); System.Drawing.Pen highlightPen = new System.Drawing.Pen(highlightPenBrush, 4.0f); System.Drawing.Drawing2D.LinearGradientBrush br; // empty spots above the current guess if (e.RowIndex < numberOfAllowedGuesses - 1 - numberOfGuesses) { br = new System.Drawing.Drawing2D.LinearGradientBrush(spotEllipseRectangle, Color.Black, Color.LightGray, emptySpotGradientAngle, false); e.Graphics.FillEllipse(br, spotEllipseRectangle); } // current guess else if (e.RowIndex == numberOfAllowedGuesses - 1 - numberOfGuesses) { // player has already chosen a color in this spot of the current guess if (guessInProgress[e.ColumnIndex] != backgroundColor) { Color c = guessInProgress[e.ColumnIndex]; br = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.Black, c, filledSpotGradientAngle, false); e.Graphics.FillEllipse(br, spotEllipseRectangle); // hilite ellipse = based on http://www3.telus.net/ryanfransen/article_glassspheres.html int r3w = Convert.ToInt16(spotEllipseRectangle.Width * 0.5); int r3h = Convert.ToInt16(spotEllipseRectangle.Height * 0.3); Rectangle r3 = new Rectangle( new Point(spotEllipseRectangle.Location.X + (spotEllipseRectangle.Width / 4), spotEllipseRectangle.Location.Y + 2), new Size(r3w, r3h)); LinearGradientBrush br2 = new LinearGradientBrush(r3, Color.White, Color.Transparent, 90); br2.WrapMode = WrapMode.TileFlipX; e.Graphics.FillEllipse(br2, r3); br2.Dispose(); } // player has not chosen a color, so this spot is the same as the empty rows above else { br = new System.Drawing.Drawing2D.LinearGradientBrush(spotEllipseRectangle, Color.Black, Color.LightGray, emptySpotGradientAngle, false); e.Graphics.FillEllipse(br, spotEllipseRectangle); } } // previous guesses: fill color based on guess history else { int guessHistoryIndex = numberOfAllowedGuesses - e.RowIndex - 1; Guess guessAtRow = guessHistory.ElementAt(guessHistoryIndex); Color guessColor = guessAtRow.getColor(e.ColumnIndex); br = new System.Drawing.Drawing2D.LinearGradientBrush(gradientBigRectangle, Color.Black, guessColor, filledSpotGradientAngle, false); e.Graphics.FillEllipse(br, spotEllipseRectangle); // hilite ellipse = based on http://www3.telus.net/ryanfransen/article_glassspheres.html int r3w = Convert.ToInt16(spotEllipseRectangle.Width * 0.5); int r3h = Convert.ToInt16(spotEllipseRectangle.Height * 0.3); Rectangle r3 = new Rectangle( new Point(spotEllipseRectangle.Location.X + (spotEllipseRectangle.Width / 4), spotEllipseRectangle.Location.Y + 2), new Size(r3w, r3h)); LinearGradientBrush br2 = new LinearGradientBrush(r3, Color.White, Color.Transparent, 90); br2.WrapMode = WrapMode.TileFlipX; e.Graphics.FillEllipse(br2, r3); br2.Dispose(); } // add the external ring e.Graphics.DrawEllipse(normalPen, spotEllipseRectangle); if (e.ColumnIndex == selectedCell.ColumnIndex && e.RowIndex == selectedCell.RowIndex) { e.Graphics.DrawEllipse(highlightPen, spotEllipseRectangle); } e.PaintContent(e.ClipBounds); e.Handled = true; normalPen.Dispose(); highlightPen.Dispose(); br.Dispose(); }
private void dgvMttoBancosReporte_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { try { if (e.RowIndex < 0) { return; } if (dgvMttoBancosReporte.Rows[e.RowIndex].Cells[(int)ColumasReporte.Moneda].Value.ToString().Contains("Total")) { } else { if (e.ColumnIndex == 0) { Brush gridColor = new SolidBrush(dgvMttoBancosReporte.GridColor); Brush backColorCell = new SolidBrush(e.CellStyle.BackColor); // Pen gridLinePen = new Pen(gridColor); e.Graphics.FillRectangle(backColorCell, e.CellBounds); // if (e.RowIndex < dgvMttoBancosReporte.Rows.Count && dgvMttoBancosReporte.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1); } e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); // if (String.IsNullOrEmpty(e.Value.ToString())) { if (e.RowIndex > 0 && dgvMttoBancosReporte.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString()) { } else { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault); } } else { if (e.RowIndex == 0) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault); } if (e.RowIndex > 0) { if (dgvMttoBancosReporte.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault); } } } e.Handled = true; } } } catch (Exception ex) { //MessageBox.Show(ex.Message, "Error inesperado.", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public override void OnCellPainting(DataGridViewCellPaintingEventArgs e, GitRevision revision, int rowHeight, in (Brush backBrush, Color foreColor, Font normalFont, Font boldFont) style)
private void dataView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex != -1 && isInList(e.RowIndex)) { using ( Brush gridBrush = new SolidBrush(this.dataView.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor) ) { using (Pen gridLinePen = new Pen(gridBrush)) { try { // 清除单元格 if (e.Value != null) { e.CellStyle.Font = new System.Drawing.Font(dataView.DefaultCellStyle.Font, FontStyle.Regular); e.CellStyle.WrapMode = DataGridViewTriState.True; e.Graphics.FillRectangle(backColorBrush, e.CellBounds); e.Handled = true; // 画 Grid 边线(仅画单元格的底边线和右边线) // 如果下一列和当前列的数据不同,则在当前的单元格画一条右边线 if (e.ColumnIndex < dataView.Columns.Count - 1 && (dataView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value == null || dataView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() != e.Value.ToString())) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); } //画最后一条记录的右边线 if (e.ColumnIndex == dataView.Columns.Count - 1) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); } //画底边线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); } // 画(填写)单元格内容,相同的内容的单元格只填写第一个 if (e.Value != null) { if (e.ColumnIndex == dataView.Columns.Count - 1 && dataView.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString() != e.Value.ToString()) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.Left, e.CellBounds.Top + 4, StringFormat.GenericDefault); } if (e.ColumnIndex > 0 && dataView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() == e.Value.ToString()) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.Left, e.CellBounds.Top + 4, StringFormat.GenericDefault); } if (e.ColumnIndex > 0 && dataView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() != e.Value.ToString() && dataView.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString() != e.Value.ToString()) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.Left, e.CellBounds.Top + 4, StringFormat.GenericDefault); } } //e.Handled = true; } catch { } } } } if (e.ColumnIndex == 0 && (isInList(e.RowIndex) || isInList(e.RowIndex - 1)) && e.Value != null) { if (isInList(e.RowIndex)) { e.CellStyle.Font = new System.Drawing.Font(dataView.DefaultCellStyle.Font, FontStyle.Bold); e.CellStyle.WrapMode = DataGridViewTriState.True; } using ( Brush gridBrush = new SolidBrush(this.dataView.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { using (Pen gridLinePen = new Pen(gridBrush)) { // 擦除原单元格背景 e.Graphics.FillRectangle(backColorBrush, e.CellBounds); ////绘制线条,这些线条是单元格相互间隔的区分线条, ////因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条 if (e.RowIndex != 0 && this.dataView.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value != null) { if (e.Value.ToString() != this.dataView.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString()) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Top - 1, e.CellBounds.Right - 1, e.CellBounds.Top - 1);//上边缘的线 e.Handled = true; //绘制值 if (e.Value != null) { e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, Brushes.Black, e.CellBounds.Left, e.CellBounds.Top + 4, StringFormat.GenericDefault); } } } else { //e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, //e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线 //绘制值 if (e.Value != null) { e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, Brushes.Black, e.CellBounds.Left, e.CellBounds.Top + 4, StringFormat.GenericDefault); } } if (isInList(e.RowIndex - 1)) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线 } //右侧的线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); e.Handled = true; } } } }
/// <summary> /// 画单元格 /// </summary> /// <param name="e"></param> private void DrawCell(DataGridViewCellPaintingEventArgs e) { if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet) { e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } Brush gridBrush = new SolidBrush(this.GridColor); SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor); SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor); int cellwidth; //上面相同的行数 int UpRows = 0; //下面相同的行数 int DownRows = 0; //总行数 int count = 0; if (this.MergeColumnNames.Contains(this.Columns[e.ColumnIndex].Name) && e.RowIndex != -1) { cellwidth = e.CellBounds.Width; Pen gridLinePen = new Pen(gridBrush); string curValue = e.Value == null ? "" : e.Value.ToString().Trim(); string curSelected = this.CurrentRow.Cells[e.ColumnIndex].Value == null ? "" : this.CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim(); if (!string.IsNullOrEmpty(curValue)) { #region 获取下面的行数 for (int i = e.RowIndex; i < this.Rows.Count; i++) { if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue)) { //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected; DownRows++; if (e.RowIndex != i) { cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width; } } else { break; } } #endregion #region 获取上面的行数 for (int i = e.RowIndex; i >= 0; i--) { if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue)) { //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected; UpRows++; if (e.RowIndex != i) { cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width; } } else { break; } } #endregion count = DownRows + UpRows - 1; if (count < 2) { return; } } if (this.Rows[e.RowIndex].Selected) { backBrush.Color = e.CellStyle.SelectionBackColor; fontBrush.Color = e.CellStyle.SelectionForeColor; } //以背景色填充 e.Graphics.FillRectangle(backBrush, e.CellBounds); //画字符串 PaintingFont(e, cellwidth, UpRows, DownRows, count); if (DownRows == 1) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); count = 0; } // 画右边线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); e.Handled = true; } }
private void _dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { DrawCell(e.Graphics, e.CellBounds, e.RowIndex, e.ColumnIndex, e.Value, e.State); e.Handled = true; }
public string GetCellText(DataGridViewCellPaintingEventArgs e, SlotModel slotModel) { return(OnGetCellText(e.Value, slotModel)); }
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { //行标题 if (e.RowIndex > -1 && e.ColumnIndex > -1) { base.OnCellPainting(e); return; } if (TopRow == null) { return; } //绘制一级标题、二级标题 if (TopRow.Cells.Count > 0 && e.RowIndex == -1 && e.ColumnIndex > -1) { using (Brush gridBrush = new SolidBrush(this.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { using (Pen gridLinePen = new Pen(gridBrush)) { //擦除背景 e.Graphics.FillRectangle(backColorBrush, e.CellBounds); //绘制底部边框线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom, e.CellBounds.Right, e.CellBounds.Bottom); if (TopRow.Cells[e.ColumnIndex].ColumnSpan > 1) { if (e.ColumnIndex == this.Columns.Count - 1) { //绘制二级标题右边框线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right, e.CellBounds.Top + e.ClipBounds.Height / 2, e.CellBounds.Right, e.CellBounds.Bottom); } else { //绘制二级标题右边框线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top + e.ClipBounds.Height / 2, e.CellBounds.Right - 1, e.CellBounds.Bottom); } //绘制二级标题顶部边框线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - e.CellBounds.Height / 2, e.CellBounds.Right, e.CellBounds.Bottom - e.CellBounds.Height / 2); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; //绘制一级标题文本 if (TopRow.Cells[e.ColumnIndex].ColumnIndex > -1 && !string.IsNullOrWhiteSpace(TopRow.Cells[e.ColumnIndex].HeaderText)) { Rectangle rect = new Rectangle(e.CellBounds.X - TopRow.Cells[e.ColumnIndex].SpanPreColumnWith, e.CellBounds.Y, TopRow.Cells[e.ColumnIndex].SpanColumnWith, e.CellBounds.Height / 2); e.Graphics.DrawString(TopRow.Cells[e.ColumnIndex].HeaderText, e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), rect, sf); } //绘制二级标题文本 e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), new Rectangle(e.CellBounds.X, e.CellBounds.Y + e.CellBounds.Height / 2, e.CellBounds.Width, e.CellBounds.Height / 2), sf); } else { if (e.ColumnIndex == this.Columns.Count - 1) { //绘制一级标题右边框线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Bottom); } else { //绘制一级标题右边框线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); } //绘制二级标题文本 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf); } e.Handled = true; } } } }
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (IsNameRow(e.RowIndex)) { return; } e.Graphics.FillRectangle(Brushes.White, e.CellBounds); //background e.Graphics.DrawLine(Pens.Black, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1); //border var action = GetStakeholderActionForRow(e.RowIndex); //var horizontalRectMargin = (e.CellBounds.Width - RectangleWidth)/2; var horizontalRectMargin = (e.CellBounds.Width - RectangleWidth - ActionWidth - ArrowWidth) / 2; var verticalRectMargin = 5; var actionColor = DecisionStateColor.ConvertStateToColor(action.Decision.State); if (_currentSortState == SortState.Stakeholder) { actionColor = GetColorForAction(action.Action); } //draw action button var actionRectangle = new Rectangle( e.CellBounds.X + horizontalRectMargin, e.CellBounds.Y + verticalRectMargin, ActionWidth, dataGridView1.RowTemplate.Height - 2 * verticalRectMargin); var actionPath = RoundedRectangle.Create(actionRectangle); e.Graphics.FillPath(new SolidBrush(actionColor), actionPath); var text = GetActionButtonText(action); DrawText(e.Graphics, text, actionRectangle); //draw arrow between var arrowPen = new Pen(Color.Black, 5f); arrowPen.StartCap = LineCap.Flat; arrowPen.EndCap = LineCap.ArrowAnchor; e.Graphics.DrawLine(arrowPen, e.CellBounds.X + horizontalRectMargin + ActionWidth + ArrowMargin, e.CellBounds.Y + (e.CellBounds.Height / 2), e.CellBounds.X + horizontalRectMargin + ActionWidth + ArrowWidth - ArrowMargin, e.CellBounds.Y + (e.CellBounds.Height / 2) ); var color = DecisionStateColor.ConvertStateToColor(action.Decision.State); if (_currentSortState == SortState.Decision) { color = GetColorForAction(action.Action); } var decisionRectangle = new Rectangle( e.CellBounds.X + horizontalRectMargin + ActionWidth + ArrowWidth, e.CellBounds.Y + verticalRectMargin, RectangleWidth, dataGridView1.RowTemplate.Height - 2 * verticalRectMargin); text = GetReceiverButtonText(action); var path = RoundedRectangle.Create(decisionRectangle); e.Graphics.FillPath(new SolidBrush(color), path); DrawText(e.Graphics, text, decisionRectangle); //var text = ActionToSentence(action.Action, action.Decision); //var stringSize = e.Graphics.MeasureString(text, DefaultFont); //var horizontalMargin = (rectangle.Width - stringSize.Width) / 2; //var verticalMargin = (rectangle.Height - stringSize.Height) / 2; //textRectangle = new RectangleF(rectangle.X + horizontalMargin, rectangle.Y + verticalMargin, stringSize.Width, stringSize.Height); //e.Graphics.DrawString(text, DefaultFont, Brushes.Black, textRectangle); e.Handled = true; }
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { try { if (e.RowIndex > -1 && e.ColumnIndex > -1) { DrawCell(e); } else { //二维表头 if (e.RowIndex == -1) { if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列 { //画边框 Graphics g = e.Graphics; e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); int left = e.CellBounds.Left, top = e.CellBounds.Top + 2, right = e.CellBounds.Right, bottom = e.CellBounds.Bottom; switch (SpanRows[e.ColumnIndex].Position) { case 1: left += 2; break; case 2: break; case 3: right -= 2; break; } //画上半部分底色 g.FillRectangle(new SolidBrush(this._mergecolumnheaderbackcolor), left, top, right - left, (bottom - top) / 2); //画中线 g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2, right, (top + bottom) / 2); //写小标题 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(e.Value + "", e.CellStyle.Font, Brushes.Black, new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf); left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2; if (left < 0) { left = this.GetCellDisplayRectangle(-1, -1, true).Width; } right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2; if (right < 0) { right = this.Width; } g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black, new Rectangle(left, top, right - left, (bottom - top) / 2), sf); e.Handled = true; } } } base.OnCellPainting(e); } catch { } }
private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { GridViewHelper.MergeColumns(this.dataGridView1, e, 0); }
//画制单元格 private static void MeragePrint(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex) { int width = 0; //合并后单元格总宽度 int height = cellArgs.CellBounds.Height; //合并后单元格总高度 for (int i = minColIndex; i <= maxColIndex; i++) { width += ((Rectangle)rowSpan[i]).Width; } Rectangle rectBegin = (Rectangle)rowSpan[minColIndex]; //合并第一个单元格的位置信息 Rectangle rectEnd = (Rectangle)rowSpan[maxColIndex]; //合并最后一个单元格的位置信息 //合并单元格的位置信息 /* Rectangle reBounds = new Rectangle(); * reBounds.X = rectBegin.X; * reBounds.Y = rectBegin.Y; * reBounds.Width = width - 1; * reBounds.Height = height - 1;*/ using (Brush gridBrush = new SolidBrush(dgv.GridColor), backColorBrush = new SolidBrush(cellArgs.CellStyle.BackColor)) { using (Pen gridLinePen = new Pen(gridBrush)) { // 画出上下两条边线,左右边线无 Point blPoint = new Point(rectBegin.Left, rectBegin.Bottom - 1); //底线左边位置 Point brPoint = new Point(rectEnd.Right - 1, rectEnd.Bottom - 1); //底线右边位置 cellArgs.Graphics.DrawLine(gridLinePen, blPoint, brPoint); //下边线 Point tlPoint = new Point(rectBegin.Left, rectBegin.Top); //上边线左边位置 Point trPoint = new Point(rectEnd.Right - 1, rectEnd.Top); //上边线右边位置 cellArgs.Graphics.DrawLine(gridLinePen, tlPoint, trPoint); //上边线 Point ltPoint = new Point(rectBegin.Left, rectBegin.Top); //左边线顶部位置 Point lbPoint = new Point(rectBegin.Left, rectBegin.Bottom - 1); //左边线底部位置 cellArgs.Graphics.DrawLine(gridLinePen, ltPoint, lbPoint); //左边线 Point rtPoint = new Point(rectEnd.Right - 1, rectEnd.Top); //右边线顶部位置 Point rbPoint = new Point(rectEnd.Right - 1, rectEnd.Bottom - 1); //右边线底部位置 cellArgs.Graphics.DrawLine(gridLinePen, rtPoint, rbPoint); //右边线 //计算绘制字符串的位置 SizeF sf = cellArgs.Graphics.MeasureString(rowValue, cellArgs.CellStyle.Font); float lstr = (width - sf.Width) / 2; float rstr = (height - sf.Height) / 2; //画出文本框 if (rowValue != "") { cellArgs.Graphics.DrawString(rowValue, cellArgs.CellStyle.Font, new SolidBrush(cellArgs.CellStyle.ForeColor), rectBegin.Left + lstr, rectBegin.Top + rstr, StringFormat.GenericDefault); } } cellArgs.Handled = true; } }
private void dgvBillList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { //e. }
static void Patches_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { }
private void dgvPhieuKham_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { xl.grd_CellPainting(sender, e, this.dgvPhieuKham, Font); }
public void OnPaint(DataGridViewCellPaintingEventArgs e) { }
private static void Grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // Header and cell borders must be set to "Single" style. var grid = (DataGridView)sender; var firstVisibleColumn = grid.Columns.Cast <DataGridViewColumn>().Where(x => x.Displayed).Min(x => x.Index); var lastVisibleColumn = grid.Columns.Cast <DataGridViewColumn>().Where(x => x.Displayed).Max(x => x.Index); var selected = e.RowIndex > -1 ? grid.Rows[e.RowIndex].Selected : false; e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border); var bounds = e.CellBounds; var tl = new Point(bounds.X, bounds.Y); var tr = new Point(bounds.X + bounds.Width - 1, bounds.Y); var bl = new Point(bounds.X, bounds.Y + bounds.Height - 1); var br = new Point(bounds.X + bounds.Width - 1, bounds.Y + bounds.Height - 1); Color backColor; // If top left corner and column header then... if (e.RowIndex == -1) { backColor = selected ? grid.ColumnHeadersDefaultCellStyle.SelectionBackColor : grid.ColumnHeadersDefaultCellStyle.BackColor; } // If row header then... else if (e.ColumnIndex == -1 && e.RowIndex > -1) { var row = grid.Rows[e.RowIndex]; backColor = selected ? row.HeaderCell.Style.SelectionBackColor : grid.RowHeadersDefaultCellStyle.BackColor; } // If normal cell then... else { var row = grid.Rows[e.RowIndex]; var cell = row.Cells[e.ColumnIndex]; backColor = selected ? cell.InheritedStyle.SelectionBackColor : cell.InheritedStyle.BackColor; } // Cell background colour. var back = new Pen(backColor, 1); // Border colour. var border = new Pen(SystemColors.Control, 1); // Do not draw borders for selected device. Pen c; // Top e.Graphics.DrawLine(back, tl, tr); // Left (only if not first) c = !selected && e.ColumnIndex > firstVisibleColumn ? border : back; e.Graphics.DrawLine(c, bl, tl); // Right (always) c = back; e.Graphics.DrawLine(c, tr, br); // Bottom (always) c = border; e.Graphics.DrawLine(c, bl, br); back.Dispose(); border.Dispose(); e.Handled = true; }
private void dtgrd_manufacturer_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { //Draw only grid content cells not ColumnHeader cells nor RowHeader cells if (e.ColumnIndex > -1 & e.RowIndex > -1) { //Pen for left and top borders using (var backGroundPen = new Pen(e.CellStyle.BackColor, 1)) //Pen for bottom and right borders using (var gridlinePen = new Pen(dtgrd_manufacturer.GridColor, 1)) //Pen for selected cell borders using (var selectedPen = new Pen(Color.ForestGreen, 1)) { var topLeftPoint = new Point(e.CellBounds.Left, e.CellBounds.Top); var topRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Top); var bottomRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); var bottomleftPoint = new Point(e.CellBounds.Left, e.CellBounds.Bottom - 1); //Draw selected cells here if (this.dtgrd_manufacturer[e.ColumnIndex, e.RowIndex].Selected) { //Paint all parts except borders. e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border); //Draw selected cells border here e.Graphics.DrawRectangle(selectedPen, new Rectangle(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width - 1, e.CellBounds.Height - 1)); //Handled painting for this cell, Stop default rendering. e.Handled = true; } //Draw non-selected cells here else { //Paint all parts except borders. e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border); //Top border of first row cells should be in background color if (e.RowIndex == 0) { e.Graphics.DrawLine(backGroundPen, topLeftPoint, topRightPoint); } //Left border of first column cells should be in background color if (e.ColumnIndex == 0) { e.Graphics.DrawLine(backGroundPen, topLeftPoint, bottomleftPoint); } //Bottom border of last row cells should be in gridLine color if (e.RowIndex == dtgrd_manufacturer.RowCount - 1) { e.Graphics.DrawLine(gridlinePen, bottomRightPoint, bottomleftPoint); } else //Bottom border of non-last row cells should be in background color { e.Graphics.DrawLine(backGroundPen, bottomRightPoint, bottomleftPoint); } //Right border of last column cells should be in gridLine color if (e.ColumnIndex == dtgrd_manufacturer.ColumnCount - 1) { e.Graphics.DrawLine(gridlinePen, bottomRightPoint, topRightPoint); } else //Right border of non-last column cells should be in background color { e.Graphics.DrawLine(backGroundPen, bottomRightPoint, topRightPoint); } //Top border of non-first row cells should be in gridLine color, and they should be drawn here after right border if (e.RowIndex > 0) { e.Graphics.DrawLine(gridlinePen, topLeftPoint, topRightPoint); } //Left border of non-first column cells should be in gridLine color, and they should be drawn here after bottom border if (e.ColumnIndex > 0) { e.Graphics.DrawLine(gridlinePen, topLeftPoint, bottomleftPoint); } //We handled painting for this cell, Stop default rendering. e.Handled = true; } } } }
/// <summary> /// 画单元格 /// </summary> /// <param name="e"></param> private void DrawCell(DataGridViewCellPaintingEventArgs e) { if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet) { e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } Brush gridBrush = new SolidBrush(this.GridColor); SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor); SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor); int cellwidth; //上面相同的行数 int UpRows = 0; //下面相同的行数 int DownRows = 0; //总行数 int count = 0; if (this.MergeColumnNames.Contains(this.Columns[e.ColumnIndex].Name) && e.RowIndex != -1) { cellwidth = e.CellBounds.Width; Pen gridLinePen = new Pen(gridBrush); string curValue = e.Value == null ? "" : e.Value.ToString().Trim(); string curSelected = this.CurrentRow.Cells[e.ColumnIndex].Value == null ? "" : this.CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim(); if (!string.IsNullOrEmpty(curValue)) { #region 获取下面的行数 for (int i = e.RowIndex; i < this.Rows.Count; i++) { if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue)) { //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected; DownRows++; if (e.RowIndex != i) { cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width; } } else { break; } } #endregion #region 获取上面的行数 for (int i = e.RowIndex; i >= 0; i--) { if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue)) { //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected; UpRows++; if (e.RowIndex != i) { cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width; } } else { break; } } #endregion count = DownRows + UpRows - 1; if (count < 2) { return; } } if (this.Rows[e.RowIndex].Selected) { backBrush.Color = e.CellStyle.SelectionBackColor; fontBrush.Color = e.CellStyle.SelectionForeColor; } //以背景色填充 e.Graphics.FillRectangle(backBrush, e.CellBounds); //画字符串 PaintingFont(e, cellwidth, UpRows, DownRows, count); if (DownRows == 1) { e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); count = 0; } // 画右边线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); e.Handled = true; } }
protected static Color GetBackColorOrSelectionBackColor(DataGridView grid, DataGridViewCellPaintingEventArgs e, Color backColor) { return(grid.Rows[e.RowIndex].Selected ? e.CellStyle.SelectionBackColor : backColor); }
protected static Color GetTextColorOrSelectionTextColor(DataGridView grid, DataGridViewCellPaintingEventArgs e) { return(grid.Rows[e.RowIndex].Selected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor); }
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { try { if (e.RowIndex > -1 && e.ColumnIndex > -1) { DrawCell(e); } else { //二维表头 if (e.RowIndex == -1) { if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列 { //画边框 Graphics g = e.Graphics; e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); int left = e.CellBounds.Left, top = e.CellBounds.Top + 2, right = e.CellBounds.Right, bottom = e.CellBounds.Bottom; switch (SpanRows[e.ColumnIndex].Position) { case 1: left += 2; break; case 2: break; case 3: right -= 2; break; } //画上半部分底色 g.FillRectangle(new SolidBrush(this._mergecolumnheaderbackcolor), left, top, right - left, (bottom - top) / 2); //画中线 g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2, right, (top + bottom) / 2); //写小标题 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(e.Value + "", e.CellStyle.Font, Brushes.Black, new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf); left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2; if (left < 0) left = this.GetCellDisplayRectangle(-1, -1, true).Width; right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2; if (right < 0) right = this.Width; g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black, new Rectangle(left, top, right - left, (bottom - top) / 2), sf); e.Handled = true; } } } base.OnCellPainting(e); } catch { } }