private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object formattedValue, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool paint)
        {
            Rectangle empty = Rectangle.Empty;

            if (paint && DataGridViewCell.PaintBorder(paintParts))
            {
                this.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
            }
            Rectangle bounds     = cellBounds;
            Rectangle rectangle3 = this.BorderWidths(advancedBorderStyle);

            bounds.Offset(rectangle3.X, rectangle3.Y);
            bounds.Width  -= rectangle3.Right;
            bounds.Height -= rectangle3.Bottom;
            Rectangle destRect = bounds;
            bool      flag     = (dataGridViewElementState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;

            if (base.DataGridView.ApplyVisualStylesToHeaderCells)
            {
                if ((cellStyle.Padding != Padding.Empty) && (cellStyle.Padding != Padding.Empty))
                {
                    if (base.DataGridView.RightToLeftInternal)
                    {
                        bounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
                    }
                    else
                    {
                        bounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top);
                    }
                    bounds.Width  -= cellStyle.Padding.Horizontal;
                    bounds.Height -= cellStyle.Padding.Vertical;
                }
                if ((paint && DataGridViewCell.PaintBackground(paintParts)) && ((destRect.Width > 0) && (destRect.Height > 0)))
                {
                    int headerState = 1;
                    if (((base.OwningColumn != null) && (base.OwningColumn.SortMode != DataGridViewColumnSortMode.NotSortable)) || ((base.DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect) || (base.DataGridView.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect)))
                    {
                        if (base.ButtonState != ButtonState.Normal)
                        {
                            headerState = 3;
                        }
                        else if ((base.DataGridView.MouseEnteredCellAddress.Y == rowIndex) && (base.DataGridView.MouseEnteredCellAddress.X == base.ColumnIndex))
                        {
                            headerState = 2;
                        }
                        else if (flag)
                        {
                            headerState = 3;
                        }
                    }
                    if (base.DataGridView.RightToLeftInternal)
                    {
                        Bitmap flipXPThemesBitmap = base.FlipXPThemesBitmap;
                        if (((flipXPThemesBitmap == null) || (flipXPThemesBitmap.Width < destRect.Width)) || (((flipXPThemesBitmap.Width > (2 * destRect.Width)) || (flipXPThemesBitmap.Height < destRect.Height)) || (flipXPThemesBitmap.Height > (2 * destRect.Height))))
                        {
                            flipXPThemesBitmap = base.FlipXPThemesBitmap = new Bitmap(destRect.Width, destRect.Height);
                        }
                        DataGridViewColumnHeaderCellRenderer.DrawHeader(Graphics.FromImage(flipXPThemesBitmap), new Rectangle(0, 0, destRect.Width, destRect.Height), headerState);
                        flipXPThemesBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
                        g.DrawImage(flipXPThemesBitmap, destRect, new Rectangle(flipXPThemesBitmap.Width - destRect.Width, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                    }
                    else
                    {
                        DataGridViewColumnHeaderCellRenderer.DrawHeader(g, destRect, headerState);
                    }
                }
                Rectangle themeMargins = DataGridViewHeaderCell.GetThemeMargins(g);
                bounds.Y      += themeMargins.Y;
                bounds.Height -= themeMargins.Y + themeMargins.Height;
                if (base.DataGridView.RightToLeftInternal)
                {
                    bounds.X     += themeMargins.Width;
                    bounds.Width -= themeMargins.X + themeMargins.Width;
                }
                else
                {
                    bounds.X     += themeMargins.X;
                    bounds.Width -= themeMargins.X + themeMargins.Width;
                }
            }
            else
            {
                if ((paint && DataGridViewCell.PaintBackground(paintParts)) && ((destRect.Width > 0) && (destRect.Height > 0)))
                {
                    SolidBrush cachedBrush = base.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && flag) ? cellStyle.SelectionBackColor : cellStyle.BackColor);
                    if (cachedBrush.Color.A == 0xff)
                    {
                        g.FillRectangle(cachedBrush, destRect);
                    }
                }
                if (cellStyle.Padding != Padding.Empty)
                {
                    if (base.DataGridView.RightToLeftInternal)
                    {
                        bounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
                    }
                    else
                    {
                        bounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top);
                    }
                    bounds.Width  -= cellStyle.Padding.Horizontal;
                    bounds.Height -= cellStyle.Padding.Vertical;
                }
            }
            bool   flag2 = false;
            Point  point = new Point(0, 0);
            string str   = formattedValue as string;

            bounds.Y++;
            bounds.Height -= 2;
            if (((((bounds.Width - 2) - 2) > 0) && (bounds.Height > 0)) && !string.IsNullOrEmpty(str))
            {
                Color color;
                bounds.Offset(2, 0);
                bounds.Width -= 4;
                if (base.DataGridView.ApplyVisualStylesToHeaderCells)
                {
                    color = DataGridViewColumnHeaderCellRenderer.VisualStyleRenderer.GetColor(ColorProperty.TextColor);
                }
                else
                {
                    color = flag ? cellStyle.SelectionForeColor : cellStyle.ForeColor;
                }
                if ((base.OwningColumn != null) && (base.OwningColumn.SortMode != DataGridViewColumnSortMode.NotSortable))
                {
                    bool flag3;
                    int  maxWidth = ((bounds.Width - 2) - 9) - 8;
                    if (((maxWidth > 0) && (DataGridViewCell.GetPreferredTextHeight(g, base.DataGridView.RightToLeftInternal, str, cellStyle, maxWidth, out flag3) <= bounds.Height)) && !flag3)
                    {
                        flag2         = this.SortGlyphDirection != SortOrder.None;
                        bounds.Width -= 0x13;
                        if (base.DataGridView.RightToLeftInternal)
                        {
                            bounds.X += 0x13;
                            point     = new Point((((bounds.Left - 2) - 2) - 4) - 9, bounds.Top + ((bounds.Height - 7) / 2));
                        }
                        else
                        {
                            point = new Point(((bounds.Right + 2) + 2) + 4, bounds.Top + ((bounds.Height - 7) / 2));
                        }
                    }
                }
                TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);
                if (paint)
                {
                    if (DataGridViewCell.PaintContentForeground(paintParts))
                    {
                        if ((flags & TextFormatFlags.SingleLine) != TextFormatFlags.Default)
                        {
                            flags |= TextFormatFlags.EndEllipsis;
                        }
                        TextRenderer.DrawText(g, str, cellStyle.Font, bounds, color, flags);
                    }
                }
                else
                {
                    empty = DataGridViewUtilities.GetTextBounds(bounds, str, flags, cellStyle);
                }
            }
            else if ((paint && (this.SortGlyphDirection != SortOrder.None)) && ((bounds.Width >= 0x11) && (bounds.Height >= 7)))
            {
                flag2 = true;
                point = new Point(bounds.Left + ((bounds.Width - 9) / 2), bounds.Top + ((bounds.Height - 7) / 2));
            }
            if ((paint && flag2) && DataGridViewCell.PaintContentBackground(paintParts))
            {
                Pen darkPen  = null;
                Pen lightPen = null;
                base.GetContrastedPens(cellStyle.BackColor, ref darkPen, ref lightPen);
                if (this.SortGlyphDirection != SortOrder.Ascending)
                {
                    switch (advancedBorderStyle.Right)
                    {
                    case DataGridViewAdvancedCellBorderStyle.Inset:
                        g.DrawLine(lightPen, point.X, point.Y + 1, (point.X + 4) - 1, (point.Y + 7) - 1);
                        g.DrawLine(lightPen, (int)(point.X + 1), (int)(point.Y + 1), (int)((point.X + 4) - 1), (int)((point.Y + 7) - 1));
                        g.DrawLine(darkPen, (int)(point.X + 4), (int)((point.Y + 7) - 1), (int)((point.X + 9) - 2), (int)(point.Y + 1));
                        g.DrawLine(darkPen, (int)(point.X + 4), (int)((point.Y + 7) - 1), (int)((point.X + 9) - 3), (int)(point.Y + 1));
                        g.DrawLine(darkPen, point.X, point.Y, (point.X + 9) - 2, point.Y);
                        return(empty);

                    case DataGridViewAdvancedCellBorderStyle.Outset:
                    case DataGridViewAdvancedCellBorderStyle.OutsetDouble:
                    case DataGridViewAdvancedCellBorderStyle.OutsetPartial:
                        g.DrawLine(darkPen, point.X, point.Y + 1, (point.X + 4) - 1, (point.Y + 7) - 1);
                        g.DrawLine(darkPen, (int)(point.X + 1), (int)(point.Y + 1), (int)((point.X + 4) - 1), (int)((point.Y + 7) - 1));
                        g.DrawLine(lightPen, (int)(point.X + 4), (int)((point.Y + 7) - 1), (int)((point.X + 9) - 2), (int)(point.Y + 1));
                        g.DrawLine(lightPen, (int)(point.X + 4), (int)((point.Y + 7) - 1), (int)((point.X + 9) - 3), (int)(point.Y + 1));
                        g.DrawLine(lightPen, point.X, point.Y, (point.X + 9) - 2, point.Y);
                        return(empty);
                    }
                    for (int j = 0; j < 4; j++)
                    {
                        g.DrawLine(darkPen, (int)(point.X + j), (int)((point.Y + j) + 2), (int)(((point.X + 9) - j) - 1), (int)((point.Y + j) + 2));
                    }
                    g.DrawLine(darkPen, (int)(point.X + 4), (int)((point.Y + 4) + 1), (int)(point.X + 4), (int)((point.Y + 4) + 2));
                    return(empty);
                }
                switch (advancedBorderStyle.Right)
                {
                case DataGridViewAdvancedCellBorderStyle.Inset:
                    g.DrawLine(lightPen, point.X, (point.Y + 7) - 2, (point.X + 4) - 1, point.Y);
                    g.DrawLine(lightPen, point.X + 1, (point.Y + 7) - 2, (point.X + 4) - 1, point.Y);
                    g.DrawLine(darkPen, point.X + 4, point.Y, (point.X + 9) - 2, (point.Y + 7) - 2);
                    g.DrawLine(darkPen, point.X + 4, point.Y, (point.X + 9) - 3, (point.Y + 7) - 2);
                    g.DrawLine(darkPen, point.X, (point.Y + 7) - 1, (point.X + 9) - 2, (point.Y + 7) - 1);
                    return(empty);

                case DataGridViewAdvancedCellBorderStyle.Outset:
                case DataGridViewAdvancedCellBorderStyle.OutsetDouble:
                case DataGridViewAdvancedCellBorderStyle.OutsetPartial:
                    g.DrawLine(darkPen, point.X, (point.Y + 7) - 2, (point.X + 4) - 1, point.Y);
                    g.DrawLine(darkPen, point.X + 1, (point.Y + 7) - 2, (point.X + 4) - 1, point.Y);
                    g.DrawLine(lightPen, point.X + 4, point.Y, (point.X + 9) - 2, (point.Y + 7) - 2);
                    g.DrawLine(lightPen, point.X + 4, point.Y, (point.X + 9) - 3, (point.Y + 7) - 2);
                    g.DrawLine(lightPen, point.X, (point.Y + 7) - 1, (point.X + 9) - 2, (point.Y + 7) - 1);
                    return(empty);
                }
                for (int i = 0; i < 4; i++)
                {
                    g.DrawLine(darkPen, (int)(point.X + i), (int)(((point.Y + 7) - i) - 1), (int)(((point.X + 9) - i) - 1), (int)(((point.Y + 7) - i) - 1));
                }
                g.DrawLine(darkPen, (int)(point.X + 4), (int)(((point.Y + 7) - 4) - 1), (int)(point.X + 4), (int)((point.Y + 7) - 4));
            }
            return(empty);
        }