Beispiel #1
0
        private static void DrawTextInternal(
            IDeviceContext dc,
            string?text,
            Font?font,
            Rectangle bounds,
            Color foreColor,
            Color backColor = default,
            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 (string.IsNullOrEmpty(text) || 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, applyGraphicsState: false);
            using WindowsGraphics wg = WindowsGraphics.FromHdc(hdc);
            using WindowsFont? wf    = WindowsGraphicsCacheManager.GetWindowsFont(font, quality);
            wg.DrawText(text, wf, bounds, foreColor, backColor, flags);
        }
Beispiel #2
0
        public static Size MeasureText(IDeviceContext dc, string text, Font font, Size proposedSize)
        {
            if (dc == null)
            {
                throw new ArgumentNullException("dc");
            }
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);

            IntPtr hdc = dc.GetHdc();

            try
            {
                using (WindowsGraphics wg = WindowsGraphics.FromHdc(hdc))
                {
                    using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) {
                        return(wg.MeasureText(text, wf, proposedSize));
                    }
                }
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
Beispiel #3
0
        /// <include file='doc\TextRenderer.uex' path='docs/doc[@for="TextRenderer.DrawText1"]/*' />
        public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor)
        {
            if (dc == null)
            {
                throw new ArgumentNullException("dc");
            }

            WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);

            IntPtr hdc = dc.GetHdc();

            try
            {
                using (WindowsGraphics wg = WindowsGraphics.FromHdc(hdc))
                {
                    using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) {
                        wg.DrawText(text, wf, pt, foreColor, backColor);
                    }
                }
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
Beispiel #4
0
        public static Size MeasureText(IDeviceContext dc, string?text, Font?font)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }
            if (string.IsNullOrEmpty(text))
            {
                return(Size.Empty);
            }

            Gdi32.QUALITY fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);

            IntPtr hdc = dc.GetHdc();

            try
            {
                using WindowsGraphics wg = WindowsGraphics.FromHdc(hdc);
                using WindowsFont? wf    = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality);
                return(wg.MeasureText(text, wf));
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
Beispiel #5
0
        public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            Gdi32.QUALITY fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics);

            IntPtr hdc = dc.GetHdc();

            try
            {
                using (WindowsGraphics wg = WindowsGraphics.FromHdc(hdc))
                {
                    using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality))
                    {
                        wg.DrawText(text, wf, bounds, foreColor);
                    }
                }
            }
            finally
            {
                dc.ReleaseHdc();
            }
        }
Beispiel #6
0
        public WindowsGraphicsWrapper(IDeviceContext idc, TextFormatFlags flags)
        {
            if (idc is Graphics)
            {
                ApplyGraphicsProperties properties = ApplyGraphicsProperties.None;

                if ((flags & TextFormatFlags.PreserveGraphicsClipping) != 0)
                {
                    properties |= ApplyGraphicsProperties.Clipping;
                }

                if ((flags & TextFormatFlags.PreserveGraphicsTranslateTransform) != 0)
                {
                    properties |= ApplyGraphicsProperties.TranslateTransform;
                }

                // Create the WindowsGraphics from the Grahpics object only if Graphics properties need
                // to be reapplied to the DC wrapped by the WindowsGraphics.
                if (properties != ApplyGraphicsProperties.None)
                {
                    wg = WindowsGraphics.FromGraphics(idc as Graphics, properties);
                }
            }
            else
            {
                // If passed-in IDeviceContext object is a WindowsGraphics we can use it directly.
                wg = idc as WindowsGraphics;

                if (wg != null)
                {
                    // In this case we cache the idc to compare it against the wg in the Dispose method to avoid
                    // disposing of the wg.
                    this.idc = idc;
                }
            }

            if (wg == null)
            {
                // The IDeviceContext object is not a WindowsGraphics, or it is a custom IDeviceContext, or
                // it is a Graphics object but we did not need to re-apply Graphics propertiesto the hdc.
                // So create the WindowsGraphics from the hdc directly.
                // Cache the IDC so the hdc can be released on dispose.
                this.idc = idc;
                wg       = WindowsGraphics.FromHdc(idc.GetHdc());
            }

            // Set text padding on the WindowsGraphics (if any).
            if ((flags & TextFormatFlags.LeftAndRightPadding) != 0)
            {
                wg.TextPadding = TextPaddingOptions.LeftAndRightPadding;
            }
            else if ((flags & TextFormatFlags.NoPadding) != 0)
            {
                wg.TextPadding = TextPaddingOptions.NoPadding;
            }
            // else wg.TextPadding = TextPaddingOptions.GlyphOverhangPadding - the default value.
        }
