开关
1 效果

2 代码
/// <summary> /// 开关样式 /// </summary> public enum SwitchStyle { style1 = 0, style2 = 1, style3 = 2, style4 = 3, style5 = 4, style6 = 5 }; public partial class USwitch : UserControl { public USwitch() { InitializeComponent(); //设置Style支持透明背景色并且双缓冲 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.UserPaint, true); this.BackColor = Color.Transparent; this.Cursor = Cursors.Hand; this.Size = new Size(87, 27); } bool isCheck = false; /// <summary> /// 是否选中 /// </summary> public bool Checked { set { isCheck = value; this.Invalidate(); } get { return isCheck; } } SwitchStyle switchStyle = SwitchStyle.style1; /// <summary> /// 开关样式 /// </summary> public SwitchStyle SwitchStyleSet { set { switchStyle = value; this.Invalidate(); } get { return switchStyle; } } protected override void OnPaint(PaintEventArgs e) { Bitmap bitMapOn = null;//状态开时的图标 Bitmap bitMapOff = null;//状态关时的图标 if (switchStyle == SwitchStyle.style1) { bitMapOn = global::WinCustControls.Properties.Resources.btncheckon1; bitMapOff = global::WinCustControls.Properties.Resources.btncheckoff1; } else if (switchStyle == SwitchStyle.style2) { bitMapOn = global::WinCustControls.Properties.Resources.btncheckon2; bitMapOff = global::WinCustControls.Properties.Resources.btncheckoff2; } else if (switchStyle == SwitchStyle.style3) { bitMapOn = global::WinCustControls.Properties.Resources.btncheckon3; bitMapOff = global::WinCustControls.Properties.Resources.btncheckoff3; } else if (switchStyle == SwitchStyle.style4) { bitMapOn = global::WinCustControls.Properties.Resources.btncheckon4; bitMapOff = global::WinCustControls.Properties.Resources.btncheckoff4; } else if (switchStyle == SwitchStyle.style5) { bitMapOn = global::WinCustControls.Properties.Resources.btncheckon5; bitMapOff = global::WinCustControls.Properties.Resources.btncheckoff5; } else if (switchStyle == SwitchStyle.style6) { bitMapOn = global::WinCustControls.Properties.Resources.btncheckon6; bitMapOff = global::WinCustControls.Properties.Resources.btncheckoff6; } Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height); if (isCheck) { g.DrawImage(bitMapOn, rec); } else { g.DrawImage(bitMapOff, rec); } } private void USwitch_Click(object sender, EventArgs e) { isCheck = !isCheck; this.Invalidate(); } }
2

