internal void PaintText(Graphics g) { ToolStripRenderer renderer = Renderer; if ((DisplayStyle & ToolStripItemDisplayStyle.Text) == ToolStripItemDisplayStyle.Text) { Font font = Font; Color textColor = ForeColor; if (IsLink) { LinkUtilities.EnsureLinkFonts(font, LinkBehavior, ref linkFont, ref hoverLinkFont); if (Pressed) { font = hoverLinkFont; textColor = ActiveLinkColor; } else if (Selected) { font = hoverLinkFont; textColor = (LinkVisited) ? VisitedLinkColor : LinkColor; } else { font = linkFont; textColor = (LinkVisited) ? VisitedLinkColor : LinkColor; } } Rectangle textRect = InternalLayout.TextRectangle; renderer.DrawItemText(new ToolStripItemTextRenderEventArgs(g, this, Text, textRect, textColor, font, InternalLayout.TextFormat)); } }
internal void PaintText(Graphics g) { ToolStripRenderer renderer = base.Renderer; if ((this.DisplayStyle & ToolStripItemDisplayStyle.Text) == ToolStripItemDisplayStyle.Text) { Font baseFont = this.Font; Color foreColor = this.ForeColor; if (this.IsLink) { LinkUtilities.EnsureLinkFonts(baseFont, this.LinkBehavior, ref this.linkFont, ref this.hoverLinkFont); if (this.Pressed) { baseFont = this.hoverLinkFont; foreColor = this.ActiveLinkColor; } else if (this.Selected) { baseFont = this.hoverLinkFont; foreColor = this.LinkVisited ? this.VisitedLinkColor : this.LinkColor; } else { baseFont = this.linkFont; foreColor = this.LinkVisited ? this.VisitedLinkColor : this.LinkColor; } } Rectangle textRectangle = base.InternalLayout.TextRectangle; renderer.DrawItemText(new ToolStripItemTextRenderEventArgs(g, this, this.Text, textRectangle, foreColor, baseFont, base.InternalLayout.TextFormat)); } }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { // Parameter checking. // One bit and one bit only should be turned on Debug.Assert(paint || computeContentBounds || computeErrorIconBounds); Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds); Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint); Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds); Debug.Assert(cellStyle is not null); if (paint && PaintBorder(paintParts)) { PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle resultBounds = Rectangle.Empty; Rectangle borderWidths = BorderWidths(advancedBorderStyle); Rectangle valBounds = cellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; Point ptCurrentCell = DataGridView.CurrentCellAddress; bool cellCurrent = ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex; bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0; Color brushColor = PaintSelectionBackground(paintParts) && cellSelected ? cellStyle.SelectionBackColor : cellStyle.BackColor; if (paint && PaintBackground(paintParts) && !brushColor.HasTransparency()) { using var brush = brushColor.GetCachedSolidBrushScope(); g.FillRectangle(brush, valBounds); } if (cellStyle.Padding != Padding.Empty) { if (DataGridView.RightToLeftInternal) { 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; } Rectangle errorBounds = valBounds; if (formattedValue is string formattedValueStr && (paint || computeContentBounds)) { // Font independent margins valBounds.Offset(HorizontalTextMarginLeft, VerticalTextMarginTop); valBounds.Width -= HorizontalTextMarginLeft + HorizontalTextMarginRight; valBounds.Height -= VerticalTextMarginTop + VerticalTextMarginBottom; if ((cellStyle.Alignment & AnyBottom) != 0) { valBounds.Height -= VerticalTextMarginBottom; } Font getLinkFont = null; Font getHoverFont = null; LinkUtilities.EnsureLinkFonts(cellStyle.Font, LinkBehavior, ref getLinkFont, ref getHoverFont); TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment( DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); using Font linkFont = getLinkFont; using Font hoverFont = getHoverFont; // Paint the focus rectangle around the link if (!paint) { Debug.Assert(computeContentBounds); resultBounds = DataGridViewUtilities.GetTextBounds( valBounds, formattedValueStr, flags, cellStyle, LinkState == LinkState.Hover ? hoverFont : linkFont); } else { if (valBounds.Width > 0 && valBounds.Height > 0) { if (cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused && PaintFocus(paintParts)) { Rectangle focusBounds = DataGridViewUtilities.GetTextBounds( valBounds, formattedValueStr, flags, cellStyle, LinkState == LinkState.Hover ? hoverFont : linkFont); if ((cellStyle.Alignment & AnyLeft) != 0) { focusBounds.X--; focusBounds.Width++; } else if ((cellStyle.Alignment & AnyRight) != 0) { focusBounds.X++; focusBounds.Width++; } focusBounds.Height += 2; ControlPaint.DrawFocusRectangle(g, focusBounds, Color.Empty, brushColor); } Color linkColor; if ((LinkState & LinkState.Active) == LinkState.Active) { linkColor = ActiveLinkColor; } else if (LinkVisited) { linkColor = VisitedLinkColor; } else { linkColor = LinkColor; } if (PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != 0) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText( g, formattedValueStr, LinkState == LinkState.Hover ? hoverFont : linkFont, valBounds, linkColor, flags); } } else if (cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused && PaintFocus(paintParts) && errorBounds.Width > 0 && errorBounds.Height > 0) { // Draw focus rectangle ControlPaint.DrawFocusRectangle(g, errorBounds, Color.Empty, brushColor); } } linkFont.Dispose(); hoverFont.Dispose(); }
// PaintPrivate is used in three places that need to duplicate the paint code: // 1. DataGridViewCell::Paint method // 2. DataGridViewCell::GetContentBounds // 3. DataGridViewCell::GetErrorIconBounds // // if computeContentBounds is true then PaintPrivate returns the contentBounds // else if computeErrorIconBounds is true then PaintPrivate returns the errorIconBounds // else it returns Rectangle.Empty; private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { // Parameter checking. // One bit and one bit only should be turned on Debug.Assert(paint || computeContentBounds || computeErrorIconBounds); Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds); Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint); Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds); Debug.Assert(cellStyle != null); if (paint && DataGridViewCell.PaintBorder(paintParts)) { PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle resultBounds = Rectangle.Empty; Rectangle borderWidths = BorderWidths(advancedBorderStyle); Rectangle valBounds = cellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; Point ptCurrentCell = this.DataGridView.CurrentCellAddress; bool cellCurrent = ptCurrentCell.X == this.ColumnIndex && ptCurrentCell.Y == rowIndex; bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0; SolidBrush br = this.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected) ? cellStyle.SelectionBackColor : cellStyle.BackColor); if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } if (cellStyle.Padding != Padding.Empty) { if (this.DataGridView.RightToLeftInternal) { 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; } Rectangle errorBounds = valBounds; string formattedValueStr = formattedValue as string; if (formattedValueStr != null && (paint || computeContentBounds)) { // Font independent margins valBounds.Offset(DATAGRIDVIEWLINKCELL_horizontalTextMarginLeft, DATAGRIDVIEWLINKCELL_verticalTextMarginTop); valBounds.Width -= DATAGRIDVIEWLINKCELL_horizontalTextMarginLeft + DATAGRIDVIEWLINKCELL_horizontalTextMarginRight; valBounds.Height -= DATAGRIDVIEWLINKCELL_verticalTextMarginTop + DATAGRIDVIEWLINKCELL_verticalTextMarginBottom; if ((cellStyle.Alignment & anyBottom) != 0) { valBounds.Height -= DATAGRIDVIEWLINKCELL_verticalTextMarginBottom; } Font linkFont = null; Font hoverFont = null; LinkUtilities.EnsureLinkFonts(cellStyle.Font, this.LinkBehavior, ref linkFont, ref hoverFont); TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(this.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); // paint the focus rectangle around the link if (paint) { if (valBounds.Width > 0 && valBounds.Height > 0) { if (cellCurrent && this.DataGridView.ShowFocusCues && this.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts)) { Rectangle focusBounds = DataGridViewUtilities.GetTextBounds(valBounds, formattedValueStr, flags, cellStyle, this.LinkState == LinkState.Hover ? hoverFont : linkFont); if ((cellStyle.Alignment & anyLeft) != 0) { focusBounds.X--; focusBounds.Width++; } else if ((cellStyle.Alignment & anyRight) != 0) { focusBounds.X++; focusBounds.Width++; } focusBounds.Height += 2; ControlPaint.DrawFocusRectangle(g, focusBounds, Color.Empty, br.Color); } Color linkColor; if ((this.LinkState & LinkState.Active) == LinkState.Active) { linkColor = this.ActiveLinkColor; } else if (this.LinkVisited) { linkColor = this.VisitedLinkColor; } else { linkColor = this.LinkColor; } if (DataGridViewCell.PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != 0) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText(g, formattedValueStr, this.LinkState == LinkState.Hover ? hoverFont : linkFont, valBounds, linkColor, flags); } } else if (cellCurrent && this.DataGridView.ShowFocusCues && this.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts) && errorBounds.Width > 0 && errorBounds.Height > 0) { // Draw focus rectangle ControlPaint.DrawFocusRectangle(g, errorBounds, Color.Empty, br.Color); } } else { Debug.Assert(computeContentBounds); resultBounds = DataGridViewUtilities.GetTextBounds(valBounds, formattedValueStr, flags, cellStyle, this.LinkState == LinkState.Hover ? hoverFont : linkFont); } linkFont.Dispose(); hoverFont.Dispose(); } else if (paint || computeContentBounds) { if (cellCurrent && this.DataGridView.ShowFocusCues && this.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts) && paint && valBounds.Width > 0 && valBounds.Height > 0) { // Draw focus rectangle ControlPaint.DrawFocusRectangle(g, valBounds, Color.Empty, br.Color); } } else if (computeErrorIconBounds) { if (!String.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(errorBounds); } } if (this.DataGridView.ShowCellErrors && paint && DataGridViewCell.PaintErrorIcon(paintParts)) { PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, errorBounds, errorText); } return(resultBounds); }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { if (paint && DataGridViewCell.PaintBorder(paintParts)) { this.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle empty = Rectangle.Empty; Rectangle rectangle2 = this.BorderWidths(advancedBorderStyle); Rectangle rect = cellBounds; rect.Offset(rectangle2.X, rectangle2.Y); rect.Width -= rectangle2.Right; rect.Height -= rectangle2.Bottom; Point currentCellAddress = base.DataGridView.CurrentCellAddress; bool flag = (currentCellAddress.X == base.ColumnIndex) && (currentCellAddress.Y == rowIndex); bool flag2 = (cellState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None; SolidBrush cachedBrush = base.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && flag2) ? cellStyle.SelectionBackColor : cellStyle.BackColor); if ((paint && DataGridViewCell.PaintBackground(paintParts)) && (cachedBrush.Color.A == 0xff)) { g.FillRectangle(cachedBrush, rect); } if (cellStyle.Padding != Padding.Empty) { if (base.DataGridView.RightToLeftInternal) { rect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { rect.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } rect.Width -= cellStyle.Padding.Horizontal; rect.Height -= cellStyle.Padding.Vertical; } Rectangle rectangle = rect; string text = formattedValue as string; if ((text != null) && (paint || computeContentBounds)) { rect.Offset(1, 1); rect.Width -= 3; rect.Height -= 2; if ((cellStyle.Alignment & anyBottom) != DataGridViewContentAlignment.NotSet) { rect.Height--; } Font linkFont = null; Font hoverLinkFont = null; LinkUtilities.EnsureLinkFonts(cellStyle.Font, this.LinkBehavior, ref linkFont, ref hoverLinkFont); TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); if (paint) { if ((rect.Width > 0) && (rect.Height > 0)) { Color activeLinkColor; if ((flag && base.DataGridView.ShowFocusCues) && (base.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts))) { Rectangle rectangle5 = DataGridViewUtilities.GetTextBounds(rect, text, flags, cellStyle, (this.LinkState == System.Windows.Forms.LinkState.Hover) ? hoverLinkFont : linkFont); if ((cellStyle.Alignment & anyLeft) != DataGridViewContentAlignment.NotSet) { rectangle5.X--; rectangle5.Width++; } else if ((cellStyle.Alignment & anyRight) != DataGridViewContentAlignment.NotSet) { rectangle5.X++; rectangle5.Width++; } rectangle5.Height += 2; ControlPaint.DrawFocusRectangle(g, rectangle5, Color.Empty, cachedBrush.Color); } if ((this.LinkState & System.Windows.Forms.LinkState.Active) == System.Windows.Forms.LinkState.Active) { activeLinkColor = this.ActiveLinkColor; } else if (this.LinkVisited) { activeLinkColor = this.VisitedLinkColor; } else { activeLinkColor = this.LinkColor; } if (DataGridViewCell.PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != TextFormatFlags.Default) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText(g, text, (this.LinkState == System.Windows.Forms.LinkState.Hover) ? hoverLinkFont : linkFont, rect, activeLinkColor, flags); } } else if (((flag && base.DataGridView.ShowFocusCues) && (base.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts))) && ((rectangle.Width > 0) && (rectangle.Height > 0))) { ControlPaint.DrawFocusRectangle(g, rectangle, Color.Empty, cachedBrush.Color); } } else { empty = DataGridViewUtilities.GetTextBounds(rect, text, flags, cellStyle, (this.LinkState == System.Windows.Forms.LinkState.Hover) ? hoverLinkFont : linkFont); } linkFont.Dispose(); hoverLinkFont.Dispose(); } else if (paint || computeContentBounds) { if (((flag && base.DataGridView.ShowFocusCues) && (base.DataGridView.Focused && DataGridViewCell.PaintFocus(paintParts))) && ((paint && (rect.Width > 0)) && (rect.Height > 0))) { ControlPaint.DrawFocusRectangle(g, rect, Color.Empty, cachedBrush.Color); } } else if (computeErrorIconBounds && !string.IsNullOrEmpty(errorText)) { empty = base.ComputeErrorIconBounds(rectangle); } if ((base.DataGridView.ShowCellErrors && paint) && DataGridViewCell.PaintErrorIcon(paintParts)) { base.PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, rectangle, errorText); } return(empty); }