Пример #1
0
        private void PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds, bool up)
        {
            PushButtonState pbState = DetermineState(up);

            // First handle transparent case

            if (ButtonRenderer.IsBackgroundPartiallyTransparent(pbState))
            {
                ButtonRenderer.DrawParentBackground(e, bounds, Control);
            }

            ButtonRenderer.DrawButtonForHandle(
                e,
                Control.ClientRectangle,
                false,
                pbState,
                DpiHelper.IsScalingRequirementMet ? Control.HandleInternal : IntPtr.Zero);

            // Now overlay the background image or backcolor (the former overrides the latter), leaving a margin.
            // We hardcode this margin for now since GetThemeMargins returns 0 all the time.
            //
            // Changing this because GetThemeMargins simply does not work in some cases.
            bounds.Inflate(-ButtonBorderSize, -ButtonBorderSize);

            //only paint if the user said not to use the themed backcolor.
            if (!Control.UseVisualStyleBackColor)
            {
                bool  isHighContrastHighlighted = up && IsHighContrastHighlighted();
                Color color = isHighContrastHighlighted ? SystemColors.Highlight : Control.BackColor;

                if (color.HasTransparency())
                {
                    using Brush brush = new SolidBrush(color);
                    e.GraphicsInternal.FillRectangle(brush, bounds);
                }
                else
                {
                    using var hdc = new DeviceContextHdcScope(e);
                    hdc.FillRectangle(
                        bounds,
                        isHighContrastHighlighted
                            ? User32.GetSysColorBrush(ColorTranslator.ToOle(color) & 0xFF)
                            : Control.BackColorBrush);
                }
            }

            // This code is mostly taken from the non-themed rendering code path.
            if (Control.BackgroundImage != null && !DisplayInformation.HighContrast)
            {
                ControlPaint.DrawBackgroundImage(
                    e.GraphicsInternal,
                    Control.BackgroundImage,
                    Color.Transparent,
                    Control.BackgroundImageLayout,
                    Control.ClientRectangle,
                    bounds,
                    Control.DisplayRectangle.Location,
                    Control.RightToLeft);
            }
        }
        private void PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds, bool up)
        {
            PushButtonState pbState = DetermineState(up);

            // First handle transparent case

            if (ButtonRenderer.IsBackgroundPartiallyTransparent(pbState))
            {
                ButtonRenderer.DrawParentBackground(e.Graphics, bounds, Control);
            }

            // Now draw the actual themed background
            if (!DpiHelper.IsScalingRequirementMet)
            {
                ButtonRenderer.DrawButton(e.Graphics, Control.ClientRectangle, false, pbState);
            }
            else
            {
                ButtonRenderer.DrawButtonForHandle(e.Graphics, Control.ClientRectangle, false, pbState, Control.HandleInternal);
            }

            // Now overlay the background image or backcolor (the former overrides the latter), leaving a
            // margin. We hardcode this margin for now since GetThemeMargins returns 0 all the
            // time.
            // Changing this because GetThemeMargins simply does not
            // work in some cases.
            bounds.Inflate(-buttonBorderSize, -buttonBorderSize);

            //only paint if the user said not to use the themed backcolor.
            if (!Control.UseVisualStyleBackColor)
            {
                bool  painted = false;
                bool  isHighContrastHighlighted = up && IsHighContrastHighlighted();
                Color color = isHighContrastHighlighted ? SystemColors.Highlight : Control.BackColor;

                // Note: PaintEvent.HDC == 0 if GDI+ has used the HDC -- it wouldn't be safe for us
                // to use it without enough bookkeeping to negate any performance gain of using GDI.
                if (color.A == 255 && !e.HDC.IsNull)
                {
                    if (DisplayInformation.BitsPerPixel > 8)
                    {
                        var r = new RECT(bounds.X, bounds.Y, bounds.Right, bounds.Bottom);

                        // SysColorBrush does not have to be deleted.
                        User32.FillRect(
                            e,
                            ref r,
                            isHighContrastHighlighted ? User32.GetSysColorBrush(ColorTranslator.ToOle(color) & 0xFF) : Control.BackColorBrush);
                        painted = true;
                    }
                }

                if (!painted)
                {
                    // don't paint anything from 100% transparent background
                    //
                    if (color.A > 0)
                    {
                        if (color.A == 255)
                        {
                            color = e.Graphics.GetNearestColor(color);
                        }

                        // Color has some transparency or we have no HDC, so we must
                        // fall back to using GDI+.
                        //
                        using (Brush brush = new SolidBrush(color))
                        {
                            e.Graphics.FillRectangle(brush, bounds);
                        }
                    }
                }
            }

            //This code is mostly taken from the non-themed rendering code path.
            if (Control.BackgroundImage != null && !DisplayInformation.HighContrast)
            {
                ControlPaint.DrawBackgroundImage(e.Graphics, Control.BackgroundImage, Color.Transparent, Control.BackgroundImageLayout, Control.ClientRectangle, bounds, Control.DisplayRectangle.Location, Control.RightToLeft);
            }
        }