コード例 #1
0
        internal static Size GetGlyphSize(IDeviceContext deviceContext, CheckBoxState state, IntPtr hwnd = default)
        {
            if (!RenderWithVisualStyles)
            {
                return(new Size(13, 13));
            }

            using var hdc = new DeviceContextHdcScope(deviceContext);
            return(GetGlyphSize(hdc, state, hwnd));
        }
コード例 #2
0
 public static void DrawText(
     this DeviceContextHdcScope hdc,
     string?text,
     FontCache.FontScope font,
     Rectangle bounds,
     Color foreColor,
     User32.DT flags,
     Color backColor            = default,
     TextPaddingOptions padding = default)
 => DrawText(hdc.HDC, text, font, bounds, foreColor, flags, backColor, padding);
コード例 #3
0
 public PaintEventHdcScope(PaintEventArgs args)
 {
     if (!args.IsGraphicsCreated)
     {
         HDC    = args.HDC;
         _scope = default;
     }
     else
     {
         _scope = new DeviceContextHdcScope(args.Graphics);
         HDC    = _scope.HDC;
     }
 }
コード例 #4
0
ファイル: CheckBoxRenderer.cs プロジェクト: tbolon/winforms
        internal static void DrawCheckBoxWithVisualStyles(
            IDeviceContext deviceContext,
            Point glyphLocation,
            CheckBoxState state,
            IntPtr hwnd = default)
        {
            InitializeRenderer((int)state);

            using var hdc = new DeviceContextHdcScope(deviceContext);
            Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(hdc, state, hwnd));

            t_visualStyleRenderer.DrawBackground(hdc, glyphBounds, hwnd);
        }
コード例 #5
0
        internal static void DrawRadioButton(Graphics g, Point glyphLocation, RadioButtonState state, IntPtr hWnd)
        {
            Rectangle glyphBounds;

            if (RenderWithVisualStyles)
            {
                InitializeRenderer((int)state);
                using var hdc = new DeviceContextHdcScope(g);
                glyphBounds   = new Rectangle(glyphLocation, GetGlyphSize(hdc, state, hWnd));
                t_visualStyleRenderer.DrawBackground(hdc, glyphBounds, hWnd);
            }
            else
            {
                using (var hdc = new DeviceContextHdcScope(g))
                {
                    glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(hdc, state, hWnd));
                }
                ControlPaint.DrawRadioButton(g, glyphBounds, ConvertToButtonState(state));
            }
        }
コード例 #6
0
        private static Size MeasureTextInternal(
            IDeviceContext dc,
            ReadOnlySpan<char> text,
            Font? font,
            Size proposedSize,
            TextFormatFlags flags = TextFormatFlags.Bottom)
        {
            if (dc is null)
                throw new ArgumentNullException(nameof(dc));

            if (text.IsEmpty)
                return Size.Empty;

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc = new DeviceContextHdcScope(dc);
            using var hfont = GdiCache.GetHFONT(font, quality, hdc);
            return hdc.MeasureText(text, hfont, proposedSize, GetTextFormatFlags(flags));
        }
コード例 #7
0
        internal static void DrawRadioButton(
            Graphics graphics,
            Point glyphLocation,
            RadioButtonState state,
            IntPtr hWnd)
        {
            Rectangle glyphBounds;

            if (RenderWithVisualStyles)
            {
                using var hdc = new DeviceContextHdcScope(graphics);
                DrawRadioButtonWithVisualStyles(hdc, glyphLocation, state, hWnd);
            }
            else
            {
                using (var hdc = new DeviceContextHdcScope(graphics))
                {
                    glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(hdc, state, hWnd));
                }
                ControlPaint.DrawRadioButton(graphics, glyphBounds, ConvertToButtonState(state));
            }
        }
