Example #1
0
        // 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);
        }
        // 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 = DataGridView.CurrentCellAddress;
            bool       cellCurrent   = ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex;
            bool       cellSelected  = (cellState & DataGridViewElementStates.Selected) != 0;
            SolidBrush br            = 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 (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(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, LinkBehavior, ref linkFont, ref hoverFont);
                TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);
                // paint the focus rectangle around the link
                if (paint)
                {
                    if (valBounds.Width > 0 && valBounds.Height > 0)
                    {
                        if (cellCurrent &&
                            DataGridView.ShowFocusCues &&
                            DataGridView.Focused &&
                            DataGridViewCell.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, br.Color);
                        }
                        Color linkColor;
                        if ((LinkState & LinkState.Active) == LinkState.Active)
                        {
                            linkColor = ActiveLinkColor;
                        }
                        else if (LinkVisited)
                        {
                            linkColor = VisitedLinkColor;
                        }
                        else
                        {
                            linkColor = LinkColor;
                        }
                        if (DataGridViewCell.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 &&
                             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,
                                                                       LinkState == LinkState.Hover ? hoverFont : linkFont);
                }
                linkFont.Dispose();
                hoverFont.Dispose();
            }
Example #3
0
        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 is not 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;

            Color backBrushColor = PaintSelectionBackground(paintParts) && cellSelected
                ? cellStyle.SelectionBackColor
                : cellStyle.BackColor;
            Color foreBrushColor = cellSelected ? cellStyle.SelectionForeColor : cellStyle.ForeColor;

            if (paint && 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)
            {
                return(Rectangle.Empty);
            }

            if (paint && PaintBackground(paintParts) && !backBrushColor.HasTransparency())
            {
                using var backBrush = backBrushColor.GetCachedSolidBrushScope();
                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))
            {
                switch (FlatStyle)
                {
                case FlatStyle.Standard:
                case FlatStyle.System:
                    if (DataGridView.ApplyVisualStylesToInnerCells)
                    {
                        if (paint && PaintContentBackground(paintParts))
                        {
                            PushButtonState pbState = VisualStyles.PushButtonState.Normal;
                            if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0)
                            {
                                pbState = PushButtonState.Pressed;
                            }
                            else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex &&
                                     DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds)
                            {
                                pbState = PushButtonState.Hot;
                            }

                            if (PaintFocus(paintParts) && cellCurrent && DataGridView.ShowFocusCues && DataGridView.Focused)
                            {
                                pbState |= PushButtonState.Default;
                            }

                            DataGridViewButtonCellRenderer.DrawButton(g, valBounds, (int)pbState);
                        }

                        resultBounds = valBounds;
                        valBounds    = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetBackgroundContentRectangle(g, valBounds);
                    }
                    else
                    {
                        if (paint && 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);
                    }

                    break;

                case 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, backBrushColor, isDefault: true);

                        if (!backBrushColor.HasTransparency())
                        {
                            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;
                    break;

                default:
                    Debug.Assert(FlatStyle == FlatStyle.Popup, "FlatStyle.Popup is the last flat style");
                    valBounds.Inflate(-1, -1);
                    if (paint && 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,
                                isDefault: true);
                            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,
                                isDefault: false);
                            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,
                                isDefault: false);
                            ControlPaint.DrawBorderSimple(
                                g,
                                valBounds,
                                colors.Options.HighContrast ? colors.WindowText : colors.ButtonShadow);
                        }
                    }

                    resultBounds = valBounds;
                    break;
                }
            }
            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 &&
                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 = ButtonFlatAdapter.PaintFlatLayout(
                            true,
                            SystemInformation.HighContrast,
                            1,
                            valBounds,
                            Padding.Empty,
                            false,
                            cellStyle.Font,
                            text,
                            DataGridView.Enabled,
                            DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment),
                            DataGridView.RightToLeft);
                        options.DotNetOneButtonCompat = false;
                        ButtonBaseAdapter.LayoutData layout = options.Layout();

                        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 = ButtonPopupAdapter.PaintPopupLayout(
                            paintUp,
                            SystemInformation.HighContrast ? 2 : 1,
                            valBounds,
                            Padding.Empty,
                            false,
                            cellStyle.Font,
                            text,
                            DataGridView.Enabled,
                            DataGridViewUtilities.ComputeDrawingContentAlignmentForCellStyleAlignment(cellStyle.Alignment),
                            DataGridView.RightToLeft);
                        options.DotNetOneButtonCompat = false;
                        ButtonBaseAdapter.LayoutData layout = options.Layout();

                        ControlPaint.DrawFocusRectangle(
                            g,
                            layout.Focus,
                            cellStyle.ForeColor,
                            cellStyle.BackColor);
                    }
                }
            }

            if (formattedString is not null && paint && 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 = foreBrushColor;
                    }

                    TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(
                        DataGridView.RightToLeftInternal,
                        cellStyle.Alignment,
                        cellStyle.WrapMode);

                    TextRenderer.DrawText(
                        g,
                        formattedString,
                        cellStyle.Font,
                        valBounds,
                        textColor,
                        flags);
                }
            }

            if (DataGridView.ShowCellErrors && paint && PaintErrorIcon(paintParts))
            {
                PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, errorBounds, errorText);
            }

            return(resultBounds);
        }
