Beispiel #1
0
        public override void RenderBorder(Graphics g, Rectangle borderRectangle)
        {
            Color borderColor = !ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled ? BorderColor.ToGrayScale() : BorderColor;

            g.SetClip(borderRectangle);

            using (Pen borderPen = new Pen(borderColor))
            {
                g.DrawRectangle(borderPen, borderRectangle.X, borderRectangle.Y, borderRectangle.Width - 1, borderRectangle.Height - 1);
            }

            g.ResetClip();
        }
        public override void RenderButton(Graphics g, Rectangle buttonRectangle)
        {
            if (ToggleSwitch.IsButtonOnLeftSide)
            {
                buttonRectangle.X += 1;
            }
            else if (ToggleSwitch.IsButtonOnRightSide)
            {
                buttonRectangle.X -= 1;
            }

            g.SmoothingMode   = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            //Draw button shadow
            buttonRectangle.Inflate(1, 1);

            Rectangle shadowClipRectangle = new Rectangle(buttonRectangle.Location, buttonRectangle.Size);

            shadowClipRectangle.Inflate(0, -1);

            if (ToggleSwitch.IsButtonOnLeftSide)
            {
                shadowClipRectangle.X    += shadowClipRectangle.Width / 2;
                shadowClipRectangle.Width = shadowClipRectangle.Width / 2;
            }
            else if (ToggleSwitch.IsButtonOnRightSide)
            {
                shadowClipRectangle.Width = shadowClipRectangle.Width / 2;
            }

            g.SetClip(shadowClipRectangle);

            Color buttonShadowColor = (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled) ? ButtonShadowColor.ToGrayScale() : ButtonShadowColor;

            using (Pen buttonShadowPen = new Pen(buttonShadowColor))
            {
                g.DrawEllipse(buttonShadowPen, buttonRectangle);
            }

            g.ResetClip();

            buttonRectangle.Inflate(-1, -1);

            //Draw outer button border
            Color buttonOuterBorderColor = ButtonNormalOuterBorderColor;

            if (ToggleSwitch.IsButtonPressed)
            {
                buttonOuterBorderColor = ButtonPressedOuterBorderColor;
            }
            else if (ToggleSwitch.IsButtonHovered)
            {
                buttonOuterBorderColor = ButtonHoverOuterBorderColor;
            }

            if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
            {
                buttonOuterBorderColor = buttonOuterBorderColor.ToGrayScale();
            }

            using (Brush outerBorderBrush = new SolidBrush(buttonOuterBorderColor))
            {
                g.FillEllipse(outerBorderBrush, buttonRectangle);
            }

            //Draw inner button border
            buttonRectangle.Inflate(-1, -1);

            Color buttonInnerBorderColor = ButtonNormalInnerBorderColor;

            if (ToggleSwitch.IsButtonPressed)
            {
                buttonInnerBorderColor = ButtonPressedInnerBorderColor;
            }
            else if (ToggleSwitch.IsButtonHovered)
            {
                buttonInnerBorderColor = ButtonHoverInnerBorderColor;
            }

            if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
            {
                buttonInnerBorderColor = buttonInnerBorderColor.ToGrayScale();
            }

            using (Brush innerBorderBrush = new SolidBrush(buttonInnerBorderColor))
            {
                g.FillEllipse(innerBorderBrush, buttonRectangle);
            }

            //Draw button surface
            buttonRectangle.Inflate(-1, -1);

            Color buttonUpperSurfaceColor = ButtonNormalSurfaceColor1;

            if (ToggleSwitch.IsButtonPressed)
            {
                buttonUpperSurfaceColor = ButtonPressedSurfaceColor1;
            }
            else if (ToggleSwitch.IsButtonHovered)
            {
                buttonUpperSurfaceColor = ButtonHoverSurfaceColor1;
            }

            if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
            {
                buttonUpperSurfaceColor = buttonUpperSurfaceColor.ToGrayScale();
            }

            Color buttonLowerSurfaceColor = ButtonNormalSurfaceColor2;

            if (ToggleSwitch.IsButtonPressed)
            {
                buttonLowerSurfaceColor = ButtonPressedSurfaceColor2;
            }
            else if (ToggleSwitch.IsButtonHovered)
            {
                buttonLowerSurfaceColor = ButtonHoverSurfaceColor2;
            }

            if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
            {
                buttonLowerSurfaceColor = buttonLowerSurfaceColor.ToGrayScale();
            }

            using (Brush buttonSurfaceBrush = new LinearGradientBrush(buttonRectangle, buttonUpperSurfaceColor, buttonLowerSurfaceColor, LinearGradientMode.Vertical))
            {
                g.FillEllipse(buttonSurfaceBrush, buttonRectangle);
            }

            g.CompositingMode    = CompositingMode.SourceOver;
            g.CompositingQuality = CompositingQuality.HighQuality;

            //Draw outer control border
            Rectangle controlRectangle = new Rectangle(0, 0, ToggleSwitch.Width, ToggleSwitch.Height);

            using (GraphicsPath borderPath = GetControlClipPath(controlRectangle))
            {
                Color controlBorderColor = (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled) ? BorderColor.ToGrayScale() : BorderColor;

                using (Pen borderPen = new Pen(controlBorderColor))
                {
                    g.DrawPath(borderPen, borderPath);
                }
            }

            g.ResetClip();

            //Draw button image
            Image buttonImage = ToggleSwitch.ButtonImage ?? (ToggleSwitch.Checked ? ToggleSwitch.OnButtonImage : ToggleSwitch.OffButtonImage);

            if (buttonImage != null)
            {
                g.SetClip(GetButtonClipPath());

                ToggleSwitch.ToggleSwitchButtonAlignment alignment = ToggleSwitch.ButtonImage != null ? ToggleSwitch.ButtonAlignment : (ToggleSwitch.Checked ? ToggleSwitch.OnButtonAlignment : ToggleSwitch.OffButtonAlignment);

                Size imageSize = buttonImage.Size;

                Rectangle imageRectangle;

                int imageXPos = buttonRectangle.X;

                bool scaleImage = ToggleSwitch.ButtonImage != null ? ToggleSwitch.ButtonScaleImageToFit : (ToggleSwitch.Checked ? ToggleSwitch.OnButtonScaleImageToFit : ToggleSwitch.OffButtonScaleImageToFit);

                if (scaleImage)
                {
                    Size canvasSize       = buttonRectangle.Size;
                    Size resizedImageSize = ImageHelper.RescaleImageToFit(imageSize, canvasSize);

                    if (alignment == ToggleSwitch.ToggleSwitchButtonAlignment.Center)
                    {
                        imageXPos = (int)((float)buttonRectangle.X + (((float)buttonRectangle.Width - (float)resizedImageSize.Width) / 2));
                    }
                    else if (alignment == ToggleSwitch.ToggleSwitchButtonAlignment.Right)
                    {
                        imageXPos = (int)((float)buttonRectangle.X + (float)buttonRectangle.Width - (float)resizedImageSize.Width);
                    }

                    imageRectangle = new Rectangle(imageXPos, (int)((float)buttonRectangle.Y + (((float)buttonRectangle.Height - (float)resizedImageSize.Height) / 2)), resizedImageSize.Width, resizedImageSize.Height);

                    if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
                    {
                        g.DrawImage(buttonImage, imageRectangle, 0, 0, buttonImage.Width, buttonImage.Height, GraphicsUnit.Pixel, ImageHelper.GetGrayscaleAttributes());
                    }
                    else
                    {
                        g.DrawImage(buttonImage, imageRectangle);
                    }
                }
                else
                {
                    if (alignment == ToggleSwitch.ToggleSwitchButtonAlignment.Center)
                    {
                        imageXPos = (int)((float)buttonRectangle.X + (((float)buttonRectangle.Width - (float)imageSize.Width) / 2));
                    }
                    else if (alignment == ToggleSwitch.ToggleSwitchButtonAlignment.Right)
                    {
                        imageXPos = (int)((float)buttonRectangle.X + (float)buttonRectangle.Width - (float)imageSize.Width);
                    }

                    imageRectangle = new Rectangle(imageXPos, (int)((float)buttonRectangle.Y + (((float)buttonRectangle.Height - (float)imageSize.Height) / 2)), imageSize.Width, imageSize.Height);

                    if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
                    {
                        g.DrawImage(buttonImage, imageRectangle, 0, 0, buttonImage.Width, buttonImage.Height, GraphicsUnit.Pixel, ImageHelper.GetGrayscaleAttributes());
                    }
                    else
                    {
                        g.DrawImageUnscaled(buttonImage, imageRectangle);
                    }
                }

                g.ResetClip();
            }
        }