コード例 #8
0
        internal static void DrawTextInternal(
            PaintEventArgs e,
            string?text,
            Font?font,
            Rectangle bounds,
            Color foreColor,
            Color backColor,
            TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter)
        {
            Gdi32.HDC hdc = e.HDC;
            if (hdc.IsNull)
            {
                // This MUST come before retreiving the HDC, which locks the Graphics object
                Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(e.GraphicsInternal);

                using var graphicsHdc = new DeviceContextHdcScope(e.GraphicsInternal, applyGraphicsState: false);
                DrawTextInternal(graphicsHdc, text, font, bounds, foreColor, quality, backColor, flags);
            }
            else
            {
                DrawTextInternal(hdc, text, font, bounds, foreColor, DefaultQuality, backColor, flags);
            }
        }
コード例 #9
0
        internal static void DrawTextInternal(
            IDeviceContext dc,
            ReadOnlySpan<char> text,
            Font? font,
            Rectangle bounds,
            Color foreColor,
            Color backColor,
            User32.DT flags = User32.DT.CENTER | User32.DT.VCENTER)
        {
            if (dc is null)
                throw new ArgumentNullException(nameof(dc));

            // Avoid creating the HDC, etc if we're not going to do any drawing
            if (text.IsEmpty || foreColor == Color.Transparent)
                return;

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc = new DeviceContextHdcScope(dc);

            DrawTextInternal(hdc, text, font, bounds, foreColor, quality, backColor, flags);
        }
コード例 #10
0
        internal static void DrawCheckBox(Graphics g, Point glyphLocation, CheckBoxState state, IntPtr hWnd)
        {
            Rectangle glyphBounds = new Rectangle(glyphLocation, GetGlyphSize(g, state, hWnd));

            if (RenderWithVisualStyles)
            {
                InitializeRenderer((int)state);

                using var hdc = new DeviceContextHdcScope(g);
                visualStyleRenderer.DrawBackground(hdc, glyphBounds, hWnd);
            }
            else
            {
                if (IsMixed(state))
                {
                    ControlPaint.DrawMixedCheckBox(g, glyphBounds, ConvertToButtonState(state));
                }
                else
                {
                    ControlPaint.DrawCheckBox(g, glyphBounds, ConvertToButtonState(state));
                }
            }
        }
コード例 #11
0
        private static Size MeasureTextInternal(
            IDeviceContext dc,
            ReadOnlySpan <char> text,
            Font?font,
            Size proposedSize,
            TextFormatFlags flags = TextFormatFlags.Bottom)
        {
            ArgumentNullException.ThrowIfNull(dc);

            if (text.IsEmpty)
            {
                return(Size.Empty);
            }

            // This MUST come before retrieving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            // Applying state may not impact text size measurements. Rather than risk missing some
            // case we'll apply as we have historically to avoid surprise regressions.
            using var hdc   = new DeviceContextHdcScope(dc, GetApplyStateFlags(dc, flags));
            using var hfont = GdiCache.GetHFONT(font, quality, hdc);
            return(hdc.HDC.MeasureText(text, hfont, proposedSize, flags));
        }
コード例 #12
0
        internal static void DrawButtonForHandle(Graphics g, Rectangle bounds, bool focused, PushButtonState state, IntPtr handle)
        {
            Rectangle contentBounds;

            if (RenderWithVisualStyles)
            {
                InitializeRenderer((int)state);

                using var hdc = new DeviceContextHdcScope(g);
                visualStyleRenderer.DrawBackground(hdc, bounds, handle);
                contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(hdc, bounds);
            }
            else
            {
                ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
                contentBounds = Rectangle.Inflate(bounds, -3, -3);
            }

            if (focused)
            {
                ControlPaint.DrawFocusRectangle(g, contentBounds);
            }
        }