Example #4
0
        // Draw the button in its current state on a Graphics surface.
        internal virtual void Draw(Graphics graphics)
        {
            ButtonState state      = CalculateState();
            Size        clientSize = ClientSize;
            int         x          = 0;
            int         y          = 0;
            int         width      = clientSize.Width;
            int         height     = clientSize.Height;

            // Draw the border and background.
            ThemeManager.MainPainter.DrawButton
                (graphics, x, y, width, height, state,
                ForeColor, BackColor, isDefault);

            // Draw the focus rectangle.
            if (hasFocus)
            {
                ControlPaint.DrawFocusRectangle
                    (graphics, new Rectangle(x + 4, y + 4,
                                             width - 8, height - 8),
                    ForeColor, BackColor);
            }

            // Draw the button image.
            Image image = this.image;

            if (image == null && imageList != null && imageIndex != -1)
            {
                image = imageList.Images[imageIndex];
            }
            if (image != null)
            {
                int imageX      = x;
                int imageY      = y;
                int imageWidth  = image.Width;
                int imageHeight = image.Height;
                switch (imageAlign)
                {
                case ContentAlignment.TopLeft: break;

                case ContentAlignment.TopCenter:
                {
                    imageX += (width - imageWidth) / 2;
                }
                break;

                case ContentAlignment.TopRight:
                {
                    imageX += width - imageWidth;
                }
                break;

                case ContentAlignment.MiddleLeft:
                {
                    imageY += (height - imageHeight) / 2;
                }
                break;

                case ContentAlignment.MiddleCenter:
                {
                    imageX += (width - imageWidth) / 2;
                    imageY += (height - imageHeight) / 2;
                }
                break;

                case ContentAlignment.MiddleRight:
                {
                    imageX += width - imageWidth;
                    imageY += (height - imageHeight) / 2;
                }
                break;

                case ContentAlignment.BottomLeft:
                {
                    imageY += height - imageHeight;
                }
                break;

                case ContentAlignment.BottomCenter:
                {
                    imageX += (width - imageWidth) / 2;
                    imageY += height - imageHeight;
                }
                break;

                case ContentAlignment.BottomRight:
                {
                    imageX += width - imageWidth;
                    imageY += height - imageHeight;
                }
                break;
                }
                if (pressed)
                {
                    ++imageX;
                    ++imageY;
                }
                if (Enabled)
                {
                    graphics.DrawImage(image, imageX, imageY);
                }
                else
                {
                    ControlPaint.DrawImageDisabled
                        (graphics, image, imageX, imageY, BackColor);
                }
            }

            // Get the text layout rectangle.
            RectangleF layout = new RectangleF
                                    (x + 2, y + 2, width - 4, height - 4);

            if (entered && pressed)
            {
                layout.Offset(1.0f, 1.0f);
            }

            // If we are using "middle" alignment, then shift the
            // text down to account for the descent.  This makes
            // the button "look better".
            Font font = Font;

            if ((TextAlign & (ContentAlignment.MiddleLeft |
                              ContentAlignment.MiddleCenter |
                              ContentAlignment.MiddleRight)) != 0)
            {
                layout.Offset(0.0f, GetDescent(graphics, font) / 2.0f);
            }

            // Draw the text on the button.
            String text = Text;

            if (text != null && text != String.Empty)
            {
                if (Enabled)
                {
                    Brush brush = new SolidBrush(ForeColor);
                    graphics.DrawString(text, font, brush, layout, format);
                    brush.Dispose();
                }
                else
                {
                    ControlPaint.DrawStringDisabled
                        (graphics, text, font, BackColor, layout, format);
                }
            }
        }