[DefaultEvent("CheckedChanged")] public partial class USwitch2 : UserControl { public USwitch2() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.UserPaint, true); this.MouseDown += UCSwitch_MouseDown; } [Description("选中改变事件"), Category("自定义")] public event EventHandler CheckedChanged; private Color m_trueColor = Color.FromArgb(34, 163, 169); [Description("选中时颜色"), Category("自定义")] public Color TrueColor { get { return m_trueColor; } set { m_trueColor = value; Refresh(); } } private Color m_falseColor = Color.FromArgb(111, 122, 126); [Description("没有选中时颜色"), Category("自定义")] public Color FalseColor { get { return m_falseColor; } set { m_falseColor = value; Refresh(); } } private bool m_checked; [Description("是否选中"), Category("自定义")] public bool Checked { get { return m_checked; } set { m_checked = value; Refresh(); if (CheckedChanged != null) { CheckedChanged(this, null); } } } private string[] m_texts; [Description("文本值,当选中或没有选中时显示,必须是长度为2的数组"), Category("自定义")] public string[] Texts { get { return m_texts; } set { m_texts = value; Refresh(); } } private SwitchType m_switchType = SwitchType.Ellipse; [Description("显示类型"), Category("自定义")] public SwitchType SwitchType { get { return m_switchType; } set { m_switchType = value; Refresh(); } } public override Font Font { get { return base.Font; } set { base.Font = value; Refresh(); } } void UCSwitch_MouseDown(object sender, MouseEventArgs e) { Checked = !Checked; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); var g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; if (m_switchType ==SwitchType.Ellipse) { var fillColor = m_checked ? m_trueColor : m_falseColor; GraphicsPath path = new GraphicsPath(); path.AddLine(new Point(this.Height / 2, 1), new Point(this.Width - this.Height / 2, 1)); path.AddArc(new Rectangle(this.Width - this.Height - 1, 1, this.Height - 2, this.Height - 2), -90, 180); path.AddLine(new Point(this.Width - this.Height / 2, this.Height - 1), new Point(this.Height / 2, this.Height - 1)); path.AddArc(new Rectangle(1, 1, this.Height - 2, this.Height - 2), 90, 180); g.FillPath(new SolidBrush(fillColor), path); string strText = string.Empty; if (m_texts != null && m_texts.Length == 2) { if (m_checked) { strText = m_texts[0]; } else { strText = m_texts[1]; } } if (m_checked) { g.FillEllipse(Brushes.White, new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4)); if (string.IsNullOrEmpty(strText)) { g.DrawEllipse(new Pen(Color.White, 2), new Rectangle((this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2)); } else { System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font); int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2; g.DrawString(strText, Font, Brushes.White, new Point((this.Height - 2 - 4) / 2, intTextY)); } } else { g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4)); if (string.IsNullOrEmpty(strText)) { g.DrawEllipse(new Pen(Color.White, 2), new Rectangle(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2)); } else { System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font); int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2; g.DrawString(strText, Font, Brushes.White, new Point(this.Width - 2 - (this.Height - 2 ) / 2 - ((this.Height)) / 2 - ((int)sizeF.Width/2+4), intTextY)); } } } else if (m_switchType == SwitchType.Quadrilateral)//四边形 { //填充颜色 var fillColor = m_checked ? m_trueColor : m_falseColor; GraphicsPath path = new GraphicsPath(); int intRadius = 5; //画圆角矩形 path.AddArc(0, 0, intRadius, intRadius, 180f, 90f); path.AddArc(this.Width - intRadius - 1, 0, intRadius, intRadius, 270f, 90f); path.AddArc(this.Width - intRadius - 1, this.Height - intRadius - 1, intRadius, intRadius, 0f, 90f); path.AddArc(0, this.Height - intRadius - 1, intRadius, intRadius, 90f, 90f); g.FillPath(new SolidBrush(fillColor), path); string strText = string.Empty; if (m_texts != null && m_texts.Length == 2) { if (m_checked) { strText = m_texts[0]; } else { strText = m_texts[1]; } } if (m_checked) { GraphicsPath path2 = new GraphicsPath(); path2.AddArc(this.Width - this.Height - 1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f); path2.AddArc(this.Width - 1 - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f); path2.AddArc(this.Width - 1 - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f); path2.AddArc(this.Width - this.Height - 1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f); g.FillPath(Brushes.White, path2); if (string.IsNullOrEmpty(strText)) { g.DrawEllipse(new Pen(Color.White, 2), new Rectangle((this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2)); } else { System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font); int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2; g.DrawString(strText, Font, Brushes.White, new Point((this.Height - 2 - 4) / 2, intTextY)); } } else { GraphicsPath path2 = new GraphicsPath(); path2.AddArc(1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f); path2.AddArc(this.Height - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f); path2.AddArc(this.Height - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f); path2.AddArc(1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f); g.FillPath(Brushes.White, path2); //g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4)); if (string.IsNullOrEmpty(strText)) { g.DrawEllipse(new Pen(Color.White, 2), new Rectangle(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2)); } else { System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font); int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2; g.DrawString(strText, Font, Brushes.White, new Point(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 - (int)sizeF.Width / 2, intTextY)); } } } else { var fillColor = m_checked ? m_trueColor : m_falseColor; int intLineHeight = (this.Height - 2 - 4) / 2; GraphicsPath path = new GraphicsPath(); path.AddLine(new Point(this.Height / 2, (this.Height - intLineHeight) / 2), new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2)); path.AddArc(new Rectangle(this.Width - this.Height / 2 - intLineHeight - 1, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), -90, 180); path.AddLine(new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2 + intLineHeight), new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2 + intLineHeight)); path.AddArc(new Rectangle(this.Height / 2, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), 90, 180); g.FillPath(new SolidBrush(fillColor), path); if (m_checked) { g.FillEllipse(new SolidBrush(fillColor), new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4)); g.FillEllipse(Brushes.White, new Rectangle(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 - 4, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2)); } else { g.FillEllipse(new SolidBrush(fillColor), new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4)); g.FillEllipse(Brushes.White, new Rectangle((this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 + 4, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2)); } } } } public enum SwitchType { /// <summary> /// 椭圆 /// </summary> Ellipse, /// <summary> /// 四边形 /// </summary> Quadrilateral, /// <summary> /// 横线 /// </summary> Line }

浙公网安备 33010602011771号