コード例 #13
0
        /// <summary>
        ///  Method to draw visualstyle themes in case of per-monitor scenarios where Hwnd is necessary
        /// </summary>
        /// <param name="hwnd"> handle to the control</param>
        internal static void DrawButtonForHandle(
            IDeviceContext deviceContext,
            Rectangle bounds,
            bool focused,
            PushButtonState state,
            IntPtr hwnd)
        {
            Rectangle contentBounds;

            if (RenderWithVisualStyles)
            {
                InitializeRenderer((int)state);

                using var hdc = new DeviceContextHdcScope(deviceContext);
                t_visualStyleRenderer.DrawBackground(hdc, bounds, hwnd);
                contentBounds = t_visualStyleRenderer.GetBackgroundContentRectangle(hdc, bounds);
            }
            else
            {
                Graphics?graphics = deviceContext.TryGetGraphics(create: true);
                if (graphics is not null)
                {
                    ControlPaint.DrawButton(graphics, bounds, ConvertToButtonState(state));
                }

                contentBounds = Rectangle.Inflate(bounds, -3, -3);
            }

            if (focused)
            {
                Graphics?graphics = deviceContext.TryGetGraphics(create: true);
                if (graphics is not null)
                {
                    ControlPaint.DrawFocusRectangle(graphics, contentBounds);
                }
            }
        }
コード例 #14
0
        internal static void DrawTextInternal(
            IDeviceContext dc,
            ReadOnlySpan <char> text,
            Font?font,
            Rectangle bounds,
            Color foreColor,
            Color backColor,
            TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter)
        {
            ArgumentNullException.ThrowIfNull(dc);

            // Avoid creating the HDC, etc if we're not going to do any drawing
            if (text.IsEmpty || foreColor == Color.Transparent)
            {
                return;
            }

            // This MUST come before retrieving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc = new DeviceContextHdcScope(dc, GetApplyStateFlags(dc, flags));

            DrawTextInternal(hdc, text, font, bounds, foreColor, quality, backColor, flags);
        }
コード例 #15
0
        private static Size MeasureTextInternal(
            IDeviceContext dc,
            string?text,
            Font?font,
            Size proposedSize,
            TextFormatFlags flags = TextFormatFlags.Bottom)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            // This MUST come before retreiving the HDC, which locks the Graphics object
            Gdi32.QUALITY quality = FontQualityFromTextRenderingHint(dc);

            using var hdc   = new DeviceContextHdcScope(dc);
            using var hfont = GdiCache.GetHFONT(font, quality);
            return(hdc.MeasureText(text, hfont, proposedSize, GetTextFormatFlags(flags)));
        }
コード例 #16
0
ファイル: ComboBoxRenderer.cs プロジェクト: tbolon/winforms
 /// <summary>
 ///  Renders a ComboBox drop-down button.
 /// </summary>
 public static void DrawDropDownButton(Graphics g, Rectangle bounds, ComboBoxState state)
 {
     using var hdc = new DeviceContextHdcScope(g);
     DrawDropDownButtonForHandle(hdc, bounds, state, IntPtr.Zero);
 }
コード例 #17
0
 /// <summary>
 ///  Draws lines with the <paramref name="hpen"/> using points defined in <paramref name="lines"/>.
 /// </summary>
 /// <param name="lines">
 ///  MUST be a mulitple of 4. Each group of 4 represents x1, y1, x2, y2.
 /// </param>
 internal unsafe static void DrawLines(this DeviceContextHdcScope hdc, Gdi32.HPEN hpen, ReadOnlySpan <int> lines)
 => DrawLines(hdc.HDC, hpen, lines);
コード例 #18
0
 public static Size GetGlyphSize(Graphics g, RadioButtonState state)
 {
     using var hdc = new DeviceContextHdcScope(g);
     return(GetGlyphSize(hdc, state, IntPtr.Zero));
 }
コード例 #19
0
 internal static void DrawLine(this DeviceContextHdcScope hdc, Gdi32.HPEN hpen, Point p1, Point p2)
 => DrawLine(hdc.HDC, hpen, p1.X, p1.Y, p2.X, p2.Y);
コード例 #20
0
 internal unsafe static void DrawLine(this DeviceContextHdcScope hdc, Gdi32.HPEN hpen, int x1, int y1, int x2, int y2)
 => DrawLine(hdc.HDC, hpen, x1, y1, x2, y2);