Example #5
0
        // 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);
        }
Example #6
0
        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);
                }
            }
        }
        /// <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);

            string        itemText = this.SelectedIndex >= 0 ? this.GetItemText(this.SelectedItem) : string.Empty;
            ComboBoxState 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;
            bool      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", this.DropDownStyle == ComboBoxStyle.DropDownList ? 5 : 4, (int)state);
                        vr.DrawParentBackground(e.Graphics, this.ClientRectangle, this);
                        vr.DrawBackground(e.Graphics, this.ClientRectangle);
                        if (this.DropDownStyle != ComboBoxStyle.DropDownList)
                        {
                            br.Inflate(1, 1);
                        }
                        Rectangle cr = this.DropDownStyle == ComboBoxStyle.DropDownList ? Rectangle.Inflate(br, -1, -1) : br;
                        vr.SetParameters("Combobox", 7, (int)(br.Contains(this.PointToClient(Cursor.Position)) ? state : ComboBoxState.Normal));
                        vr.DrawBackground(e.Graphics, br, cr);
                        if (this.Focused && State != ComboBoxState.Pressed)
                        {
                            Size      sz = TextRenderer.MeasureText(e.Graphics, "Wg", this.Font, tr.Size, TextFormatFlags.Default);
                            Rectangle fr = Rectangle.Inflate(tr, 0, ((sz.Height - tr.Height) / 2) + 1);
                            ControlPaint.DrawFocusRectangle(e.Graphics, fr);
                        }
                        TextRenderer.DrawText(e.Graphics, itemText, this.Font, tr, this.ForeColor, tff);
                        vsSuccess = true;
                    }
                    catch { }
                }
            }

            if (!vsSuccess)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("CR:{0};ClR:{1};Foc:{2};St:{3};Tx:{4}", ClientRectangle, e.ClipRectangle, this.Focused, state, itemText));
                e.Graphics.Clear(this.BackColor);
                ControlPaint.DrawBorder3D(e.Graphics, this.ClientRectangle, Border3DStyle.Sunken);
                ControlPaint.DrawComboButton(e.Graphics, br, this.Enabled ? (state == ComboBoxState.Pressed ? ButtonState.Pushed : ButtonState.Normal) : ButtonState.Inactive);
                //using (var bb = new SolidBrush(this.BackColor))
                //	e.Graphics.FillRectangle(bb, tr);
                if (this.Focused)
                {
                    Size      sz = TextRenderer.MeasureText(e.Graphics, "Wg", this.Font, tr.Size, TextFormatFlags.Default);
                    Rectangle fr = Rectangle.Inflate(tr, 0, ((sz.Height - tr.Height) / 2) + 1);
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, fr);
                    ControlPaint.DrawFocusRectangle(e.Graphics, fr);                     //, this.ForeColor, SystemColors.Highlight);
                }
                TextRenderer.DrawText(e.Graphics, itemText, this.Font, tr, this.Focused ? SystemColors.HighlightText : this.ForeColor, tff);
            }
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            object obj2;

            if (this.Font.Height < 0)
            {
                this.Font = Control.DefaultFont;
            }
            if (e.Index < 0)
            {
                return;
            }
            if (e.Index < this.Items.Count)
            {
                obj2 = this.Items[e.Index];
            }
            else
            {
                obj2 = base.NativeGetItemText(e.Index);
            }
            Rectangle   bounds = e.Bounds;
            int         num    = 1;
            int         num2   = this.Font.Height + (2 * num);
            ButtonState normal = ButtonState.Normal;

            if (this.flat)
            {
                normal |= ButtonState.Flat;
            }
            if (e.Index < this.Items.Count)
            {
                switch (this.CheckedItems.GetCheckedState(e.Index))
                {
                case CheckState.Checked:
                    normal |= ButtonState.Checked;
                    break;

                case CheckState.Indeterminate:
                    normal |= ButtonState.Checked | ButtonState.Inactive;
                    break;
                }
            }
            if (Application.RenderWithVisualStyles)
            {
                CheckBoxState state = CheckBoxRenderer.ConvertFromButtonState(normal, false, (e.State & DrawItemState.HotLight) == DrawItemState.HotLight);
                this.idealCheckSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state).Width;
            }
            int num3 = Math.Max((num2 - this.idealCheckSize) / 2, 0);

            if ((num3 + this.idealCheckSize) > bounds.Height)
            {
                num3 = bounds.Height - this.idealCheckSize;
            }
            Rectangle rectangle = new Rectangle(bounds.X + num, bounds.Y + num3, this.idealCheckSize, this.idealCheckSize);

            if (this.RightToLeft == RightToLeft.Yes)
            {
                rectangle.X = ((bounds.X + bounds.Width) - this.idealCheckSize) - num;
            }
            if (Application.RenderWithVisualStyles)
            {
                CheckBoxState state3 = CheckBoxRenderer.ConvertFromButtonState(normal, false, (e.State & DrawItemState.HotLight) == DrawItemState.HotLight);
                CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(rectangle.X, rectangle.Y), state3);
            }
            else
            {
                ControlPaint.DrawCheckBox(e.Graphics, rectangle, normal);
            }
            Rectangle rect = new Rectangle((bounds.X + this.idealCheckSize) + (num * 2), bounds.Y, bounds.Width - (this.idealCheckSize + (num * 2)), bounds.Height);

            if (this.RightToLeft == RightToLeft.Yes)
            {
                rect.X = bounds.X;
            }
            string s         = "";
            Color  highlight = (this.SelectionMode != System.Windows.Forms.SelectionMode.None) ? e.BackColor : this.BackColor;
            Color  grayText  = (this.SelectionMode != System.Windows.Forms.SelectionMode.None) ? e.ForeColor : this.ForeColor;

            if (!base.Enabled)
            {
                grayText = SystemColors.GrayText;
            }
            Font font = this.Font;

            s = base.GetItemText(obj2);
            if ((this.SelectionMode != System.Windows.Forms.SelectionMode.None) && ((e.State & DrawItemState.Selected) == DrawItemState.Selected))
            {
                if (base.Enabled)
                {
                    highlight = SystemColors.Highlight;
                    grayText  = SystemColors.HighlightText;
                }
                else
                {
                    highlight = SystemColors.InactiveBorder;
                    grayText  = SystemColors.GrayText;
                }
            }
            using (Brush brush = new SolidBrush(highlight))
            {
                e.Graphics.FillRectangle(brush, rect);
            }
            Rectangle layoutRectangle = new Rectangle(rect.X + 1, rect.Y, rect.Width - 1, rect.Height - (num * 2));

            if (this.UseCompatibleTextRendering)
            {
                using (StringFormat format = new StringFormat())
                {
                    if (base.UseTabStops)
                    {
                        float   num4     = 3.6f * this.Font.Height;
                        float[] tabStops = new float[15];
                        float   num5     = -(this.idealCheckSize + (num * 2));
                        for (int i = 1; i < tabStops.Length; i++)
                        {
                            tabStops[i] = num4;
                        }
                        if (Math.Abs(num5) < num4)
                        {
                            tabStops[0] = num4 + num5;
                        }
                        else
                        {
                            tabStops[0] = num4;
                        }
                        format.SetTabStops(0f, tabStops);
                    }
                    else if (base.UseCustomTabOffsets)
                    {
                        float[] destination = new float[base.CustomTabOffsets.Count];
                        base.CustomTabOffsets.CopyTo(destination, 0);
                        format.SetTabStops(0f, destination);
                    }
                    if (this.RightToLeft == RightToLeft.Yes)
                    {
                        format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                    }
                    format.FormatFlags |= StringFormatFlags.NoWrap;
                    format.Trimming     = StringTrimming.None;
                    using (SolidBrush brush2 = new SolidBrush(grayText))
                    {
                        e.Graphics.DrawString(s, font, brush2, layoutRectangle, format);
                    }
                    goto Label_049B;
                }
            }
            TextFormatFlags flags = TextFormatFlags.Default;

            flags |= TextFormatFlags.NoPrefix;
            if (base.UseTabStops || base.UseCustomTabOffsets)
            {
                flags |= TextFormatFlags.ExpandTabs;
            }
            if (this.RightToLeft == RightToLeft.Yes)
            {
                flags |= TextFormatFlags.RightToLeft;
                flags |= TextFormatFlags.Right;
            }
            TextRenderer.DrawText(e.Graphics, s, font, layoutRectangle, grayText, flags);
Label_049B:
            if (((e.State & DrawItemState.Focus) == DrawItemState.Focus) && ((e.State & DrawItemState.NoFocusRect) != DrawItemState.NoFocusRect))
            {
                ControlPaint.DrawFocusRectangle(e.Graphics, rect, grayText, highlight);
            }
        }
Example #9
0
        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)
        {
            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);
        }
        /// <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);
                }
            }
        }