1、实现切换开关自定义控件
(1)、设置初始化;
(2)、扩展自定义属性;
(3)、控件重绘;
(4)、定义事件。
2、自定义控件代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomLib
{
//指定默认事件
[DefaultEvent("CheckedChanged")]
public class Switch:Control
{
/// <summary>
/// 构造函数
/// </summary>
public Switch()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.Selectable, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Size = new Size(80, 30);
this.Click += Switch_Click;
}
//事件
public event EventHandler CheckedChanged;
/// <summary>
/// 点击事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Switch_Click(object sender, EventArgs e)
{
CheckedChanged?.Invoke(this, new EventArgs());
}
/// <summary>
/// 重绘
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
//背景填充色
var fillColor = swChecked ? trueColor : falseColor;
//路径
GraphicsPath path = new GraphicsPath();
path.AddLine(Height / 2, 1, Width - Height / 2, 1);
path.AddArc(new Rectangle(Width - Height - 1, 1, Height - 2, Height - 2), 270, 180);
path.AddLine(Width - Height / 2, Height - 1, Height / 2, Height - 1);
path.AddArc(new Rectangle(1, 1, Height - 2, Height - 2), 90, 180);
//填充路径
g.FillPath(new SolidBrush(fillColor), path);
//显示文本
string text = swChecked ? trueText : falseText;
//根据开关状态,显示圆和文字
if (swChecked)
{
g.FillEllipse(Brushes.White, new Rectangle(Width - Height - 1 + 2, 1 + 2, Height - 2 - 4, Height - 2 - 4));
SizeF fontSize = g.MeasureString(text.Replace("", "开"), swFont);
int x = (Height - 2 - 4) / 2;
int y = (Height - (int)fontSize.Height) / 2 + 2;
g.DrawString(text, swFont, Brushes.White, x, y);
}
else
{
g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, Height - 2 - 4, Height - 2 - 4));
SizeF fontSize = g.MeasureString(text.Replace("", "关"), swFont);
int x = Width - 2 - Height / 2 - (int)fontSize.Width + 10;
int y = (Height - (int)fontSize.Height) / 2 + 2;
g.DrawString(text, swFont, Brushes.White, x, y);
}
}
#region 属性
private bool swChecked=false;
[Description("开关状态"), Category("自定义")]
public bool SwChecked
{
get { return swChecked; }
set { swChecked = value; Invalidate(); }
}
private Color trueColor = Color.Green;
[Description("开背景色"),Category("自定义")]
public Color TrueColor
{
get { return trueColor; }
set { trueColor = value; Invalidate(); }
}
private Color falseColor = Color.Gray;
[Description("关背景色"), Category("自定义")]
public Color FalseColor
{
get { return falseColor; }
set { falseColor = value; Invalidate(); }
}
private string trueText= "开";
[Description("开文字"), Category("自定义")]
public string TrueText
{
get { return trueText; }
set { trueText = value; Invalidate(); }
}
private string falseText= "关";
[Description("关文字"), Category("自定义")]
public string FalseText
{
get { return falseText; }
set { falseText = value; Invalidate(); }
}
private Font swFont=new Font("Arial", 12, FontStyle.Bold);
[Description("字体"), Category("自定义")]
public Font SwFont
{
get { return swFont; }
set { swFont = value; Invalidate(); }
}
#endregion
}
}
2、控件使用和事件处理
using CustomLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomControlApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void switch1_CheckedChanged(object sender, EventArgs e)
{
switch1.SwChecked=!switch1.SwChecked;
}
}
}
3、运行结果