コード例 #21
0
 internal static void FillRectangle(this DeviceContextHdcScope hdc, Rectangle rectangle, Gdi32.HBRUSH hbrush)
 => FillRectangle(hdc.HDC, rectangle, hbrush);
コード例 #22
0
 internal static void DrawRectangle(this DeviceContextHdcScope hdc, Rectangle rectangle, Gdi32.HPEN hpen)
 => DrawRectangle(hdc.HDC, rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom, hpen);
コード例 #23
0
 internal static void DrawAndFillEllipse(
     this DeviceContextHdcScope hdc,
     Gdi32.HPEN pen,
     Gdi32.HBRUSH brush,
     Rectangle bounds)
 => DrawAndFillEllipse(hdc.HDC, pen, brush, bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
コード例 #24
0
 internal static Color FindNearestColor(this DeviceContextHdcScope hdc, Color color)
 => FindNearestColor(hdc.HDC, color);
コード例 #25
0
ファイル: GroupBoxRenderer.cs プロジェクト: echalone/winforms
        private static void DrawUnthemedGroupBoxWithText(
            IDeviceContext deviceContext,
            Rectangle bounds,
            string groupBoxText,
            Font font,
            Color textColor,
            TextFormatFlags flags)
        {
            // Calculate text area, and render text inside it
            Rectangle textBounds = bounds;

            textBounds.Width -= TextOffset;
            Size measuredBounds = TextRenderer.MeasureText(
                deviceContext,
                groupBoxText,
                font,
                new Size(textBounds.Width, textBounds.Height),
                flags);

            textBounds.Width  = measuredBounds.Width;
            textBounds.Height = measuredBounds.Height;

            if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right)
            {
                textBounds.X = bounds.Right - textBounds.Width - TextOffset;
            }
            else
            {
                textBounds.X += TextOffset;
            }

            TextRenderer.DrawText(deviceContext, groupBoxText, font, textBounds, textColor, flags);

            // Pad text area to stop background from touching text
            if (textBounds.Width > 0)
            {
                textBounds.Inflate(2, 0);
            }

            int boxTop = bounds.Top + font.Height / 2;

            using var hdc = new DeviceContextHdcScope(deviceContext);

            ReadOnlySpan <int> darkLines = stackalloc int[]
            {
                bounds.Left, boxTop - 1, bounds.Left, bounds.Height - 2,                            // Left
                bounds.Left, bounds.Height - 2, bounds.Width - 1, bounds.Height - 2,                // Right
                bounds.Left, boxTop - 1, textBounds.X - 3, boxTop - 1,                              // Top-left
                textBounds.X + textBounds.Width + 2, boxTop - 1, bounds.Width - 2, boxTop - 1,      // Top-right
                bounds.Width - 2, boxTop - 1, bounds.Width - 2, bounds.Height - 2                   // Right
            };

            using var hpenDark = new Gdi32.CreatePenScope(SystemColors.ControlDark);
            hdc.DrawLines(hpenDark, darkLines);

            ReadOnlySpan <int> lightLines = stackalloc int[]
            {
                bounds.Left + 1, boxTop, bounds.Left + 1, bounds.Height - 1,                        // Left
                bounds.Left, bounds.Height - 1, bounds.Width, bounds.Height - 1,                    // Right
                bounds.Left + 1, boxTop, textBounds.X - 2, boxTop,                                  // Top-left
                textBounds.X + textBounds.Width + 1, boxTop, bounds.Width - 1, boxTop,              // Top-right
                bounds.Width - 1, boxTop, bounds.Width - 1, bounds.Height - 1                       // Right
            };

            using var hpenLight = new Gdi32.CreatePenScope(SystemColors.ControlLight);
            hdc.DrawLines(hpenLight, lightLines);
        }
