/// <summary> /// Renders the button. /// </summary> /// <param name="g">The g.</param> /// <param name="buttonRectangle">The button rectangle.</param> public override void RenderButton(Graphics g, Rectangle buttonRectangle) { g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBilinear; buttonRectangle.Inflate(-1, -1); using (GraphicsPath buttonPath = GetRoundedRectanglePath(buttonRectangle, CornerRadius)) { g.SetClip(buttonPath); //Draw button surface Color buttonSurfaceColor1 = ButtonNormalSurfaceColor1; Color buttonSurfaceColor2 = ButtonNormalSurfaceColor2; if (ToggleSwitch.IsButtonPressed) { buttonSurfaceColor1 = ButtonPressedSurfaceColor1; buttonSurfaceColor2 = ButtonPressedSurfaceColor2; } else if (ToggleSwitch.IsButtonHovered) { buttonSurfaceColor1 = ButtonHoverSurfaceColor1; buttonSurfaceColor2 = ButtonHoverSurfaceColor2; } if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled) { buttonSurfaceColor1 = buttonSurfaceColor1.ToGrayScale(); buttonSurfaceColor2 = buttonSurfaceColor2.ToGrayScale(); } using (Brush buttonSurfaceBrush = new LinearGradientBrush(buttonRectangle, buttonSurfaceColor1, buttonSurfaceColor2, LinearGradientMode.Vertical)) { g.FillPath(buttonSurfaceBrush, buttonPath); } //Draw button border Color buttonBorderColor1 = ButtonNormalBorderColor1; Color buttonBorderColor2 = ButtonNormalBorderColor2; if (ToggleSwitch.IsButtonPressed) { buttonBorderColor1 = ButtonPressedBorderColor1; buttonBorderColor2 = ButtonPressedBorderColor2; } else if (ToggleSwitch.IsButtonHovered) { buttonBorderColor1 = ButtonHoverBorderColor1; buttonBorderColor2 = ButtonHoverBorderColor2; } if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled) { buttonBorderColor1 = buttonBorderColor1.ToGrayScale(); buttonBorderColor2 = buttonBorderColor2.ToGrayScale(); } using (Brush buttonBorderBrush = new LinearGradientBrush(buttonRectangle, buttonBorderColor1, buttonBorderColor2, LinearGradientMode.Vertical)) { using (Pen buttonBorderPen = new Pen(buttonBorderBrush)) { g.DrawPath(buttonBorderPen, buttonPath); } } g.ResetClip(); //Draw button image Image buttonImage = ToggleSwitch.ButtonImage ?? (ToggleSwitch.Checked ? ToggleSwitch.OnButtonImage : ToggleSwitch.OffButtonImage); if (buttonImage != null) { g.SetClip(buttonPath); ZeroitToggleSwitch.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 == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Center) { imageXPos = (int)((float)buttonRectangle.X + (((float)buttonRectangle.Width - (float)resizedImageSize.Width) / 2)); } else if (alignment == ZeroitToggleSwitch.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 == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Center) { imageXPos = (int)((float)buttonRectangle.X + (((float)buttonRectangle.Width - (float)imageSize.Width) / 2)); } else if (alignment == ZeroitToggleSwitch.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(); } } }
/// <summary> /// Renders the button. /// </summary> /// <param name="g">The g.</param> /// <param name="buttonRectangle">The button rectangle.</param> public override void RenderButton(Graphics g, Rectangle buttonRectangle) { Rectangle controlRectangle = GetInnerControlRectangle(); g.SetClip(controlRectangle); int fullCathetusLength = GetCathetusLengthBasedOnAngle(); int dblFullCathetusLength = 2 * fullCathetusLength; Point[] polygonPoints = new Point[4]; Rectangle adjustedButtonRect = new Rectangle(buttonRectangle.X - fullCathetusLength, controlRectangle.Y, buttonRectangle.Width + dblFullCathetusLength, controlRectangle.Height); if (SlantAngle > 0) { polygonPoints[0] = new Point(adjustedButtonRect.X + fullCathetusLength, adjustedButtonRect.Y); polygonPoints[1] = new Point(adjustedButtonRect.X + adjustedButtonRect.Width - 1, adjustedButtonRect.Y); polygonPoints[2] = new Point(adjustedButtonRect.X + adjustedButtonRect.Width - fullCathetusLength - 1, adjustedButtonRect.Y + adjustedButtonRect.Height - 1); polygonPoints[3] = new Point(adjustedButtonRect.X, adjustedButtonRect.Y + adjustedButtonRect.Height - 1); } else { polygonPoints[0] = new Point(adjustedButtonRect.X, adjustedButtonRect.Y); polygonPoints[1] = new Point(adjustedButtonRect.X + adjustedButtonRect.Width - fullCathetusLength - 1, adjustedButtonRect.Y); polygonPoints[2] = new Point(adjustedButtonRect.X + adjustedButtonRect.Width - 1, adjustedButtonRect.Y + adjustedButtonRect.Height - 1); polygonPoints[3] = new Point(adjustedButtonRect.X + fullCathetusLength, adjustedButtonRect.Y + adjustedButtonRect.Height - 1); } g.IntersectClip(adjustedButtonRect); Color buttonColor = ToggleSwitch.Checked ? OnButtonColor : OffButtonColor; Color buttonBorderColor = ToggleSwitch.Checked ? OnButtonBorderColor : OffButtonBorderColor; if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled) { buttonColor = buttonColor.ToGrayScale(); buttonBorderColor = buttonBorderColor.ToGrayScale(); } using (Pen buttonPen = new Pen(buttonBorderColor)) { g.DrawPolygon(buttonPen, polygonPoints); } using (Brush buttonBrush = new SolidBrush(buttonColor)) { g.FillPolygon(buttonBrush, polygonPoints); } Image buttonImage = ToggleSwitch.ButtonImage ?? (ToggleSwitch.Checked ? ToggleSwitch.OnButtonImage : ToggleSwitch.OffButtonImage); string buttonText = ToggleSwitch.Checked ? ToggleSwitch.OnText : ToggleSwitch.OffText; if (buttonImage != null || !string.IsNullOrEmpty(buttonText)) { ZeroitToggleSwitch.ToggleSwitchButtonAlignment alignment = ToggleSwitch.ButtonImage != null ? ToggleSwitch.ButtonAlignment : (ToggleSwitch.Checked ? ToggleSwitch.OnButtonAlignment : ToggleSwitch.OffButtonAlignment); if (buttonImage != null) { Size imageSize = buttonImage.Size; Rectangle imageRectangle; int imageXPos = (int)adjustedButtonRect.X; bool scaleImage = ToggleSwitch.ButtonImage != null ? ToggleSwitch.ButtonScaleImageToFit : (ToggleSwitch.Checked ? ToggleSwitch.OnButtonScaleImageToFit : ToggleSwitch.OffButtonScaleImageToFit); if (scaleImage) { Size canvasSize = adjustedButtonRect.Size; Size resizedImageSize = ImageHelper.RescaleImageToFit(imageSize, canvasSize); if (alignment == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Center) { imageXPos = (int)((float)adjustedButtonRect.X + (((float)adjustedButtonRect.Width - (float)resizedImageSize.Width) / 2)); } else if (alignment == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Right) { imageXPos = (int)((float)adjustedButtonRect.X + (float)adjustedButtonRect.Width - (float)resizedImageSize.Width); } imageRectangle = new Rectangle(imageXPos, (int)((float)adjustedButtonRect.Y + (((float)adjustedButtonRect.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 == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Center) { imageXPos = (int)((float)adjustedButtonRect.X + (((float)adjustedButtonRect.Width - (float)imageSize.Width) / 2)); } else if (alignment == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Right) { imageXPos = (int)((float)adjustedButtonRect.X + (float)adjustedButtonRect.Width - (float)imageSize.Width); } imageRectangle = new Rectangle(imageXPos, (int)((float)adjustedButtonRect.Y + (((float)adjustedButtonRect.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); } } } else if (!string.IsNullOrEmpty(buttonText)) { Font buttonFont = ToggleSwitch.Checked ? ToggleSwitch.OnFont : ToggleSwitch.OffFont; Color buttonForeColor = ToggleSwitch.Checked ? ToggleSwitch.OnForeColor : ToggleSwitch.OffForeColor; if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled) { buttonForeColor = buttonForeColor.ToGrayScale(); } SizeF textSize = g.MeasureString(buttonText, buttonFont); float textXPos = adjustedButtonRect.X; if (alignment == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Center) { textXPos = (float)adjustedButtonRect.X + (((float)adjustedButtonRect.Width - (float)textSize.Width) / 2); } else if (alignment == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Right) { textXPos = (float)adjustedButtonRect.X + (float)adjustedButtonRect.Width - (float)textSize.Width; } RectangleF textRectangle = new RectangleF(textXPos, (float)adjustedButtonRect.Y + (((float)adjustedButtonRect.Height - (float)textSize.Height) / 2), textSize.Width, textSize.Height); using (Brush textBrush = new SolidBrush(buttonForeColor)) { g.DrawString(buttonText, buttonFont, textBrush, textRectangle); } } } g.ResetClip(); }
/// <summary> /// Renders the button. /// </summary> /// <param name="g">The g.</param> /// <param name="buttonRectangle">The button rectangle.</param> 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()); ZeroitToggleSwitch.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 == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Center) { imageXPos = (int)((float)buttonRectangle.X + (((float)buttonRectangle.Width - (float)resizedImageSize.Width) / 2)); } else if (alignment == ZeroitToggleSwitch.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 == ZeroitToggleSwitch.ToggleSwitchButtonAlignment.Center) { imageXPos = (int)((float)buttonRectangle.X + (((float)buttonRectangle.Width - (float)imageSize.Width) / 2)); } else if (alignment == ZeroitToggleSwitch.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(); } }