private void DrawImage(Graphics g) { var image = Enabled ? ImageEnabled : (ImageDisabled ?? ImageEnabled); ImageAttributes imageAttr = null; if (null == image) { return; } if (m_monochrom) { imageAttr = new ImageAttributes(); // transform the monochrom image // white -> BackColor // black -> ForeColor var colorMap = new ColorMap[2]; colorMap[0] = new ColorMap { OldColor = Color.White, NewColor = BackColor }; colorMap[1] = new ColorMap { OldColor = Color.Black, NewColor = ForeColor }; imageAttr.SetRemapTable(colorMap); } Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); if (!Enabled && (null == ImageDisabled)) { using (Bitmap bitmapMono = new Bitmap(image, ClientRectangle.Size)) { if (imageAttr != null) { using (Graphics gMono = Graphics.FromImage(bitmapMono)) { gMono.DrawImage(image, new[] { new Point(0, 0), new Point(image.Width - 1, 0), new Point(0, image.Height - 1) }, rect, GraphicsUnit.Pixel, imageAttr); } } ControlPaint.DrawImageDisabled(g, bitmapMono, 0, 0, BackColor); } } else { // Three points provided are upper-left, upper-right and // lower-left of the destination parallelogram. Point[] pts = new Point[3]; pts[0].X = Enabled && m_mouseOver && m_mouseCapture ? 1 : 0; pts[0].Y = Enabled && m_mouseOver && m_mouseCapture ? 1 : 0; pts[1].X = pts[0].X + ClientRectangle.Width; pts[1].Y = pts[0].Y; pts[2].X = pts[0].X; pts[2].Y = pts[1].Y + ClientRectangle.Height; if (imageAttr == null) { g.DrawImage(image, pts, rect, GraphicsUnit.Pixel); } else { g.DrawImage(image, pts, rect, GraphicsUnit.Pixel, imageAttr); } } }
// 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 elementState, 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 valBounds = cellBounds; Rectangle borderWidths = BorderWidths(advancedBorderStyle); valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; if (valBounds.Width > 0 && valBounds.Height > 0 && (paint || computeContentBounds)) { Rectangle imgBounds = valBounds; if (cellStyle.Padding != Padding.Empty) { if (DataGridView.RightToLeftInternal) { imgBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top); } else { imgBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top); } imgBounds.Width -= cellStyle.Padding.Horizontal; imgBounds.Height -= cellStyle.Padding.Vertical; } bool cellSelected = (elementState & DataGridViewElementStates.Selected) != 0; SolidBrush br = DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected) ? cellStyle.SelectionBackColor : cellStyle.BackColor); if (imgBounds.Width > 0 && imgBounds.Height > 0) { Image img = formattedValue as Image; Icon ico = null; if (img == null) { ico = formattedValue as Icon; } if (ico != null || img != null) { DataGridViewImageCellLayout imageLayout = ImageLayout; if (imageLayout == DataGridViewImageCellLayout.NotSet) { if (OwningColumn is DataGridViewImageColumn) { imageLayout = ((DataGridViewImageColumn)OwningColumn).ImageLayout; Debug.Assert(imageLayout != DataGridViewImageCellLayout.NotSet); } else { imageLayout = DataGridViewImageCellLayout.Normal; } } if (imageLayout == DataGridViewImageCellLayout.Stretch) { if (paint) { if (DataGridViewCell.PaintBackground(paintParts)) { DataGridViewCell.PaintPadding(g, valBounds, cellStyle, br, DataGridView.RightToLeftInternal); } if (DataGridViewCell.PaintContentForeground(paintParts)) { if (img != null) { // ImageAttributes attr = new ImageAttributes(); attr.SetWrapMode(WrapMode.TileFlipXY); g.DrawImage(img, imgBounds, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attr); attr.Dispose(); } else { g.DrawIcon(ico, imgBounds); } } } resultBounds = imgBounds; } else { Rectangle imgBounds2 = ImgBounds(imgBounds, (img == null) ? ico.Width : img.Width, (img == null) ? ico.Height : img.Height, imageLayout, cellStyle); resultBounds = imgBounds2; if (paint) { if (DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } if (DataGridViewCell.PaintContentForeground(paintParts)) { //paint the image Region reg = g.Clip; g.SetClip(Rectangle.Intersect(Rectangle.Intersect(imgBounds2, imgBounds), Rectangle.Truncate(g.VisibleClipBounds))); if (img != null) { g.DrawImage(img, imgBounds2); } else { g.DrawIconUnstretched(ico, imgBounds2); } g.Clip = reg; } } } } else { if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } resultBounds = Rectangle.Empty; } } else { if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255) { g.FillRectangle(br, valBounds); } resultBounds = Rectangle.Empty; } Point ptCurrentCell = DataGridView.CurrentCellAddress; if (paint && DataGridViewCell.PaintFocus(paintParts) && ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex && DataGridView.ShowFocusCues && DataGridView.Focused) { // Draw focus rectangle ControlPaint.DrawFocusRectangle(g, valBounds, Color.Empty, br.Color); } if (DataGridView.ShowCellErrors && paint && DataGridViewCell.PaintErrorIcon(paintParts)) { PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, valBounds, errorText); } } else if (computeErrorIconBounds) { if (!string.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(valBounds); } else { resultBounds = Rectangle.Empty; } } else { Debug.Assert(valBounds.Height <= 0 || valBounds.Width <= 0); resultBounds = Rectangle.Empty; } return(resultBounds); }
protected internal override void uwfOnLatePaint(PaintEventArgs e) { ControlPaint.PrintBorder(e.Graphics, ClientRectangle, BorderStyle, Border3DStyle.Flat); }
/// <summary> /// Draws a border for the ToolTip similar to the default border. /// </summary> public void DrawBorder() { ControlPaint.DrawBorder(Graphics, Bounds, SystemColors.WindowFrame, ButtonBorderStyle.Solid); }
/// <include file='doc\ToolStripRenderer.uex' path='docs/doc[@for="ToolStripRenderer.OnRenderMenuItem"]/*' /> /// <devdoc> /// Draw the items background /// </devdoc> protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) { ToolStripMenuItem item = e.Item as ToolStripMenuItem; Graphics g = e.Graphics; if (item is MdiControlStrip.SystemMenuItem) { return; // no highlights are painted behind a system menu item } // if (item != null) { Rectangle bounds = new Rectangle(Point.Empty, item.Size); if (item.IsTopLevel && !ToolStripManager.VisualStylesEnabled) { // CLASSIC MODE (3D edges) // Draw box highlight for toplevel items in downlevel platforms if (item.BackgroundImage != null) { ControlPaint.DrawBackgroundImage(g, item.BackgroundImage, item.BackColor, item.BackgroundImageLayout, item.ContentRectangle, item.ContentRectangle); } else if (item.RawBackColor != Color.Empty) { FillBackground(g, item.ContentRectangle, item.BackColor); } // Toplevel menu items do 3D borders. ToolBarState state = GetToolBarState(item); RenderSmall3DBorderInternal(g, bounds, state, (item.RightToLeft == RightToLeft.Yes)); } else { // XP++ MODE (no 3D edges) // Draw blue filled highlight for toplevel items in themed platforms // or items parented to a drop down Rectangle fillRect = new Rectangle(Point.Empty, item.Size); if (item.IsOnDropDown) { // Scoot in by 2 pixels when selected fillRect.X += 2; fillRect.Width -= 3; //its already 1 away from the right edge } if (item.Selected || item.Pressed) { // Legacy behavior is to always paint the menu item background. // The correct behavior is to only paint the background if the menu item is // enabled. if (!AccessibilityImprovements.Level1 || item.Enabled) { g.FillRectangle(SystemBrushes.Highlight, fillRect); } if (AccessibilityImprovements.Level1) { Color borderColor = ToolStripManager.VisualStylesEnabled ? SystemColors.Highlight : ProfessionalColors.MenuItemBorder; // draw selection border - always drawn regardless of Enabled. using (Pen p = new Pen(borderColor)) { g.DrawRectangle(p, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1); } } } else { if (item.BackgroundImage != null) { ControlPaint.DrawBackgroundImage(g, item.BackgroundImage, item.BackColor, item.BackgroundImageLayout, item.ContentRectangle, fillRect); } else if (!ToolStripManager.VisualStylesEnabled && (item.RawBackColor != Color.Empty)) { FillBackground(g, fillRect, item.BackColor); } } } } }
public static void DrawGroupBox(Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state) { Size font_size = TextRenderer.MeasureText(groupBoxText, font); if (Application.RenderWithVisualStyles || always_use_visual_styles == true) { VisualStyleRenderer vsr; Rectangle new_bounds; switch (state) { case GroupBoxState.Normal: default: vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal); new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 1, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 1); break; case GroupBoxState.Disabled: vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Disabled); new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 2, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 2); break; } if (groupBoxText == String.Empty) { vsr.DrawBackground(g, bounds); } else { vsr.DrawBackgroundExcludingArea(g, new_bounds, new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height)); } if (textColor == Color.Empty) { textColor = vsr.GetColor(ColorProperty.TextColor); } if (groupBoxText != String.Empty) { TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 8, bounds.Top), textColor, flags); } } else { // MS has a pretty big bug when rendering the non-visual styles group box. Instead of using the height // part of the bounds as height, they use it as the bottom, so the boxes are drawn in completely different // places. Rather than emulate this bug, we do it correctly. After googling for a while, I don't think // anyone has ever actually used this class for anything, so it should be fine. :) Rectangle new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2), bounds.Width, bounds.Height - (int)(font_size.Height / 2)); // Don't paint over the background where we are going to put the text Region old_clip = g.Clip; g.SetClip(new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude); ControlPaint.DrawBorder3D(g, new_bounds, Border3DStyle.Etched); g.Clip = old_clip; if (groupBoxText != String.Empty) { if (textColor == Color.Empty) { textColor = state == GroupBoxState.Normal ? SystemColors.ControlText : SystemColors.GrayText; } TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 8, bounds.Top), textColor, flags); } } }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint) { Rectangle empty; Point currentCellAddress = base.DataGridView.CurrentCellAddress; bool flag = (elementState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None; bool flag2 = (currentCellAddress.X == base.ColumnIndex) && (currentCellAddress.Y == rowIndex); string text = formattedValue as string; SolidBrush cachedBrush = base.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && flag) ? cellStyle.SelectionBackColor : cellStyle.BackColor); SolidBrush brush2 = base.DataGridView.GetCachedBrush(flag ? cellStyle.SelectionForeColor : cellStyle.ForeColor); if (paint && DataGridViewCell.PaintBorder(paintParts)) { this.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle rect = cellBounds; Rectangle rectangle3 = this.BorderWidths(advancedBorderStyle); rect.Offset(rectangle3.X, rectangle3.Y); rect.Width -= rectangle3.Right; rect.Height -= rectangle3.Bottom; if ((rect.Height <= 0) || (rect.Width <= 0)) { return(Rectangle.Empty); } 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 cellValueBounds = rect; if (((rect.Height <= 0) || (rect.Width <= 0)) || (!paint && !computeContentBounds)) { if (computeErrorIconBounds) { if (!string.IsNullOrEmpty(errorText)) { empty = base.ComputeErrorIconBounds(cellValueBounds); } else { empty = Rectangle.Empty; } } else { empty = Rectangle.Empty; } goto Label_06AD; } if ((this.FlatStyle == System.Windows.Forms.FlatStyle.Standard) || (this.FlatStyle == System.Windows.Forms.FlatStyle.System)) { if (base.DataGridView.ApplyVisualStylesToInnerCells) { if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { PushButtonState normal = PushButtonState.Normal; if ((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) { normal = PushButtonState.Pressed; } else if (((base.DataGridView.MouseEnteredCellAddress.Y == rowIndex) && (base.DataGridView.MouseEnteredCellAddress.X == base.ColumnIndex)) && mouseInContentBounds) { normal = PushButtonState.Hot; } if ((DataGridViewCell.PaintFocus(paintParts) && flag2) && (base.DataGridView.ShowFocusCues && base.DataGridView.Focused)) { normal |= PushButtonState.Default; } DataGridViewButtonCellRenderer.DrawButton(g, rect, (int)normal); } empty = rect; rect = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetBackgroundContentRectangle(g, rect); } else { if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { ControlPaint.DrawBorder(g, rect, SystemColors.Control, (this.ButtonState == System.Windows.Forms.ButtonState.Normal) ? ButtonBorderStyle.Outset : ButtonBorderStyle.Inset); } empty = rect; rect.Inflate(-SystemInformation.Border3DSize.Width, -SystemInformation.Border3DSize.Height); } goto Label_06AD; } if (this.FlatStyle != System.Windows.Forms.FlatStyle.Flat) { rect.Inflate(-1, -1); if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { if ((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) { ButtonBaseAdapter.ColorData data2 = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, rect, data2.options.highContrast ? data2.windowText : data2.windowFrame, true); ControlPaint.DrawBorder(g, rect, data2.options.highContrast ? data2.windowText : data2.buttonShadow, ButtonBorderStyle.Solid); } else if (((base.DataGridView.MouseEnteredCellAddress.Y == rowIndex) && (base.DataGridView.MouseEnteredCellAddress.X == base.ColumnIndex)) && mouseInContentBounds) { ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, rect, colors.options.highContrast ? colors.windowText : colors.buttonShadow, false); ButtonBaseAdapter.Draw3DLiteBorder(g, rect, colors, true); } else { ButtonBaseAdapter.ColorData data4 = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, rect, data4.options.highContrast ? data4.windowText : data4.buttonShadow, false); ButtonBaseAdapter.DrawFlatBorder(g, rect, data4.options.highContrast ? data4.windowText : data4.buttonShadow); } } empty = rect; goto Label_06AD; } rect.Inflate(-1, -1); if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { ButtonBaseAdapter.DrawDefaultBorder(g, rect, brush2.Color, true); if (cachedBrush.Color.A == 0xff) { if ((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) { ButtonBaseAdapter.ColorData data = ButtonBaseAdapter.PaintFlatRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); IntPtr hdc = g.GetHdc(); try { using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc)) { WindowsBrush brush3; if (data.options.highContrast) { brush3 = new WindowsSolidBrush(graphics.DeviceContext, data.buttonShadow); } else { brush3 = new WindowsSolidBrush(graphics.DeviceContext, data.lowHighlight); } try { ButtonBaseAdapter.PaintButtonBackground(graphics, rect, brush3); } finally { brush3.Dispose(); } } goto Label_04CF; } finally { g.ReleaseHdc(); } } if (((base.DataGridView.MouseEnteredCellAddress.Y == rowIndex) && (base.DataGridView.MouseEnteredCellAddress.X == base.ColumnIndex)) && mouseInContentBounds) { IntPtr hDc = g.GetHdc(); try { using (WindowsGraphics graphics2 = WindowsGraphics.FromHdc(hDc)) { Color controlDark = SystemColors.ControlDark; using (WindowsBrush brush4 = new WindowsSolidBrush(graphics2.DeviceContext, controlDark)) { ButtonBaseAdapter.PaintButtonBackground(graphics2, rect, brush4); } } } finally { g.ReleaseHdc(); } } } } Label_04CF: empty = rect; Label_06AD: if (((paint && DataGridViewCell.PaintFocus(paintParts)) && (flag2 && base.DataGridView.ShowFocusCues)) && ((base.DataGridView.Focused && (rect.Width > ((2 * SystemInformation.Border3DSize.Width) + 1))) && (rect.Height > ((2 * SystemInformation.Border3DSize.Height) + 1)))) { if ((this.FlatStyle == System.Windows.Forms.FlatStyle.System) || (this.FlatStyle == System.Windows.Forms.FlatStyle.Standard)) { ControlPaint.DrawFocusRectangle(g, Rectangle.Inflate(rect, -1, -1), Color.Empty, SystemColors.Control); } else if (this.FlatStyle == System.Windows.Forms.FlatStyle.Flat) { if (((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) || ((base.DataGridView.CurrentCellAddress.Y == rowIndex) && (base.DataGridView.CurrentCellAddress.X == base.ColumnIndex))) { ButtonBaseAdapter.ColorData data5 = ButtonBaseAdapter.PaintFlatRender(g, cellStyle.ForeColor, cellStyle.BackColor, base.DataGridView.Enabled).Calculate(); string str2 = (text != null) ? text : string.Empty; ButtonBaseAdapter.LayoutOptions options = ButtonFlatAdapter.PaintFlatLayout(g, true, SystemInformation.HighContrast, 1, rect, Padding.Empty, false, cellStyle.Font, str2, base.DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), base.DataGridView.RightToLeft); options.everettButtonCompat = false; ButtonBaseAdapter.LayoutData data6 = options.Layout(); ButtonBaseAdapter.DrawFlatFocus(g, data6.focus, data5.options.highContrast ? data5.windowText : data5.constrastButtonShadow); } } else if (((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) || ((base.DataGridView.CurrentCellAddress.Y == rowIndex) && (base.DataGridView.CurrentCellAddress.X == base.ColumnIndex))) { bool up = this.ButtonState == System.Windows.Forms.ButtonState.Normal; string str3 = (text != null) ? text : string.Empty; ButtonBaseAdapter.LayoutOptions options2 = ButtonPopupAdapter.PaintPopupLayout(g, up, SystemInformation.HighContrast ? 2 : 1, rect, Padding.Empty, false, cellStyle.Font, str3, base.DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), base.DataGridView.RightToLeft); options2.everettButtonCompat = false; ButtonBaseAdapter.LayoutData data7 = options2.Layout(); ControlPaint.DrawFocusRectangle(g, data7.focus, cellStyle.ForeColor, cellStyle.BackColor); } } if (((text != null) && paint) && DataGridViewCell.PaintContentForeground(paintParts)) { rect.Offset(2, 1); rect.Width -= 4; rect.Height -= 2; if ((((this.ButtonState & (System.Windows.Forms.ButtonState.Checked | System.Windows.Forms.ButtonState.Pushed)) != System.Windows.Forms.ButtonState.Normal) && (this.FlatStyle != System.Windows.Forms.FlatStyle.Flat)) && (this.FlatStyle != System.Windows.Forms.FlatStyle.Popup)) { rect.Offset(1, 1); rect.Width--; rect.Height--; } if ((rect.Width > 0) && (rect.Height > 0)) { Color color; if (base.DataGridView.ApplyVisualStylesToInnerCells && ((this.FlatStyle == System.Windows.Forms.FlatStyle.System) || (this.FlatStyle == System.Windows.Forms.FlatStyle.Standard))) { color = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetColor(ColorProperty.TextColor); } else { color = brush2.Color; } TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); TextRenderer.DrawText(g, text, cellStyle.Font, rect, color, flags); } } if ((base.DataGridView.ShowCellErrors && paint) && DataGridViewCell.PaintErrorIcon(paintParts)) { base.PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, cellValueBounds, errorText); } return(empty); }
/// <devdoc> /// Called by the dataGrid when it needs the caption /// to repaint. /// </devdoc> internal void Paint(Graphics g, Rectangle bounds, bool alignRight) { Size textSize = new Size((int)g.MeasureString(text, this.Font).Width + 2, this.Font.Height + 2); downButtonRect = GetDetailsButtonRect(bounds, alignRight); int downButtonWidth = GetDetailsButtonWidth(); backButtonRect = GetBackButtonRect(bounds, alignRight, downButtonWidth); int backButtonArea = backButtonVisible ? backButtonRect.Width + xOffset + buttonToText : 0; int downButtonArea = downButtonVisible && !dataGrid.ParentRowsIsEmpty() ? downButtonWidth + xOffset + buttonToText : 0; int textWidthLeft = bounds.Width - xOffset - backButtonArea - downButtonArea; textRect = new Rectangle( bounds.X, bounds.Y + yOffset, Math.Min(textWidthLeft, 2 * textPadding + textSize.Width), 2 * textPadding + textSize.Height); // align the caption text box, downButton, and backButton // if the RigthToLeft property is set to true if (alignRight) { textRect.X = bounds.Right - textRect.Width; backButtonRect.X = bounds.X + xOffset * 4 + downButtonWidth; downButtonRect.X = bounds.X + xOffset * 2; } Debug.WriteLineIf(CompModSwitches.DGCaptionPaint.TraceVerbose, "text size = " + textSize.ToString()); Debug.WriteLineIf(CompModSwitches.DGCaptionPaint.TraceVerbose, "downButtonWidth = " + downButtonWidth.ToString(CultureInfo.InvariantCulture)); Debug.WriteLineIf(CompModSwitches.DGCaptionPaint.TraceVerbose, "textWidthLeft = " + textWidthLeft.ToString(CultureInfo.InvariantCulture)); Debug.WriteLineIf(CompModSwitches.DGCaptionPaint.TraceVerbose, "backButtonRect " + backButtonRect.ToString()); Debug.WriteLineIf(CompModSwitches.DGCaptionPaint.TraceVerbose, "textRect " + textRect.ToString()); Debug.WriteLineIf(CompModSwitches.DGCaptionPaint.TraceVerbose, "downButtonRect " + downButtonRect.ToString()); // we should use the code that is commented out // with today's code, there are pixels on the backButtonRect and the downButtonRect // that are getting painted twice // g.FillRectangle(backBrush, bounds); if (backButtonVisible) { PaintBackButton(g, backButtonRect, alignRight); if (backActive) { if (lastMouseLocation == CaptionLocation.BackButton) { backButtonRect.Inflate(1, 1); ControlPaint.DrawBorder3D(g, backButtonRect, backPressed ? Border3DStyle.SunkenInner : Border3DStyle.RaisedInner); } } } PaintText(g, textRect, alignRight); if (downButtonVisible && !dataGrid.ParentRowsIsEmpty()) { PaintDownButton(g, downButtonRect); // the rules have changed, yet again. // now: if we show the parent rows and the mouse is // not on top of this icon, then let the icon be depressed. // if the mouse is pressed over the icon, then show the icon pressed // if the mouse is over the icon and not pressed, then show the icon SunkenInner; // if (lastMouseLocation == CaptionLocation.DownButton) { downButtonRect.Inflate(1, 1); ControlPaint.DrawBorder3D(g, downButtonRect, downPressed ? Border3DStyle.SunkenInner : Border3DStyle.RaisedInner); } } }
public static void DrawCheckBox(Graphics g, Point glyphLocation, Rectangle textBounds, string checkBoxText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, CheckBoxState state) { Rectangle bounds = new Rectangle(glyphLocation, GetGlyphSize(g, state)); if (Application.RenderWithVisualStyles || always_use_visual_styles == true) { VisualStyleRenderer vsr = GetCheckBoxRenderer(state); vsr.DrawBackground(g, bounds); if (image != null) { vsr.DrawImage(g, imageBounds, image); } if (focused) { ControlPaint.DrawFocusRectangle(g, textBounds); } if (checkBoxText != String.Empty) { if (state == CheckBoxState.CheckedDisabled || state == CheckBoxState.MixedDisabled || state == CheckBoxState.UncheckedDisabled) { TextRenderer.DrawText(g, checkBoxText, font, textBounds, SystemColors.GrayText, flags); } else { TextRenderer.DrawText(g, checkBoxText, font, textBounds, SystemColors.ControlText, flags); } } } else { switch (state) { case CheckBoxState.CheckedDisabled: case CheckBoxState.MixedDisabled: case CheckBoxState.MixedPressed: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Inactive | ButtonState.Checked); break; case CheckBoxState.CheckedHot: case CheckBoxState.CheckedNormal: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Checked); break; case CheckBoxState.CheckedPressed: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Pushed | ButtonState.Checked); break; case CheckBoxState.MixedHot: case CheckBoxState.MixedNormal: ControlPaint.DrawMixedCheckBox(g, bounds, ButtonState.Checked); break; case CheckBoxState.UncheckedDisabled: case CheckBoxState.UncheckedPressed: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Inactive); break; case CheckBoxState.UncheckedHot: case CheckBoxState.UncheckedNormal: ControlPaint.DrawCheckBox(g, bounds, ButtonState.Normal); break; } if (image != null) { g.DrawImage(image, imageBounds); } if (focused) { ControlPaint.DrawFocusRectangle(g, textBounds); } if (checkBoxText != String.Empty) { TextRenderer.DrawText(g, checkBoxText, font, textBounds, SystemColors.ControlText, flags); } } }
private void DrawGroupBox(PaintEventArgs e) { Graphics graphics = e.Graphics; Rectangle textRectangle = ClientRectangle; // Max text bounding box passed to drawing methods to support RTL. int textOffset = 8; // Offset from the left bound. Color backColor = DisabledColor; Pen light = new Pen(ControlPaint.Light(backColor, 1.0f)); Pen dark = new Pen(ControlPaint.Dark(backColor, 0f)); Size textSize; textRectangle.X += textOffset; textRectangle.Width -= 2 * textOffset; try { if (UseCompatibleTextRendering) { using (Brush textBrush = new SolidBrush(ForeColor)){ using (StringFormat format = new StringFormat()){ format.HotkeyPrefix = ShowKeyboardCues ? System.Drawing.Text.HotkeyPrefix.Show : System.Drawing.Text.HotkeyPrefix.Hide; // Adjust string format for Rtl controls if (RightToLeft == RightToLeft.Yes) { format.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } textSize = Size.Ceiling(graphics.MeasureString(Text, Font, textRectangle.Width, format)); if (Enabled) { graphics.DrawString(Text, Font, textBrush, textRectangle, format); } else { ControlPaint.DrawStringDisabled(graphics, Text, Font, backColor, textRectangle, format); } } } } else { using (WindowsGraphics wg = WindowsGraphics.FromGraphics(graphics)){ IntTextFormatFlags flags = IntTextFormatFlags.WordBreak | IntTextFormatFlags.TextBoxControl; if (!ShowKeyboardCues) { flags |= IntTextFormatFlags.HidePrefix; } if (RightToLeft == RightToLeft.Yes) { flags |= IntTextFormatFlags.RightToLeft; flags |= IntTextFormatFlags.Right; } using (WindowsFont wfont = WindowsGraphicsCacheManager.GetWindowsFont(this.Font)) { textSize = wg.MeasureText(Text, wfont, new Size(textRectangle.Width, int.MaxValue), flags); if (Enabled) { wg.DrawText(Text, wfont, textRectangle, ForeColor, flags); } else { ControlPaint.DrawStringDisabled(wg, Text, Font, backColor, textRectangle, ((TextFormatFlags)flags)); } } } } int textLeft = textOffset; // Left side of binding box (independent on RTL). if (RightToLeft == RightToLeft.Yes) { textLeft += textRectangle.Width - textSize.Width; } // Math.Min to assure we paint at least a small line. int textRight = Math.Min(textLeft + textSize.Width, Width - 6); int boxTop = FontHeight / 2; if (SystemInformation.HighContrast && AccessibilityImprovements.Level1) { Color boxColor; if (Enabled) { boxColor = ForeColor; } else { boxColor = SystemColors.GrayText; } bool needToDispose = !boxColor.IsSystemColor; Pen boxPen = null; try { if (needToDispose) { boxPen = new Pen(boxColor); } else { boxPen = SystemPens.FromSystemColor(boxColor); } // left graphics.DrawLine(boxPen, 0, boxTop, 0, Height); //bottom graphics.DrawLine(boxPen, 0, Height - 1, Width, Height - 1); //top-left graphics.DrawLine(boxPen, 0, boxTop, textLeft, boxTop); //top-right graphics.DrawLine(boxPen, textRight, boxTop, Width - 1, boxTop); //right graphics.DrawLine(boxPen, Width - 1, boxTop, Width - 1, Height - 1); } finally { if (needToDispose && boxPen != null) { boxPen.Dispose(); } } } else { // left graphics.DrawLine(light, 1, boxTop, 1, Height - 1); graphics.DrawLine(dark, 0, boxTop, 0, Height - 2); // bottom graphics.DrawLine(light, 0, Height - 1, Width, Height - 1); graphics.DrawLine(dark, 0, Height - 2, Width - 1, Height - 2); // top-left graphics.DrawLine(dark, 0, boxTop - 1, textLeft, boxTop - 1); graphics.DrawLine(light, 1, boxTop, textLeft, boxTop); // top-right graphics.DrawLine(dark, textRight, boxTop - 1, Width - 2, boxTop - 1); graphics.DrawLine(light, textRight, boxTop, Width - 1, boxTop); // right graphics.DrawLine(light, Width - 1, boxTop - 1, Width - 1, Height - 1); graphics.DrawLine(dark, Width - 2, boxTop, Width - 2, Height - 2); } } finally { light.Dispose(); dark.Dispose(); } }
// Draw the button in its current state on a Graphics surface. internal override void Draw(Graphics graphics) { // calculate ButtonState needed by DrawRadioButton ButtonState state = CalculateState(); StringFormat format = GetStringFormat(); if (needsLayout) { LayoutElements(); } if (appearance == Appearance.Button) { ThemeManager.MainPainter.DrawButton (graphics, 0, 0, content.Width, content.Height, state, ForeColor, BackColor, false); } else { using (Brush bg = CreateBackgroundBrush()) { ThemeManager.MainPainter.DrawRadioButton (graphics, checkX, checkY, checkSize, checkSize, state, ForeColor, BackColor, bg); } } // TODO: image drawing Rectangle rect = content; if (appearance == Appearance.Button) { int x = content.X; int y = content.Y; int width = content.Width; int height = content.Height; x += 2; y += 2; width -= 4; height -= 4; if ((state & ButtonState.Pushed) != 0) { ++x; ++x; } rect = new Rectangle(x, y, width, height); } RectangleF layout = rect; Font font = Font; if ((TextAlign & (ContentAlignment.MiddleLeft | ContentAlignment.MiddleCenter | ContentAlignment.MiddleRight)) != 0) { layout.Offset(0.0f, GetDescent(graphics, font) / 2.0f); } if (Enabled) { using (Brush brush = new SolidBrush(ForeColor)) { graphics.DrawString(Text, font, brush, layout, format); } } else { ControlPaint.DrawStringDisabled(graphics, Text, font, BackColor, layout, format); } }
/// <summary> /// Paints the control. /// </summary> /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param> protected virtual void PaintControl(PaintEventArgs e) { var cbi = NativeMethods.COMBOBOXINFO.FromComboBox(this); var itemText = SelectedIndex >= 0 ? GetItemText(SelectedItem) : string.Empty; var state = Enabled ? currentState : ComboBoxState.Disabled; Rectangle tr = cbi.rcItem; /*Rectangle tr = this.ClientRectangle; * tr.Width -= (SystemInformation.VerticalScrollBarWidth + 2); * tr.Inflate(0, -2); * tr.Offset(1, 0);*/ Rectangle br = cbi.rcButton; var vsSuccess = false; if (VisualStyleRenderer.IsSupported && Application.RenderWithVisualStyles) { /*Rectangle r = Rectangle.Inflate(this.ClientRectangle, 1, 1); * if (this.DropDownStyle != ComboBoxStyle.DropDownList) * { * e.Graphics.Clear(this.BackColor); * ComboBoxRenderer.DrawTextBox(e.Graphics, r, itemText, this.Font, tr, tff, state); * ComboBoxRenderer.DrawDropDownButton(e.Graphics, br, state); * } * else*/ { try { var vr = new VisualStyleRenderer("Combobox", DropDownStyle == ComboBoxStyle.DropDownList ? 5 : 4, (int)state); vr.DrawParentBackground(e.Graphics, ClientRectangle, this); vr.DrawBackground(e.Graphics, ClientRectangle); if (DropDownStyle != ComboBoxStyle.DropDownList) { br.Inflate(1, 1); } var cr = DropDownStyle == ComboBoxStyle.DropDownList ? Rectangle.Inflate(br, -1, -1) : br; vr.SetParameters("Combobox", 7, (int)(br.Contains(PointToClient(Cursor.Position)) ? state : ComboBoxState.Normal)); vr.DrawBackground(e.Graphics, br, cr); if (Focused && State != ComboBoxState.Pressed) { var sz = TextRenderer.MeasureText(e.Graphics, "Wg", Font, tr.Size, TextFormatFlags.Default); var fr = Rectangle.Inflate(tr, 0, (sz.Height - tr.Height) / 2 + 1); ControlPaint.DrawFocusRectangle(e.Graphics, fr); } var fgc = Enabled ? ForeColor : SystemColors.GrayText; TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff); vsSuccess = true; } catch { } } } if (!vsSuccess) { Diagnostics.Debug.WriteLine($"CR:{ClientRectangle};ClR:{e.ClipRectangle};Foc:{Focused};St:{state};Tx:{itemText}"); var focusedNotPressed = Focused && state != ComboBoxState.Pressed; var bgc = Enabled ? (focusedNotPressed ? SystemColors.Highlight : BackColor) : SystemColors.Control; var fgc = Enabled ? (focusedNotPressed ? SystemColors.HighlightText : ForeColor) : SystemColors.GrayText; e.Graphics.Clear(bgc); ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, Border3DStyle.Sunken); ControlPaint.DrawComboButton(e.Graphics, br, Enabled ? (state == ComboBoxState.Pressed ? ButtonState.Pushed : ButtonState.Normal) : ButtonState.Inactive); tr = new Rectangle(tr.X + 3, tr.Y + 1, tr.Width - 4, tr.Height - 2); TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff); if (focusedNotPressed) { var fr = Rectangle.Inflate(cbi.rcItem, -1, -1); fr.Height++; fr.Width++; ControlPaint.DrawFocusRectangle(e.Graphics, fr, fgc, bgc); e.Graphics.DrawRectangle(SystemPens.Window, cbi.rcItem); } } }
static X11DesktopColors() { FindDesktopEnvironment(); switch (desktop) { case Desktop.Gtk: { //IntPtr dispmgr; //IntPtr gdkdisplay; IntPtr widget; IntPtr style_ptr; GtkStyleStruct style; try { GtkInit(); //dispmgr = gdk_display_manager_get (); //gdkdisplay = gdk_display_manager_get_default_display (dispmgr); widget = gtk_invisible_new(); gtk_widget_ensure_style(widget); style_ptr = gtk_widget_get_style(widget); style = (GtkStyleStruct)Marshal.PtrToStructure(style_ptr, typeof(GtkStyleStruct)); ThemeEngine.Current.ColorControl = ColorFromGdkColor(style.bg[0]); ThemeEngine.Current.ColorControlText = ColorFromGdkColor(style.fg[0]); ThemeEngine.Current.ColorControlDark = ColorFromGdkColor(style.dark[0]); ThemeEngine.Current.ColorControlLight = ColorFromGdkColor(style.light[0]); ThemeEngine.Current.ColorControlLightLight = ControlPaint.Light(ColorFromGdkColor(style.light[0])); ThemeEngine.Current.ColorControlDarkDark = ControlPaint.Dark(ColorFromGdkColor(style.dark[0])); // We don't want ControlLight and ControlLightLight to disappear on a white background! Color white = Color.FromArgb(255, 255, 255, 255); if (ThemeEngine.Current.ColorControlLight.ToArgb() == white.ToArgb()) { ThemeEngine.Current.ColorControlLight = Color.FromArgb(255, 190, 190, 190); } if (ThemeEngine.Current.ColorControlLightLight.ToArgb() == white.ToArgb()) { ThemeEngine.Current.ColorControlLightLight = Color.FromArgb(255, 220, 220, 220); } widget = gtk_menu_new(); gtk_widget_ensure_style(widget); style_ptr = gtk_widget_get_style(widget); style = (GtkStyleStruct)Marshal.PtrToStructure(style_ptr, typeof(GtkStyleStruct)); ThemeEngine.Current.ColorMenu = ColorFromGdkColor(style.bg [0]); ThemeEngine.Current.ColorMenuText = ColorFromGdkColor(style.text [0]); } catch (DllNotFoundException) { Console.Error.WriteLine("Gtk not found (missing LD_LIBRARY_PATH to libgtk-x11-2.0.so.0?), using built-in colorscheme"); } catch { Console.Error.WriteLine("Gtk colorscheme read failure, using built-in colorscheme"); } break; } case Desktop.KDE: { if (!ReadKDEColorsheme()) { Console.Error.WriteLine("KDE colorscheme read failure, using built-in colorscheme"); } break; } default: { break; } } }
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 && 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(); }
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); }
private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, 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); Point ptCurrentCell = DataGridView.CurrentCellAddress; bool cellSelected = (elementState & DataGridViewElementStates.Selected) != 0; bool cellCurrent = (ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex); Rectangle resultBounds; string formattedString = formattedValue as string; SolidBrush backBrush = DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected) ? cellStyle.SelectionBackColor : cellStyle.BackColor); SolidBrush foreBrush = DataGridView.GetCachedBrush(cellSelected ? cellStyle.SelectionForeColor : cellStyle.ForeColor); if (paint && DataGridViewCell.PaintBorder(paintParts)) { PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle valBounds = cellBounds; Rectangle borderWidths = BorderWidths(advancedBorderStyle); valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; if (valBounds.Height > 0 && valBounds.Width > 0) { if (paint && DataGridViewCell.PaintBackground(paintParts) && backBrush.Color.A == 255) { g.FillRectangle(backBrush, 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 (valBounds.Height > 0 && valBounds.Width > 0 && (paint || computeContentBounds)) { if (FlatStyle == FlatStyle.Standard || FlatStyle == FlatStyle.System) { if (DataGridView.ApplyVisualStylesToInnerCells) { if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { PushButtonState pbState = VisualStyles.PushButtonState.Normal; if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0) { pbState = VisualStyles.PushButtonState.Pressed; } else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex && DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds) { pbState = VisualStyles.PushButtonState.Hot; } if (DataGridViewCell.PaintFocus(paintParts) && cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused) { pbState |= VisualStyles.PushButtonState.Default; } DataGridViewButtonCellRenderer.DrawButton(g, valBounds, (int)pbState); } resultBounds = valBounds; valBounds = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetBackgroundContentRectangle(g, valBounds); } else { if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { ControlPaint.DrawBorder(g, valBounds, SystemColors.Control, (ButtonState == ButtonState.Normal) ? ButtonBorderStyle.Outset : ButtonBorderStyle.Inset); } resultBounds = valBounds; valBounds.Inflate(-SystemInformation.Border3DSize.Width, -SystemInformation.Border3DSize.Height); } } else if (FlatStyle == FlatStyle.Flat) { // ButtonBase::PaintFlatDown and ButtonBase::PaintFlatUp paint the border in the same way valBounds.Inflate(-1, -1); if (paint && PaintContentBackground(paintParts)) { ButtonBaseAdapter.DrawDefaultBorder(g, valBounds, foreBrush.Color, true /*isDefault == true*/); if (backBrush.Color.A == 255) { if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0) { ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintFlatRender( g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); using var hdc = new DeviceContextHdcScope(g); using var hbrush = new Gdi32.CreateBrushScope( colors.options.highContrast ? colors.buttonShadow : colors.lowHighlight); hdc.FillRectangle(valBounds, hbrush); } else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex && DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds) { using var hdc = new DeviceContextHdcScope(g); using var hbrush = new Gdi32.CreateBrushScope(SystemColors.ControlDark); hdc.FillRectangle(valBounds, hbrush); } } } resultBounds = valBounds; } else { Debug.Assert(FlatStyle == FlatStyle.Popup, "FlatStyle.Popup is the last flat style"); valBounds.Inflate(-1, -1); if (paint && DataGridViewCell.PaintContentBackground(paintParts)) { if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0) { // paint down ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, valBounds, colors.options.highContrast ? colors.windowText : colors.windowFrame, true /*isDefault*/); ControlPaint.DrawBorder(g, valBounds, colors.options.highContrast ? colors.windowText : colors.buttonShadow, ButtonBorderStyle.Solid); } else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex && DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds) { // paint over ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, valBounds, colors.options.highContrast ? colors.windowText : colors.buttonShadow, false /*isDefault*/); ButtonBaseAdapter.Draw3DLiteBorder(g, valBounds, colors, true); } else { // paint up ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintPopupRender(g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); ButtonBaseAdapter.DrawDefaultBorder(g, valBounds, colors.options.highContrast ? colors.windowText : colors.buttonShadow, false /*isDefault*/); ButtonBaseAdapter.DrawFlatBorder(g, valBounds, colors.options.highContrast ? colors.windowText : colors.buttonShadow); } } resultBounds = valBounds; } } else if (computeErrorIconBounds) { if (!string.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(errorBounds); } else { resultBounds = Rectangle.Empty; } } else { Debug.Assert(valBounds.Height <= 0 || valBounds.Width <= 0); resultBounds = Rectangle.Empty; } if (paint && DataGridViewCell.PaintFocus(paintParts) && cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused && valBounds.Width > 2 * SystemInformation.Border3DSize.Width + 1 && valBounds.Height > 2 * SystemInformation.Border3DSize.Height + 1) { // Draw focus rectangle if (FlatStyle == FlatStyle.System || FlatStyle == FlatStyle.Standard) { ControlPaint.DrawFocusRectangle(g, Rectangle.Inflate(valBounds, -1, -1), Color.Empty, SystemColors.Control); } else if (FlatStyle == FlatStyle.Flat) { if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0 || (DataGridView.CurrentCellAddress.Y == rowIndex && DataGridView.CurrentCellAddress.X == ColumnIndex)) { ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintFlatRender(g, cellStyle.ForeColor, cellStyle.BackColor, DataGridView.Enabled).Calculate(); string text = formattedString ?? string.Empty; ButtonBaseAdapter.LayoutOptions options = ButtonInternal.ButtonFlatAdapter.PaintFlatLayout(g, true, SystemInformation.HighContrast, 1, valBounds, Padding.Empty, false, cellStyle.Font, text, DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), DataGridView.RightToLeft); options.everettButtonCompat = false; ButtonBaseAdapter.LayoutData layout = options.Layout(); ButtonInternal.ButtonBaseAdapter.DrawFlatFocus(g, layout.focus, colors.options.highContrast ? colors.windowText : colors.constrastButtonShadow); } } else { Debug.Assert(FlatStyle == FlatStyle.Popup, "FlatStyle.Popup is the last flat style"); if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0 || (DataGridView.CurrentCellAddress.Y == rowIndex && DataGridView.CurrentCellAddress.X == ColumnIndex)) { // If we are painting the current cell, then paint the text up. // If we are painting the current cell and the current cell is pressed down, then paint the text down. bool paintUp = (ButtonState == ButtonState.Normal); string text = formattedString ?? string.Empty; ButtonBaseAdapter.LayoutOptions options = ButtonInternal.ButtonPopupAdapter.PaintPopupLayout(g, paintUp, SystemInformation.HighContrast ? 2 : 1, valBounds, Padding.Empty, false, cellStyle.Font, text, DataGridView.Enabled, DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment), DataGridView.RightToLeft); options.everettButtonCompat = false; ButtonBaseAdapter.LayoutData layout = options.Layout(); ControlPaint.DrawFocusRectangle(g, layout.focus, cellStyle.ForeColor, cellStyle.BackColor); } } } if (formattedString != null && paint && DataGridViewCell.PaintContentForeground(paintParts)) { // Font independent margins valBounds.Offset(DATAGRIDVIEWBUTTONCELL_horizontalTextMargin, DATAGRIDVIEWBUTTONCELL_verticalTextMargin); valBounds.Width -= 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin; valBounds.Height -= 2 * DATAGRIDVIEWBUTTONCELL_verticalTextMargin; if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0 && FlatStyle != FlatStyle.Flat && FlatStyle != FlatStyle.Popup) { valBounds.Offset(1, 1); valBounds.Width--; valBounds.Height--; } if (valBounds.Width > 0 && valBounds.Height > 0) { Color textColor; if (DataGridView.ApplyVisualStylesToInnerCells && (FlatStyle == FlatStyle.System || FlatStyle == FlatStyle.Standard)) { textColor = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetColor(ColorProperty.TextColor); } else { textColor = foreBrush.Color; } TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); TextRenderer.DrawText(g, formattedString, cellStyle.Font, valBounds, textColor, flags); } } if (DataGridView.ShowCellErrors && paint && DataGridViewCell.PaintErrorIcon(paintParts)) { PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, errorBounds, errorText); } } else { resultBounds = Rectangle.Empty; } return(resultBounds); }
private void DrawImage(Graphics g) { if (this.ImageList == null || this.ImageIndex == -1) { return; } if (this.ImageIndex < 0 || this.ImageIndex >= this.ImageList.Images.Count) { return; } Image _Image = this.ImageList.Images[this.ImageIndex]; Point pt = Point.Empty; switch (this.ImageAlign) { case ContentAlignment.TopLeft: pt.X = contentRect.Left; pt.Y = contentRect.Top; break; case ContentAlignment.TopCenter: pt.X = (Width - _Image.Width) / 2; pt.Y = contentRect.Top; break; case ContentAlignment.TopRight: pt.X = contentRect.Right - _Image.Width; pt.Y = contentRect.Top; break; case ContentAlignment.MiddleLeft: pt.X = contentRect.Left; pt.Y = (Height - _Image.Height) / 2; break; case ContentAlignment.MiddleCenter: pt.X = (Width - _Image.Width) / 2; pt.Y = (Height - _Image.Height) / 2; break; case ContentAlignment.MiddleRight: pt.X = contentRect.Right - _Image.Width; pt.Y = (Height - _Image.Height) / 2; break; case ContentAlignment.BottomLeft: pt.X = contentRect.Left; pt.Y = contentRect.Bottom - _Image.Height; break; case ContentAlignment.BottomCenter: pt.X = (Width - _Image.Width) / 2; pt.Y = contentRect.Bottom - _Image.Height; break; case ContentAlignment.BottomRight: pt.X = contentRect.Right - _Image.Width; pt.Y = contentRect.Bottom - _Image.Height; break; } if (this.ButtonState == CustomButtonState.Pressed) { pt.Offset(1, 1); } if (this.Enabled) { this.ImageList.Draw(g, pt, this.ImageIndex); } else { ControlPaint.DrawImageDisabled(g, _Image, pt.X, pt.Y, this.BackColor); } }
// 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 graphics, 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 computeContentBounds == TRUE then resultBounds will be the contentBounds. // If computeErrorIconBounds == TRUE then resultBounds will be the error icon bounds. // Else resultBounds will be Rectangle.Empty; Rectangle resultBounds = Rectangle.Empty; if (paint && DataGridViewCell.PaintBorder(paintParts)) { PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } Rectangle borderWidths = BorderWidths(advancedBorderStyle); Rectangle valBounds = cellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; SolidBrush br; Point ptCurrentCell = this.DataGridView.CurrentCellAddress; bool cellCurrent = ptCurrentCell.X == this.ColumnIndex && ptCurrentCell.Y == rowIndex; bool cellEdited = cellCurrent && this.DataGridView.EditingControl != null; bool cellSelected = (cellState & DataGridViewElementStates.Selected) != 0; if (DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected && !cellEdited) { br = this.DataGridView.GetCachedBrush(cellStyle.SelectionBackColor); } else { br = this.DataGridView.GetCachedBrush(cellStyle.BackColor); } if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255 && valBounds.Width > 0 && valBounds.Height > 0) { graphics.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; } if (paint && cellCurrent && !cellEdited) { // Draw focus rectangle if (DataGridViewCell.PaintFocus(paintParts) && this.DataGridView.ShowFocusCues && this.DataGridView.Focused && valBounds.Width > 0 && valBounds.Height > 0) { ControlPaint.DrawFocusRectangle(graphics, valBounds, Color.Empty, br.Color); } } Rectangle errorBounds = valBounds; string formattedString = formattedValue as string; if (formattedString != null && ((paint && !cellEdited) || computeContentBounds)) { // Font independent margins int verticalTextMarginTop = cellStyle.WrapMode == DataGridViewTriState.True ? DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginTopWithWrapping : DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginTopWithoutWrapping; valBounds.Offset(DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginLeft, verticalTextMarginTop); valBounds.Width -= DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginLeft + DATAGRIDVIEWTEXTBOXCELL_horizontalTextMarginRight; valBounds.Height -= verticalTextMarginTop + DATAGRIDVIEWTEXTBOXCELL_verticalTextMarginBottom; if (valBounds.Width > 0 && valBounds.Height > 0) { TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(this.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode); if (paint) { if (DataGridViewCell.PaintContentForeground(paintParts)) { if ((flags & TextFormatFlags.SingleLine) != 0) { flags |= TextFormatFlags.EndEllipsis; } TextRenderer.DrawText(graphics, formattedString, cellStyle.Font, valBounds, cellSelected ? cellStyle.SelectionForeColor : cellStyle.ForeColor, flags); } } else { resultBounds = DataGridViewUtilities.GetTextBounds(valBounds, formattedString, flags, cellStyle); } } } else if (computeErrorIconBounds) { if (!String.IsNullOrEmpty(errorText)) { resultBounds = ComputeErrorIconBounds(errorBounds); } } else { Debug.Assert(cellEdited || formattedString == null); Debug.Assert(paint || computeContentBounds); } if (this.DataGridView.ShowCellErrors && paint && DataGridViewCell.PaintErrorIcon(paintParts)) { PaintErrorIcon(graphics, cellStyle, rowIndex, cellBounds, errorBounds, errorText); } return(resultBounds); }
private void DrawText(Graphics g) { SolidBrush TextBrush = new SolidBrush(this.ForeColor); RectangleF R = (RectangleF)contentRect; if (!this.Enabled) { TextBrush.Color = SystemColors.GrayText; } StringFormat sf = new StringFormat(StringFormatFlags.NoWrap | StringFormatFlags.NoClip); if (ShowKeyboardCues) { sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show; } else { sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Hide; } switch (this.TextAlign) { case ContentAlignment.TopLeft: sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Near; break; case ContentAlignment.TopCenter: sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Near; break; case ContentAlignment.TopRight: sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Near; break; case ContentAlignment.MiddleLeft: sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; break; case ContentAlignment.MiddleCenter: sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; break; case ContentAlignment.MiddleRight: sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Center; break; case ContentAlignment.BottomLeft: sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Far; break; case ContentAlignment.BottomCenter: sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; break; case ContentAlignment.BottomRight: sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Far; break; } if (this.ButtonState == CustomButtonState.Pressed) { R.Offset(1, 1); } if (this.Enabled) { g.DrawString(this.Text, this.Font, TextBrush, R, sf); } else { ControlPaint.DrawStringDisabled(g, this.Text, this.Font, this.BackColor, R, sf); } }
/// <include file='doc\ToolStripRenderer.uex' path='docs/doc[@for="ToolStripRenderer.OnRenderSplitButton"]/*' /> /// <devdoc> /// Draw the item's background. /// </devdoc> protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e) { ToolStripSplitButton splitButton = e.Item as ToolStripSplitButton; Graphics g = e.Graphics; bool rightToLeft = (splitButton.RightToLeft == RightToLeft.Yes); Color arrowColor = splitButton.Enabled ? SystemColors.ControlText : SystemColors.ControlDark; // in right to left - we need to swap the parts so we dont draw v][ toolStripSplitButton VisualStyleElement splitButtonDropDownPart = (rightToLeft) ? VisualStyleElement.ToolBar.SplitButton.Normal : VisualStyleElement.ToolBar.SplitButtonDropDown.Normal; VisualStyleElement splitButtonPart = (rightToLeft) ? VisualStyleElement.ToolBar.DropDownButton.Normal : VisualStyleElement.ToolBar.SplitButton.Normal; Rectangle bounds = new Rectangle(Point.Empty, splitButton.Size); if (ToolStripManager.VisualStylesEnabled && VisualStyleRenderer.IsElementDefined(splitButtonDropDownPart) && VisualStyleRenderer.IsElementDefined(splitButtonPart)) { VisualStyleRenderer vsRenderer = VisualStyleRenderer; // Draw the SplitButton Button portion of it. vsRenderer.SetParameters(splitButtonPart.ClassName, splitButtonPart.Part, GetSplitButtonItemState(splitButton)); // the lovely Windows theming for split button comes in three pieces: // SplitButtonDropDown: [ v | // Separator: | // SplitButton: | ] // this is great except if you want to swap the button in RTL. In this case we need // to use the DropDownButton instead of the SplitButtonDropDown and paint the arrow ourselves. Rectangle splitButtonBounds = splitButton.ButtonBounds; if (rightToLeft) { // scoot to the left so we dont draw double shadow like so: ][ splitButtonBounds.Inflate(2, 0); } // Draw the button portion of it. vsRenderer.DrawBackground(g, splitButtonBounds); // Draw the SplitButton DropDownButton portion of it. vsRenderer.SetParameters(splitButtonDropDownPart.ClassName, splitButtonDropDownPart.Part, GetSplitButtonDropDownItemState(splitButton)); // Draw the drop down button portion vsRenderer.DrawBackground(g, splitButton.DropDownButtonBounds); // fill in the background image Rectangle fillRect = splitButton.ContentRectangle; if (splitButton.BackgroundImage != null) { ControlPaint.DrawBackgroundImage(g, splitButton.BackgroundImage, splitButton.BackColor, splitButton.BackgroundImageLayout, fillRect, fillRect); } // draw the separator over it. RenderSeparatorInternal(g, splitButton, splitButton.SplitterBounds, true); // and of course, now if we're in RTL we now need to paint the arrow // because we're no longer using a part that has it built in. if (rightToLeft || splitButton.BackgroundImage != null) { DrawArrow(new ToolStripArrowRenderEventArgs(g, splitButton, splitButton.DropDownButtonBounds, arrowColor, ArrowDirection.Down)); } } else { // Draw the split button button Rectangle splitButtonButtonRect = splitButton.ButtonBounds; if (splitButton.BackgroundImage != null) { // fill in the background image Rectangle fillRect = (splitButton.Selected) ? splitButton.ContentRectangle :bounds; if (splitButton.BackgroundImage != null) { ControlPaint.DrawBackgroundImage(g, splitButton.BackgroundImage, splitButton.BackColor, splitButton.BackgroundImageLayout, bounds, fillRect); } } else { FillBackground(g, splitButtonButtonRect, splitButton.BackColor); } ToolBarState state = GetSplitButtonToolBarState(splitButton, false); RenderSmall3DBorderInternal(g, splitButtonButtonRect, state, rightToLeft); // draw the split button drop down Rectangle dropDownRect = splitButton.DropDownButtonBounds; // fill the color in the dropdown button if (splitButton.BackgroundImage == null) { FillBackground(g, dropDownRect, splitButton.BackColor); } state = GetSplitButtonToolBarState(splitButton, true); if ((state == ToolBarState.Pressed) || (state == ToolBarState.Hot)) { RenderSmall3DBorderInternal(g, dropDownRect, state, rightToLeft); } DrawArrow(new ToolStripArrowRenderEventArgs(g, splitButton, dropDownRect, arrowColor, ArrowDirection.Down)); } }
/// <include file='doc\PrintPreviewControl.uex' path='docs/doc[@for="PrintPreviewControl.OnPaint"]/*' /> /// <internalonly/> /// <devdoc> /// <para> /// Paints the control. /// </para> /// </devdoc> protected override void OnPaint(PaintEventArgs pevent) { Brush backBrush = new SolidBrush(BackColor); try { if (pageInfo == null || pageInfo.Length == 0) { pevent.Graphics.FillRectangle(backBrush, ClientRectangle); if (pageInfo != null || exceptionPrinting) { // Calculate formats StringFormat format = new StringFormat(); format.Alignment = ControlPaint.TranslateAlignment(ContentAlignment.MiddleCenter); format.LineAlignment = ControlPaint.TranslateLineAlignment(ContentAlignment.MiddleCenter); // Do actual drawing SolidBrush brush = new SolidBrush(ForeColor); try { if (exceptionPrinting) { pevent.Graphics.DrawString(SR.PrintPreviewExceptionPrinting, Font, brush, ClientRectangle, format); } else { pevent.Graphics.DrawString(SR.PrintPreviewNoPages, Font, brush, ClientRectangle, format); } } finally { brush.Dispose(); format.Dispose(); } } else { BeginInvoke(new MethodInvoker(CalculatePageInfo)); } } else { if (!layoutOk) { ComputeLayout(); } Size controlPhysicalSize = new Size(PixelsToPhysical(new Point(Size), screendpi)); //Point imagePixels = PhysicalToPixels(new Point(imageSize), screendpi); Point virtualPixels = new Point(VirtualSize); // center pages on screen if small enough Point offset = new Point(Math.Max(0, (Size.Width - virtualPixels.X) / 2), Math.Max(0, (Size.Height - virtualPixels.Y) / 2)); offset.X -= Position.X; offset.Y -= Position.Y; lastOffset = offset; int borderPixelsX = PhysicalToPixels(border, screendpi.X); int borderPixelsY = PhysicalToPixels(border, screendpi.Y); Region originalClip = pevent.Graphics.Clip; Rectangle[] pageRenderArea = new Rectangle[rows * columns]; Point lastImageSize = Point.Empty; int maxImageHeight = 0; try { for (int row = 0; row < rows; row++) { //Initialize our LastImageSize variable... lastImageSize.X = 0; lastImageSize.Y = maxImageHeight * row; for (int column = 0; column < columns; column++) { int imageIndex = StartPage + column + row * columns; if (imageIndex < pageInfo.Length) { Size pageSize = pageInfo[imageIndex].PhysicalSize; if (autoZoom) { double zoomX = ((double)controlPhysicalSize.Width - border * (columns + 1)) / (columns * pageSize.Width); double zoomY = ((double)controlPhysicalSize.Height - border * (rows + 1)) / (rows * pageSize.Height); zoom = Math.Min(zoomX, zoomY); } imageSize = new Size((int)(zoom * pageSize.Width), (int)(zoom * pageSize.Height)); Point imagePixels = PhysicalToPixels(new Point(imageSize), screendpi); int x = offset.X + borderPixelsX * (column + 1) + lastImageSize.X; int y = offset.Y + borderPixelsY * (row + 1) + lastImageSize.Y; lastImageSize.X += imagePixels.X; //The Height is the Max of any PageHeight.. maxImageHeight = Math.Max(maxImageHeight, imagePixels.Y); pageRenderArea[imageIndex - StartPage] = new Rectangle(x, y, imagePixels.X, imagePixels.Y); pevent.Graphics.ExcludeClip(pageRenderArea[imageIndex - StartPage]); } } } pevent.Graphics.FillRectangle(backBrush, ClientRectangle); } finally { pevent.Graphics.Clip = originalClip; } for (int i = 0; i < pageRenderArea.Length; i++) { if (i + StartPage < pageInfo.Length) { Rectangle box = pageRenderArea[i]; pevent.Graphics.DrawRectangle(Pens.Black, box); using (SolidBrush brush = new SolidBrush(ForeColor)) { pevent.Graphics.FillRectangle(brush, box); } box.Inflate(-1, -1); if (pageInfo[i + StartPage].Image != null) { pevent.Graphics.DrawImage(pageInfo[i + StartPage].Image, box); } box.Width--; box.Height--; pevent.Graphics.DrawRectangle(Pens.Black, box); } } } } finally { backBrush.Dispose(); } base.OnPaint(pevent); // raise paint event }