protected void DrawCheckBackgroundFlat(PaintEventArgs e, Rectangle bounds, Color borderColor, Color checkBackground, bool disabledColors)
        {
            Color control             = checkBackground;
            Color contrastControlDark = borderColor;

            if (!this.Control.Enabled && disabledColors)
            {
                contrastControlDark = ControlPaint.ContrastControlDark;
                control             = SystemColors.Control;
            }
            float dpiScaleRatio = CheckableControlBaseAdapter.GetDpiScaleRatio(e.Graphics);

            using (WindowsGraphics graphics = WindowsGraphics.FromGraphics(e.Graphics))
            {
                using (WindowsPen pen = new WindowsPen(graphics.DeviceContext, contrastControlDark))
                {
                    using (WindowsBrush brush = new WindowsSolidBrush(graphics.DeviceContext, control))
                    {
                        if (dpiScaleRatio > 1.1)
                        {
                            bounds.Width--;
                            bounds.Height--;
                            graphics.DrawAndFillEllipse(pen, brush, bounds);
                            bounds.Inflate(-1, -1);
                        }
                        else
                        {
                            DrawAndFillEllipse(graphics, pen, brush, bounds);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // used by DataGridViewCheckBoxCell
        internal static void DrawCheckBackground(bool controlEnabled, CheckState controlCheckState, Graphics g, Rectangle bounds, Color checkColor, Color checkBackground, bool disabledColors, ColorData colors)
        {
            using (WindowsGraphics wg = WindowsGraphics.FromGraphics(g)) {
                WindowsBrush brush;
                if (!controlEnabled && disabledColors)
                {
                    brush = new WindowsSolidBrush(wg.DeviceContext, SystemColors.Control);
                }
                else if (controlCheckState == CheckState.Indeterminate && checkBackground == SystemColors.Window && disabledColors)
                {
                    Color comboColor = SystemInformation.HighContrast ? SystemColors.ControlDark :
                                       SystemColors.Control;
                    byte R = (byte)((comboColor.R + SystemColors.Window.R) / 2);
                    byte G = (byte)((comboColor.G + SystemColors.Window.G) / 2);
                    byte B = (byte)((comboColor.B + SystemColors.Window.B) / 2);
                    brush = new WindowsSolidBrush(wg.DeviceContext, Color.FromArgb(R, G, B));
                }
                else
                {
                    brush = new WindowsSolidBrush(wg.DeviceContext, checkBackground);
                }

                try {
                    wg.FillRectangle(brush, bounds);
                }
                finally {
                    if (brush != null)
                    {
                        brush.Dispose();
                    }
                }
            }
        }
        protected void DrawCheckBackgroundFlat(PaintEventArgs e, Rectangle bounds, Color borderColor, Color checkBackground, bool disabledColors)
        {
            Color field  = checkBackground;
            Color border = borderColor;

            if (!Control.Enabled && disabledColors)
            {
                border = ControlPaint.ContrastControlDark;
                field  = SystemColors.Control;
            }

            float scale = GetDpiScaleRatio(e.Graphics);

            using (WindowsGraphics wg = WindowsGraphics.FromGraphics(e.Graphics)) {
                using (WindowsPen borderPen = new WindowsPen(wg.DeviceContext, border)) {
                    using (WindowsBrush fieldBrush = new WindowsSolidBrush(wg.DeviceContext, field)) {
                        // for Dev10 525537, in high DPI mode when we draw ellipse as three rectantles,
                        // the quality of ellipse is poor. Draw it directly as ellipse
                        if (scale > 1.1)
                        {
                            bounds.Width--;
                            bounds.Height--;
                            wg.DrawAndFillEllipse(borderPen, fieldBrush, bounds);
                            bounds.Inflate(-1, -1);
                        }
                        else
                        {
                            DrawAndFillEllipse(wg, borderPen, fieldBrush, bounds);
                        }
                    }
                }
            }
        }
        protected void DrawCheckOnly(PaintEventArgs e, LayoutData layout, Color checkColor, Color checkBackground, bool disabledColors)
        {
            // check
            //
            if (Control.Checked)
            {
                if (!Control.Enabled && disabledColors)
                {
                    checkColor = SystemColors.ControlDark;
                }

                double scale = GetDpiScaleRatio(e.Graphics);
                using (WindowsGraphics wg = WindowsGraphics.FromGraphics(e.Graphics))
                {
                    using (WindowsBrush brush = new WindowsSolidBrush(wg.DeviceContext, checkColor))
                    {
                        // circle drawing doesn't work at this size
                        int       offset = 5;
                        Rectangle vCross = new Rectangle(layout.checkBounds.X + GetScaledNumber(offset, scale), layout.checkBounds.Y + GetScaledNumber(offset - 1, scale), GetScaledNumber(2, scale), GetScaledNumber(4, scale));
                        wg.FillRectangle(brush, vCross);
                        Rectangle hCross = new Rectangle(layout.checkBounds.X + GetScaledNumber(offset - 1, scale), layout.checkBounds.Y + GetScaledNumber(offset, scale), GetScaledNumber(4, scale), GetScaledNumber(2, scale));
                        wg.FillRectangle(brush, hCross);
                    }
                }
            }
        }
 internal static void DrawCheckBackground(bool controlEnabled, CheckState controlCheckState, Graphics g, Rectangle bounds, Color checkColor, Color checkBackground, bool disabledColors, ButtonBaseAdapter.ColorData colors)
 {
     using (WindowsGraphics graphics = WindowsGraphics.FromGraphics(g))
     {
         WindowsBrush brush;
         if (!controlEnabled && disabledColors)
         {
             brush = new WindowsSolidBrush(graphics.DeviceContext, SystemColors.Control);
         }
         else if (((controlCheckState == CheckState.Indeterminate) && (checkBackground == SystemColors.Window)) && disabledColors)
         {
             Color color = SystemInformation.HighContrast ? SystemColors.ControlDark : SystemColors.Control;
             byte  red   = (byte)((color.R + SystemColors.Window.R) / 2);
             byte  green = (byte)((color.G + SystemColors.Window.G) / 2);
             byte  blue  = (byte)((color.B + SystemColors.Window.B) / 2);
             brush = new WindowsSolidBrush(graphics.DeviceContext, Color.FromArgb(red, green, blue));
         }
         else
         {
             brush = new WindowsSolidBrush(graphics.DeviceContext, checkBackground);
         }
         try
         {
             graphics.FillRectangle(brush, bounds);
         }
         finally
         {
             if (brush != null)
             {
                 brush.Dispose();
             }
         }
     }
 }
Ejemplo n.º 6
0
        protected void DrawCheckFlat(PaintEventArgs e, LayoutData layout, Color checkColor, Color checkBackground, Color checkBorder, ColorData colors)
        {
            Rectangle bounds = layout.checkBounds;

            // Removed subtracting one for Width and Height. In Everett we needed to do this,
            // since we were using GDI+ to draw the border. Now that we are using GDI,
            // we should not do before drawing the border.

            if (!layout.options.everettButtonCompat)
            {
                bounds.Width--;
                bounds.Height--;
            }
            using (WindowsGraphics wg = WindowsGraphics.FromGraphics(e.Graphics))
            {
                using (WindowsPen pen = new WindowsPen(wg.DeviceContext, checkBorder))
                {
                    wg.DrawRectangle(pen, bounds);
                }

                // Now subtract, since the rest of the code is like Everett.
                if (layout.options.everettButtonCompat)
                {
                    bounds.Width--;
                    bounds.Height--;
                }
                bounds.Inflate(-1, -1);
            }
            if (Control.CheckState == CheckState.Indeterminate)
            {
                bounds.Width++;
                bounds.Height++;
                DrawDitheredFill(e.Graphics, colors.buttonFace, checkBackground, bounds);
            }
            else
            {
                using (WindowsGraphics wg = WindowsGraphics.FromGraphics(e.Graphics))
                {
                    using (WindowsBrush brush = new WindowsSolidBrush(wg.DeviceContext, checkBackground))
                    {
                        // Even though we are using GDI here as opposed to GDI+ in Everett, we still need to add 1.
                        bounds.Width++;
                        bounds.Height++;
                        wg.FillRectangle(brush, bounds);
                    }
                }
            }
            DrawCheckOnly(e, layout, colors, checkColor, checkBackground);
        }
Ejemplo n.º 7
0
        // Draws this image to a graphics object.  The drawing command originates on the graphics
        // object, but a graphics object generally has no idea how to render a given image.  So,
        // it passes the call to the actual image.  This version stretches the image to the given
        // dimensions and allows the user to specify a rectangle within the image to draw.
        internal void Draw(Graphics graphics, Rectangle targetRect)
        {
            Rectangle copy = targetRect;

            using Matrix transform = graphics.Transform;
            PointF offset = transform.Offset;

            copy.X += (int)offset.X;
            copy.Y += (int)offset.Y;

            using (WindowsGraphics wg = WindowsGraphics.FromGraphics(graphics, ApplyGraphicsProperties.Clipping))
            {
                IntPtr dc = wg.GetHdc();
                DrawIcon(dc, Rectangle.Empty, copy, true);
            }
        }
Ejemplo n.º 8
0
        internal void DrawUnstretched(Graphics graphics, Rectangle targetRect)
        {
            Rectangle rectangle = targetRect;

            rectangle.X += (int)graphics.Transform.OffsetX;
            rectangle.Y += (int)graphics.Transform.OffsetY;
            WindowsGraphics graphics2 = WindowsGraphics.FromGraphics(graphics, ApplyGraphicsProperties.Clipping);
            IntPtr          hdc       = graphics2.GetHdc();

            try
            {
                this.DrawIcon(hdc, Rectangle.Empty, rectangle, false);
            }
            finally
            {
                graphics2.Dispose();
            }
        }
Ejemplo n.º 9
0
        internal static Rectangle DrawPopupBorder(Graphics g, Rectangle r, ColorData colors)
        {
            using (WindowsGraphics wg = WindowsGraphics.FromGraphics(g)) {
                using (WindowsPen high = new WindowsPen(wg.DeviceContext, colors.highlight),
                       shadow = new WindowsPen(wg.DeviceContext, colors.buttonShadow),
                       face = new WindowsPen(wg.DeviceContext, colors.buttonFace)) {
                    wg.DrawLine(high, r.Right - 1, r.Top, r.Right - 1, r.Bottom);
                    wg.DrawLine(high, r.Left, r.Bottom - 1, r.Right, r.Bottom - 1);

                    wg.DrawLine(shadow, r.Left, r.Top, r.Left, r.Bottom);
                    wg.DrawLine(shadow, r.Left, r.Top, r.Right - 1, r.Top);

                    wg.DrawLine(face, r.Right - 2, r.Top + 1, r.Right - 2, r.Bottom - 1);
                    wg.DrawLine(face, r.Left + 1, r.Bottom - 2, r.Right - 1, r.Bottom - 2);
                }
            }
            r.Inflate(-1, -1);
            return(r);
        }
        protected void DrawCheckFlat(PaintEventArgs e, ButtonBaseAdapter.LayoutData layout, Color checkColor, Color checkBackground, Color checkBorder, ButtonBaseAdapter.ColorData colors)
        {
            Rectangle checkBounds = layout.checkBounds;

            if (!layout.options.everettButtonCompat)
            {
                checkBounds.Width--;
                checkBounds.Height--;
            }
            using (WindowsGraphics graphics = WindowsGraphics.FromGraphics(e.Graphics))
            {
                using (WindowsPen pen = new WindowsPen(graphics.DeviceContext, checkBorder))
                {
                    graphics.DrawRectangle(pen, checkBounds);
                }
                if (layout.options.everettButtonCompat)
                {
                    checkBounds.Width--;
                    checkBounds.Height--;
                }
                checkBounds.Inflate(-1, -1);
            }
            if (this.Control.CheckState == CheckState.Indeterminate)
            {
                checkBounds.Width++;
                checkBounds.Height++;
                ButtonBaseAdapter.DrawDitheredFill(e.Graphics, colors.buttonFace, checkBackground, checkBounds);
            }
            else
            {
                using (WindowsGraphics graphics2 = WindowsGraphics.FromGraphics(e.Graphics))
                {
                    using (WindowsBrush brush = new WindowsSolidBrush(graphics2.DeviceContext, checkBackground))
                    {
                        checkBounds.Width++;
                        checkBounds.Height++;
                        graphics2.FillRectangle(brush, checkBounds);
                    }
                }
            }
            this.DrawCheckOnly(e, layout, colors, checkColor, checkBackground, true);
        }
        protected void DrawCheckBackgroundFlat(PaintEventArgs e, Rectangle bounds, Color borderColor, Color checkBackground)
        {
            Color field  = checkBackground;
            Color border = borderColor;

            if (!Control.Enabled)
            {
                // if we are not in HighContrast mode OR we opted into the legacy behavior
                if (!SystemInformation.HighContrast)
                {
                    border = ControlPaint.ContrastControlDark;
                }
                // otherwise we are in HighContrast mode
                field = SystemColors.Control;
            }

            double scale = GetDpiScaleRatio(e.Graphics);

            using (WindowsGraphics wg = WindowsGraphics.FromGraphics(e.Graphics))
            {
                using (WindowsPen borderPen = new WindowsPen(wg.DeviceContext, border))
                {
                    using (WindowsBrush fieldBrush = new WindowsSolidBrush(wg.DeviceContext, field))
                    {
                        // In high DPI mode when we draw ellipse as three rectantles,
                        // the quality of ellipse is poor. Draw it directly as ellipse
                        if (scale > 1.1)
                        {
                            bounds.Width--;
                            bounds.Height--;
                            wg.DrawAndFillEllipse(borderPen, fieldBrush, bounds);
                            bounds.Inflate(-1, -1);
                        }
                        else
                        {
                            DrawAndFillEllipse(wg, borderPen, fieldBrush, bounds);
                        }
                    }
                }
            }
        }
 protected void DrawCheckOnly(PaintEventArgs e, ButtonBaseAdapter.LayoutData layout, Color checkColor, Color checkBackground, bool disabledColors)
 {
     if (this.Control.Checked)
     {
         if (!this.Control.Enabled && disabledColors)
         {
             checkColor = SystemColors.ControlDark;
         }
         float dpiScaleRatio = CheckableControlBaseAdapter.GetDpiScaleRatio(e.Graphics);
         using (WindowsGraphics graphics = WindowsGraphics.FromGraphics(e.Graphics))
         {
             using (WindowsBrush brush = new WindowsSolidBrush(graphics.DeviceContext, checkColor))
             {
                 int       n    = 5;
                 Rectangle rect = new Rectangle(layout.checkBounds.X + GetScaledNumber(n, dpiScaleRatio), layout.checkBounds.Y + GetScaledNumber(n - 1, dpiScaleRatio), GetScaledNumber(2, dpiScaleRatio), GetScaledNumber(4, dpiScaleRatio));
                 graphics.FillRectangle(brush, rect);
                 Rectangle rectangle2 = new Rectangle(layout.checkBounds.X + GetScaledNumber(n - 1, dpiScaleRatio), layout.checkBounds.Y + GetScaledNumber(n, dpiScaleRatio), GetScaledNumber(4, dpiScaleRatio), GetScaledNumber(2, dpiScaleRatio));
                 graphics.FillRectangle(brush, rectangle2);
             }
         }
     }
 }
 internal static Rectangle DrawPopupBorder(Graphics g, Rectangle r, ButtonBaseAdapter.ColorData colors)
 {
     using (WindowsGraphics graphics = WindowsGraphics.FromGraphics(g))
     {
         using (WindowsPen pen = new WindowsPen(graphics.DeviceContext, colors.highlight))
         {
             using (WindowsPen pen2 = new WindowsPen(graphics.DeviceContext, colors.buttonShadow))
             {
                 using (WindowsPen pen3 = new WindowsPen(graphics.DeviceContext, colors.buttonFace))
                 {
                     graphics.DrawLine(pen, r.Right - 1, r.Top, r.Right - 1, r.Bottom);
                     graphics.DrawLine(pen, r.Left, r.Bottom - 1, r.Right, r.Bottom - 1);
                     graphics.DrawLine(pen2, r.Left, r.Top, r.Left, r.Bottom);
                     graphics.DrawLine(pen2, r.Left, r.Top, r.Right - 1, r.Top);
                     graphics.DrawLine(pen3, r.Right - 2, r.Top + 1, r.Right - 2, r.Bottom - 1);
                     graphics.DrawLine(pen3, r.Left + 1, r.Bottom - 2, r.Right - 1, r.Bottom - 2);
                 }
             }
         }
     }
     r.Inflate(-1, -1);
     return(r);
 }
Ejemplo n.º 14
0
        /// <summary>
        ///     Creates a WindowsGraphics object from a Graphics object.  Clipping and coordinate transforms
        ///     are preserved.
        ///
        ///     Notes:
        ///     - The passed Graphics object cannot be used until the WindowsGraphics is disposed
        ///     since it borrows the hdc from the Graphics object locking it.
        ///     - Changes to the hdc using the WindowsGraphics object are not preserved into the Graphics object;
        ///     the hdc is returned to the Graphics object intact.
        ///
        ///     Some background about how Graphics uses the internal hdc when created from an existing one
        ///     (mail from GillesK from GDI+ team):
        ///     User has an HDC with a particular state:
        ///     Graphics object gets created based on that HDC. We query the HDC for its state and apply it to the Graphics.
        ///     At this stage, we do a SaveHDC and clear everything out of it.
        ///     User calls GetHdc. We restore the HDC to the state it was in and give it to the user.
        ///     User calls ReleaseHdc, we save the current state of the HDC and clear everything
        ///     (so that the graphics state gets applied next time we use it).
        ///     Next time the user calls GetHdc we give him back the state after the second ReleaseHdc.
        ///     (But the state changes between the GetHdc and ReleaseHdc are not applied to the Graphics).
        ///     Please note that this only applies the HDC created graphics, for Bitmap derived graphics, GetHdc creates a new DIBSection and
        ///     things get a lot more complicated.
        /// </summary>


        public static WindowsGraphics FromGraphics(Graphics g)
        {
            ApplyGraphicsProperties properties = ApplyGraphicsProperties.All;

            return(WindowsGraphics.FromGraphics(g, properties));
        }
Ejemplo n.º 15
0
        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.
        }