コード例 #26
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 != null);

            Point ptCurrentCell = DataGridView.CurrentCellAddress;
            bool  cellSelected  = (elementState & DataGridViewElementStates.Selected) != 0;
            bool  cellCurrent   = (ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == rowIndex);

            Rectangle resultBounds;
            string    formattedString = formattedValue as string;

            SolidBrush backBrush = DataGridView.GetCachedBrush((PaintSelectionBackground(paintParts) && cellSelected) ? cellStyle.SelectionBackColor : cellStyle.BackColor);
            SolidBrush foreBrush = DataGridView.GetCachedBrush(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) && backBrush.Color.A == 255)
            {
                g.FillRectangle(backBrush, valBounds);
            }

            if (cellStyle.Padding != Padding.Empty)
            {
                if (DataGridView.RightToLeftInternal)
                {
                    valBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
                }
                else
                {
                    valBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top);
                }
                valBounds.Width  -= cellStyle.Padding.Horizontal;
                valBounds.Height -= cellStyle.Padding.Vertical;
            }

            Rectangle errorBounds = valBounds;

            if (valBounds.Height > 0 && valBounds.Width > 0 && (paint || computeContentBounds))
            {
                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, foreBrush.Color, true /*isDefault == true*/);

                        if (backBrush.Color.A == 255)
                        {
                            if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0)
                            {
                                ButtonBaseAdapter.ColorData colors = ButtonBaseAdapter.PaintFlatRender(
                                    g,
                                    cellStyle.ForeColor,
                                    cellStyle.BackColor,
                                    DataGridView.Enabled).Calculate();

                                using var hdc    = new DeviceContextHdcScope(g);
                                using var hbrush = new Gdi32.CreateBrushScope(
                                          colors.options.HighContrast ? colors.buttonShadow : colors.lowHighlight);
                                hdc.FillRectangle(valBounds, hbrush);
                            }
                            else if (DataGridView.MouseEnteredCellAddress.Y == rowIndex &&
                                     DataGridView.MouseEnteredCellAddress.X == ColumnIndex && mouseInContentBounds)
                            {
                                using var hdc    = new DeviceContextHdcScope(g);
                                using var hbrush = new Gdi32.CreateBrushScope(SystemColors.ControlDark);
                                hdc.FillRectangle(valBounds, hbrush);
                            }
                        }
                    }

                    resultBounds = valBounds;
                    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.DrawBorderSolid(
                                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.everettButtonCompat = 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.everettButtonCompat = false;
                        ButtonBaseAdapter.LayoutData layout = options.Layout();

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

            if (formattedString != null && paint && DataGridViewCell.PaintContentForeground(paintParts))
            {
                // Font independent margins
                valBounds.Offset(DATAGRIDVIEWBUTTONCELL_horizontalTextMargin, DATAGRIDVIEWBUTTONCELL_verticalTextMargin);
                valBounds.Width  -= 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin;
                valBounds.Height -= 2 * DATAGRIDVIEWBUTTONCELL_verticalTextMargin;

                if ((ButtonState & (ButtonState.Pushed | ButtonState.Checked)) != 0 &&
                    FlatStyle != FlatStyle.Flat && FlatStyle != FlatStyle.Popup)
                {
                    valBounds.Offset(1, 1);
                    valBounds.Width--;
                    valBounds.Height--;
                }

                if (valBounds.Width > 0 && valBounds.Height > 0)
                {
                    Color textColor;
                    if (DataGridView.ApplyVisualStylesToInnerCells &&
                        (FlatStyle == FlatStyle.System || FlatStyle == FlatStyle.Standard))
                    {
                        textColor = DataGridViewButtonCellRenderer.DataGridViewButtonRenderer.GetColor(ColorProperty.TextColor);
                    }
                    else
                    {
                        textColor = foreBrush.Color;
                    }
                    TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);
                    TextRenderer.DrawText(g,
                                          formattedString,
                                          cellStyle.Font,
                                          valBounds,
                                          textColor,
                                          flags);
                }
            }

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

            return(resultBounds);
        }
コード例 #27
0
 internal static void DrawRectangle(this DeviceContextHdcScope hdc, Rectangle rectangle, Gdi32.HPEN hpen)
 => DrawRectangle(hdc.HDC, rectangle, hpen);