Beispiel #7
0
        public static WindowsGraphics FromGraphics(Graphics g, ApplyGraphicsProperties properties)
        {
            Debug.Assert(g != null, "null Graphics object.");
            //Debug.Assert( properties != ApplyGraphicsProperties.None, "Consider using other WindowsGraphics constructor if not preserving Graphics properties." );

            WindowsRegion wr = null;

            float[] elements = null;

            Region clipRgn     = null;
            Matrix worldTransf = null;

            if ((properties & ApplyGraphicsProperties.TranslateTransform) != 0 || (properties & ApplyGraphicsProperties.Clipping) != 0)
            {
                if (g.GetContextInfo() is object[] data && data.Length == 2)
                {
                    clipRgn     = data[0] as Region;
                    worldTransf = data[1] as Matrix;
                }

                if (worldTransf != null)
                {
                    if ((properties & ApplyGraphicsProperties.TranslateTransform) != 0)
                    {
                        elements = worldTransf.Elements;
                    }
                    worldTransf.Dispose();
                }

                if (clipRgn != null)
                {
                    if ((properties & ApplyGraphicsProperties.Clipping) != 0)
                    {
                        // We have to create the WindowsRegion and dipose the Region object before locking the Graphics object,
                        // in case of an unlikely exception before releasing the WindowsRegion, the finalizer will do it for us.
                        // (no try-finally block since this method is used frequently - perf).
                        // If the Graphics.Clip has not been set (Region.IsInfinite) we don't need to apply it to the DC.
                        if (!clipRgn.IsInfinite(g))
                        {
                            wr = WindowsRegion.FromRegion(clipRgn, g); // WindowsRegion will take ownership of the hRegion.
                        }
                    }
                    clipRgn.Dispose(); // Disposing the Region object doesn't destroy the hRegion.
                }
            }

            WindowsGraphics wg = WindowsGraphics.FromHdc(g.GetHdc()); // This locks the Graphics object.

            wg.graphics = g;

            // Apply transform and clip
            if (wr != null)
            {
                using (wr)
                {
                    // If the Graphics object was created from a native DC the actual clipping region is the intersection
                    // beteween the original DC clip region and the GDI+ one - for display Graphics it is the same as
                    // Graphics.VisibleClipBounds.
                    wg.DeviceContext.IntersectClip(wr);
                }
            }

            if (elements != null)
            {
                // elements (XFORM) = [eM11, eM12, eM21, eM22, eDx, eDy], eDx/eDy specify the translation offset.
                wg.DeviceContext.TranslateTransform((int)elements[4], (int)elements[5]);
            }

            return(wg);
        }
        public WindowsGraphicsWrapper(IDeviceContext deviceContext, TextFormatFlags flags)
        {
            if (deviceContext is Graphics)
            {
                ApplyGraphicsProperties properties = ApplyGraphicsProperties.None;

                if ((flags & TextFormatFlags.PreserveGraphicsClipping) != 0)
                {
                    properties |= ApplyGraphicsProperties.Clipping;
                }

                if ((flags & TextFormatFlags.PreserveGraphicsTranslateTransform) != 0)
                {
                    properties |= ApplyGraphicsProperties.TranslateTransform;
                }

                // Create the WindowsGraphics from the Grahpics object only if Graphics properties need
                // to be reapplied to the DC wrapped by the WindowsGraphics.
                if (properties != ApplyGraphicsProperties.None)
                {
                    try
                    {
                        _windowsGraphics = WindowsGraphics.FromGraphics(deviceContext as Graphics, properties);
                    }
                    catch
                    {
                        GC.SuppressFinalize(this);
                        throw;
                    }
                }
            }
            else
            {
                // If passed-in IDeviceContext object is a WindowsGraphics we can use it directly.
                _windowsGraphics = deviceContext as WindowsGraphics;

                if (_windowsGraphics != null)
                {
                    // In this case we cache the idc to compare it against the wg in the Dispose method to avoid
                    // disposing of the wg.
                    _deviceContext = deviceContext;
                }
            }

            if (_windowsGraphics == null)
            {
                // The IDeviceContext object is not a WindowsGraphics, or it is a custom IDeviceContext, or
                // it is a Graphics object but we did not need to re-apply Graphics propertiesto the hdc.
                // So create the WindowsGraphics from the hdc directly.
                // Cache the IDC so the hdc can be released ons dispose.
                try
                {
                    _deviceContext   = deviceContext;
                    _windowsGraphics = WindowsGraphics.FromHdc((Gdi32.HDC)deviceContext.GetHdc());
                }
                catch
                {
                    SuppressFinalize();
                    deviceContext.ReleaseHdc();
                    throw;
                }
            }

            // Set text padding on the WindowsGraphics (if any).
            if ((flags & TextFormatFlags.LeftAndRightPadding) != 0)
            {
                _windowsGraphics.TextPadding = TextPaddingOptions.LeftAndRightPadding;
            }
            else if ((flags & TextFormatFlags.NoPadding) != 0)
            {
                _windowsGraphics.TextPadding = TextPaddingOptions.NoPadding;
            }
            // else wg.TextPadding = TextPaddingOptions.GlyphOverhangPadding - the default value.
        }
        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);
        }
