* UITurnSwitch: 调整配色,增加自定义角度
This commit is contained in:
parent
a98cbe3cf2
commit
777361b186
@ -17,12 +17,14 @@
|
|||||||
* 创建日期: 2023-07-05
|
* 创建日期: 2023-07-05
|
||||||
*
|
*
|
||||||
* 2023-07-05: V3.3.9 增加文件说明
|
* 2023-07-05: V3.3.9 增加文件说明
|
||||||
|
* 2023-07-06: V3.3.9 调整配色,增加自定义角度
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Drawing2D;
|
using System.Drawing.Drawing2D;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace Sunny.UI
|
namespace Sunny.UI
|
||||||
{
|
{
|
||||||
@ -41,18 +43,10 @@ namespace Sunny.UI
|
|||||||
public UITurnSwitch()
|
public UITurnSwitch()
|
||||||
{
|
{
|
||||||
SetStyleFlags();
|
SetStyleFlags();
|
||||||
Height = 150;
|
Height = 160;
|
||||||
Width = 150;
|
Width = 160;
|
||||||
ShowText = false;
|
ShowText = false;
|
||||||
ShowRect = false;
|
ShowRect = false;
|
||||||
|
|
||||||
inActiveColor = Color.Gray;
|
|
||||||
fillColor = Color.White;
|
|
||||||
|
|
||||||
rectColor = UIStyles.Blue.SwitchActiveColor;
|
|
||||||
fillColor = UIStyles.Blue.SwitchFillColor;
|
|
||||||
inActiveColor = UIStyles.Blue.SwitchInActiveColor;
|
|
||||||
rectDisableColor = UIStyles.Blue.SwitchRectDisableColor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[DefaultValue(false)]
|
[DefaultValue(false)]
|
||||||
@ -121,9 +115,9 @@ namespace Sunny.UI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color inActiveColor;
|
private Color inActiveColor = Color.Red;
|
||||||
|
|
||||||
[DefaultValue(typeof(Color), "Gray")]
|
[DefaultValue(typeof(Color), "Red")]
|
||||||
[Description("关闭颜色"), Category("SunnyUI")]
|
[Description("关闭颜色"), Category("SunnyUI")]
|
||||||
public Color InActiveColor
|
public Color InActiveColor
|
||||||
{
|
{
|
||||||
@ -135,26 +129,84 @@ namespace Sunny.UI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private Color activeColor = Color.Lime;
|
||||||
/// 填充颜色,当值为背景色或透明色或空值则不填充
|
|
||||||
/// </summary>
|
|
||||||
[Description("填充颜色"), Category("SunnyUI")]
|
|
||||||
[DefaultValue(typeof(Color), "White")]
|
|
||||||
public Color ButtonColor
|
|
||||||
{
|
|
||||||
get => fillColor;
|
|
||||||
set => SetFillColor(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 边框颜色
|
/// 边框颜色
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Description("打开颜色"), Category("SunnyUI")]
|
[Description("打开颜色"), Category("SunnyUI")]
|
||||||
[DefaultValue(typeof(Color), "80, 160, 255")]
|
[DefaultValue(typeof(Color), "Lime")]
|
||||||
public Color ActiveColor
|
public Color ActiveColor
|
||||||
{
|
{
|
||||||
get => rectColor;
|
get => activeColor;
|
||||||
set => SetRectColor(value);
|
set
|
||||||
|
{
|
||||||
|
activeColor = value;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int inActiveAngle = 315;
|
||||||
|
|
||||||
|
[DefaultValue(315)]
|
||||||
|
[Description("开关关闭角度"), Category("SunnyUI")]
|
||||||
|
public int InActiveAngle
|
||||||
|
{
|
||||||
|
get => inActiveAngle;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
inActiveAngle = value;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int activeAngle = 45;
|
||||||
|
/// <summary>
|
||||||
|
/// 边框颜色
|
||||||
|
/// </summary>
|
||||||
|
[Description("开关打开角度"), Category("SunnyUI")]
|
||||||
|
[DefaultValue(45)]
|
||||||
|
public int ActiveAngle
|
||||||
|
{
|
||||||
|
get => activeAngle;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
activeAngle = value;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color backColor = Color.Silver;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 填充颜色
|
||||||
|
/// </summary>
|
||||||
|
[Description("填充颜色"), Category("SunnyUI")]
|
||||||
|
[DefaultValue(typeof(Color), "Silver")]
|
||||||
|
public Color FillColor
|
||||||
|
{
|
||||||
|
get => backColor;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
backColor = value;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color handleColor = Color.DarkGray;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 按钮把手颜色
|
||||||
|
/// </summary>
|
||||||
|
[Description("按钮把手颜色"), Category("SunnyUI")]
|
||||||
|
[DefaultValue(typeof(Color), "DarkGray")]
|
||||||
|
public Color HandleColor
|
||||||
|
{
|
||||||
|
get => handleColor;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
handleColor = value;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -196,35 +248,10 @@ namespace Sunny.UI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 设置主题样式
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="uiColor">主题样式</param>
|
|
||||||
public override void SetStyleColor(UIBaseStyle uiColor)
|
|
||||||
{
|
|
||||||
base.SetStyleColor(uiColor);
|
|
||||||
|
|
||||||
rectColor = uiColor.SwitchActiveColor;
|
|
||||||
fillColor = uiColor.SwitchFillColor;
|
|
||||||
inActiveColor = uiColor.SwitchInActiveColor;
|
|
||||||
rectDisableColor = uiColor.SwitchRectDisableColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Description("不可用颜色"), Category("SunnyUI")]
|
|
||||||
[DefaultValue(typeof(Color), "173, 178, 181")]
|
|
||||||
public Color DisabledColor
|
|
||||||
{
|
|
||||||
get => rectDisableColor;
|
|
||||||
set => SetRectDisableColor(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnEnabledChanged(EventArgs e)
|
|
||||||
{
|
|
||||||
base.OnEnabledChanged(e);
|
|
||||||
Invalidate();
|
|
||||||
}
|
|
||||||
|
|
||||||
private int backSize = 100;
|
private int backSize = 100;
|
||||||
|
|
||||||
|
[Description("开关尺寸"), Category("SunnyUI")]
|
||||||
|
[DefaultValue(100)]
|
||||||
public int BackSize
|
public int BackSize
|
||||||
{
|
{
|
||||||
get => backSize;
|
get => backSize;
|
||||||
@ -236,6 +263,9 @@ namespace Sunny.UI
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int backInnerSize = 80;
|
private int backInnerSize = 80;
|
||||||
|
|
||||||
|
[Description("开关内圈尺寸"), Category("SunnyUI")]
|
||||||
|
[DefaultValue(80)]
|
||||||
public int BackInnerSize
|
public int BackInnerSize
|
||||||
{
|
{
|
||||||
get => backInnerSize;
|
get => backInnerSize;
|
||||||
@ -260,38 +290,57 @@ namespace Sunny.UI
|
|||||||
g.FillEllipse(rectColor, new Rectangle(center.X - BackSize / 2, center.Y - BackSize / 2, BackSize, BackSize));
|
g.FillEllipse(rectColor, new Rectangle(center.X - BackSize / 2, center.Y - BackSize / 2, BackSize, BackSize));
|
||||||
int size = backSize - 10;
|
int size = backSize - 10;
|
||||||
g.FillEllipse(Color.White, new Rectangle(center.X - size / 2, center.Y - size / 2, size, size));
|
g.FillEllipse(Color.White, new Rectangle(center.X - size / 2, center.Y - size / 2, size, size));
|
||||||
g.FillEllipse(rectColor, new Rectangle(center.X - backInnerSize / 2, center.Y - backInnerSize / 2, backInnerSize, backInnerSize));
|
g.FillEllipse(FillColor, new Rectangle(center.X - backInnerSize / 2, center.Y - backInnerSize / 2, backInnerSize, backInnerSize));
|
||||||
|
|
||||||
|
int size2 = 6;
|
||||||
|
using Pen pn = rectColor.Pen(2);
|
||||||
|
PointF pt;
|
||||||
if (Active)
|
if (Active)
|
||||||
{
|
{
|
||||||
int size1 = 10;
|
PointF[] points = GetHandles(ActiveAngle);
|
||||||
int size2 = 6;
|
g.FillPolygon(HandleColor, points);
|
||||||
PointF pt1 = center.CalcAzRangePoint(size1, 45);
|
g.DrawPolygon(pn, points);
|
||||||
PointF pt2 = center.CalcAzRangePoint(size1, 225);
|
pt = center.CalcAzRangePoint(BackSize / 2 - size2, ActiveAngle);
|
||||||
PointF pt3 = pt1.CalcAzRangePoint(BackSize / 2 + size2, 315);
|
|
||||||
PointF pt4 = pt2.CalcAzRangePoint(BackSize / 2 + size2, 315);
|
|
||||||
PointF pt5 = pt1.CalcAzRangePoint(BackSize / 2 + size2, 135);
|
|
||||||
PointF pt6 = pt2.CalcAzRangePoint(BackSize / 2 + size2, 135);
|
|
||||||
g.FillPolygon(Color.Silver, new PointF[] { pt3, pt4, pt6, pt5 });
|
|
||||||
|
|
||||||
pt1 = center.CalcAzRangePoint(BackSize / 2 - size2, 315);
|
|
||||||
g.FillEllipse(Color.Lime, pt1.X - size2, pt1.Y - size2, size2 * 2, size2 * 2);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int size1 = 10;
|
PointF[] points = GetHandles(InActiveAngle);
|
||||||
int size2 = 6;
|
g.FillPolygon(HandleColor, points);
|
||||||
PointF pt1 = center.CalcAzRangePoint(size1, 135);
|
g.DrawPolygon(pn, points);
|
||||||
PointF pt2 = center.CalcAzRangePoint(size1, 315);
|
pt = center.CalcAzRangePoint(BackSize / 2 - size2, InActiveAngle);
|
||||||
PointF pt3 = pt1.CalcAzRangePoint(BackSize / 2 + size2, 45);
|
|
||||||
PointF pt4 = pt2.CalcAzRangePoint(BackSize / 2 + size2, 45);
|
|
||||||
PointF pt5 = pt1.CalcAzRangePoint(BackSize / 2 + size2, 225);
|
|
||||||
PointF pt6 = pt2.CalcAzRangePoint(BackSize / 2 + size2, 225);
|
|
||||||
g.FillPolygon(Color.Silver, new PointF[] { pt3, pt4, pt6, pt5 });
|
|
||||||
|
|
||||||
pt1 = center.CalcAzRangePoint(BackSize / 2 - size2, 45);
|
|
||||||
g.FillEllipse(Color.Red, pt1.X - size2, pt1.Y - size2, size2 * 2, size2 * 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.FillEllipse(color, pt.X - size2, pt.Y - size2, size2 * 2, size2 * 2);
|
||||||
|
Size sz = TextRenderer.MeasureText(ActiveText, Font);
|
||||||
|
pt = center.CalcAzRangePoint(BackSize / 2 + size2 + 4 + sz.Width / 2, ActiveAngle);
|
||||||
|
g.DrawString(ActiveText, Font, ActiveColor, new Rectangle((int)(pt.X - sz.Width / 2), (int)(pt.Y - sz.Height / 2), sz.Width, sz.Height), ContentAlignment.MiddleCenter);
|
||||||
|
|
||||||
|
sz = TextRenderer.MeasureText(InActiveText, Font);
|
||||||
|
pt = center.CalcAzRangePoint(BackSize / 2 + size2 + 4 + sz.Width / 2, InActiveAngle);
|
||||||
|
g.DrawString(InActiveText, Font, InActiveColor, new Rectangle((int)(pt.X - sz.Width / 2), (int)(pt.Y - sz.Height / 2), sz.Width, sz.Height), ContentAlignment.MiddleCenter);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private PointF[] GetHandles(int angle)
|
||||||
|
{
|
||||||
|
int size1 = 10;
|
||||||
|
int size2 = 4;
|
||||||
|
Point center = new Point(Width / 2, Height / 2);
|
||||||
|
PointF pt1 = center.CalcAzRangePoint(size1, angle - 90);
|
||||||
|
PointF pt2 = center.CalcAzRangePoint(size1, angle + 90);
|
||||||
|
PointF pt3 = pt1.CalcAzRangePoint(BackSize / 2 + size2, angle);
|
||||||
|
PointF pt4 = pt2.CalcAzRangePoint(BackSize / 2 + size2, angle);
|
||||||
|
PointF pt5 = pt1.CalcAzRangePoint(BackSize / 2 + size2, angle + 180);
|
||||||
|
PointF pt6 = pt2.CalcAzRangePoint(BackSize / 2 + size2, angle + 180);
|
||||||
|
|
||||||
|
PointF pt11 = center.CalcAzRangePoint(size1 - 2, angle - 90);
|
||||||
|
PointF pt12 = center.CalcAzRangePoint(size1 - 2, angle + 90);
|
||||||
|
PointF pt13 = pt11.CalcAzRangePoint(BackSize / 2 + size2 + 2, angle);
|
||||||
|
PointF pt14 = pt12.CalcAzRangePoint(BackSize / 2 + size2 + 2, angle);
|
||||||
|
PointF pt15 = pt11.CalcAzRangePoint(BackSize / 2 + size2 + 2, angle + 180);
|
||||||
|
PointF pt16 = pt12.CalcAzRangePoint(BackSize / 2 + size2 + 2, angle + 180);
|
||||||
|
|
||||||
|
return new PointF[] { pt3, pt13, pt14, pt4, pt6, pt16, pt15, pt5 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user