Beispiel #10
0
        protected override void OnPaint(PaintEventArgs e)
        {
            Color nearestColor;

            this.Animate();
            Rectangle r = LayoutUtils.DeflateRect(base.ClientRectangle, base.Padding);

            ImageAnimator.UpdateFrames();
            System.Drawing.Image image = this.Image;
            if (image != null)
            {
                this.DrawImage(e.Graphics, image, r, base.RtlTranslateAlignment(this.ImageAlign));
            }
            IntPtr hdc = e.Graphics.GetHdc();

            try
            {
                using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc))
                {
                    nearestColor = graphics.GetNearestColor(base.Enabled ? this.ForeColor : base.DisabledColor);
                }
            }
            finally
            {
                e.Graphics.ReleaseHdc();
            }
            if (this.AutoEllipsis)
            {
                Rectangle clientRectangle = base.ClientRectangle;
                Size      preferredSize   = this.GetPreferredSize(new Size(clientRectangle.Width, clientRectangle.Height));
                this.showToolTip = (clientRectangle.Width < preferredSize.Width) || (clientRectangle.Height < preferredSize.Height);
            }
            else
            {
                this.showToolTip = false;
            }
            if (this.UseCompatibleTextRendering)
            {
                using (StringFormat format = this.CreateStringFormat())
                {
                    if (base.Enabled)
                    {
                        using (Brush brush = new SolidBrush(nearestColor))
                        {
                            e.Graphics.DrawString(this.Text, this.Font, brush, r, format);
                            goto Label_01BF;
                        }
                    }
                    ControlPaint.DrawStringDisabled(e.Graphics, this.Text, this.Font, nearestColor, r, format);
                    goto Label_01BF;
                }
            }
            TextFormatFlags flags = this.CreateTextFormatFlags();

            if (base.Enabled)
            {
                TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, nearestColor, flags);
            }
            else
            {
                Color foreColor = TextRenderer.DisabledTextColor(this.BackColor);
                TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, foreColor, flags);
            }
Label_01BF:
            base.OnPaint(e);
        }