(四十)c#Winform自定义控件-开关-HZHControls

官网

https://www.hzhcontrols.cn

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

GDI+需要有一点了解,不知道的可以百度瞅瞅

开始

添加一个用户控件,命名UCSwitch

添加一个枚举用以控制样式

 1 public enum SwitchType
 2     {
 3         /// <summary>
 4         /// 椭圆
 5         /// </summary>
 6         Ellipse,
 7         /// <summary>
 8         /// 四边形
 9         /// </summary>
10         Quadrilateral,
11         /// <summary>
12         /// 横线
13         /// </summary>
14         Line
15     }

 

添加属性

 1   [Description("选中改变事件"), Category("自定义")]
 2         public event EventHandler CheckedChanged;
 3         private Color m_trueColor = Color.FromArgb(34, 163, 169);
 4 
 5         [Description("选中时颜色"), Category("自定义")]
 6         public Color TrueColor
 7         {
 8             get { return m_trueColor; }
 9             set
10             {
11                 m_trueColor = value;
12                 Refresh();
13             }
14         }
15 
16         private Color m_falseColor = Color.FromArgb(111, 122, 126);
17 
18         [Description("没有选中时颜色"), Category("自定义")]
19         public Color FalseColor
20         {
21             get { return m_falseColor; }
22             set
23             {
24                 m_falseColor = value;
25                 Refresh();
26             }
27         }
28 
29         private bool m_checked;
30 
31         [Description("是否选中"), Category("自定义")]
32         public bool Checked
33         {
34             get { return m_checked; }
35             set
36             {
37                 m_checked = value;
38                 Refresh();
39                 if (CheckedChanged != null)
40                 {
41                     CheckedChanged(this, null);
42                 }
43             }
44         }
45 
46         private string[] m_texts;
47 
48         [Description("文本值,当选中或没有选中时显示,必须是长度为2的数组"), Category("自定义")]
49         public string[] Texts
50         {
51             get { return m_texts; }
52             set
53             {
54                 m_texts = value;
55                 Refresh();
56             }
57         }
58         private SwitchType m_switchType = SwitchType.Ellipse;
59 
60         [Description("显示类型"), Category("自定义")]
61         public SwitchType SwitchType
62         {
63             get { return m_switchType; }
64             set
65             {
66                 m_switchType = value;
67                 Refresh();
68             }
69         }
70 
71         public override Font Font
72         {
73             get
74             {
75                 return base.Font;
76             }
77             set
78             {
79                 base.Font = value;
80                 Refresh();
81             }
82         }

重绘

  1  protected override void OnPaint(PaintEventArgs e)
  2         {
  3             base.OnPaint(e);
  4             var g = e.Graphics;
  5             g.SetGDIHigh();
  6             if (m_switchType == HZH_Controls.Controls.SwitchType.Ellipse)
  7             {
  8                 var fillColor = m_checked ? m_trueColor : m_falseColor;
  9                 GraphicsPath path = new GraphicsPath();
 10                 path.AddLine(new Point(this.Height / 2, 1), new Point(this.Width - this.Height / 2, 1));
 11                 path.AddArc(new Rectangle(this.Width - this.Height - 1, 1, this.Height - 2, this.Height - 2), -90, 180);
 12                 path.AddLine(new Point(this.Width - this.Height / 2, this.Height - 1), new Point(this.Height / 2, this.Height - 1));
 13                 path.AddArc(new Rectangle(1, 1, this.Height - 2, this.Height - 2), 90, 180);
 14                 g.FillPath(new SolidBrush(fillColor), path);
 15 
 16                 string strText = string.Empty;
 17                 if (m_texts != null && m_texts.Length == 2)
 18                 {
 19                     if (m_checked)
 20                     {
 21                         strText = m_texts[0];
 22                     }
 23                     else
 24                     {
 25                         strText = m_texts[1];
 26                     }
 27                 }
 28 
 29                 if (m_checked)
 30                 {
 31                     g.FillEllipse(Brushes.White, new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
 32                     if (string.IsNullOrEmpty(strText))
 33                     {
 34                         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));
 35                     }
 36                     else
 37                     {
 38                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
 39                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
 40                         g.DrawString(strText, Font, Brushes.White, new Point((this.Height - 2 - 4) / 2, intTextY));
 41                     }
 42                 }
 43                 else
 44                 {
 45                     g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
 46                     if (string.IsNullOrEmpty(strText))
 47                     {
 48                         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));
 49                     }
 50                     else
 51                     {
 52                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
 53                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
 54                         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));
 55                     }
 56                 }
 57             }
 58             else if (m_switchType == HZH_Controls.Controls.SwitchType.Quadrilateral)
 59             {
 60                 var fillColor = m_checked ? m_trueColor : m_falseColor;
 61                 GraphicsPath path = new GraphicsPath();
 62                 int intRadius = 5;
 63                 path.AddArc(0, 0, intRadius, intRadius, 180f, 90f);
 64                 path.AddArc(this.Width - intRadius - 1, 0, intRadius, intRadius, 270f, 90f);
 65                 path.AddArc(this.Width - intRadius - 1, this.Height - intRadius - 1, intRadius, intRadius, 0f, 90f);
 66                 path.AddArc(0, this.Height - intRadius - 1, intRadius, intRadius, 90f, 90f);
 67 
 68                 g.FillPath(new SolidBrush(fillColor), path);
 69 
 70                 string strText = string.Empty;
 71                 if (m_texts != null && m_texts.Length == 2)
 72                 {
 73                     if (m_checked)
 74                     {
 75                         strText = m_texts[0];
 76                     }
 77                     else
 78                     {
 79                         strText = m_texts[1];
 80                     }
 81                 }
 82 
 83                 if (m_checked)
 84                 {
 85                     GraphicsPath path2 = new GraphicsPath();
 86                     path2.AddArc(this.Width - this.Height - 1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f);
 87                     path2.AddArc(this.Width - 1 - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f);
 88                     path2.AddArc(this.Width - 1 - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f);
 89                     path2.AddArc(this.Width - this.Height - 1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f);
 90                     g.FillPath(Brushes.White, path2);
 91 
 92                     if (string.IsNullOrEmpty(strText))
 93                     {
 94                         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));
 95                     }
 96                     else
 97                     {
 98                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
 99                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
100                         g.DrawString(strText, Font, Brushes.White, new Point((this.Height - 2 - 4) / 2, intTextY));
101                     }
102                 }
103                 else
104                 {
105                     GraphicsPath path2 = new GraphicsPath();
106                     path2.AddArc(1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f);
107                     path2.AddArc(this.Height - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f);
108                     path2.AddArc(this.Height - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f);
109                     path2.AddArc(1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f);
110                     g.FillPath(Brushes.White, path2);
111 
112                     //g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
113                     if (string.IsNullOrEmpty(strText))
114                     {
115                         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));
116                     }
117                     else
118                     {
119                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
120                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
121                         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));
122                     }
123                 }
124             }
125             else
126             {
127                 var fillColor = m_checked ? m_trueColor : m_falseColor;
128                 int intLineHeight = (this.Height - 2 - 4) / 2;
129 
130                 GraphicsPath path = new GraphicsPath();
131                 path.AddLine(new Point(this.Height / 2, (this.Height - intLineHeight) / 2), new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2));
132                 path.AddArc(new Rectangle(this.Width - this.Height / 2 - intLineHeight - 1, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), -90, 180);
133                 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));
134                 path.AddArc(new Rectangle(this.Height / 2, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), 90, 180);
135                 g.FillPath(new SolidBrush(fillColor), path);
136 
137                 if (m_checked)
138                 {
139                     g.FillEllipse(new SolidBrush(fillColor), new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
140                     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));
141                 }
142                 else
143                 {
144                     g.FillEllipse(new SolidBrush(fillColor), new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
145                     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));
146                 }
147             }
148         }

处理一下点击事件

1  void UCSwitch_MouseDown(object sender, MouseEventArgs e)
2         {
3             Checked = !Checked;
4         }

完整代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Drawing.Drawing2D;
 10 
 11 namespace HZH_Controls.Controls
 12 {
 13     [DefaultEvent("CheckedChanged")]
 14     public partial class UCSwitch : UserControl
 15     {
 16         [Description("选中改变事件"), Category("自定义")]
 17         public event EventHandler CheckedChanged;
 18         private Color m_trueColor = Color.FromArgb(34, 163, 169);
 19 
 20         [Description("选中时颜色"), Category("自定义")]
 21         public Color TrueColor
 22         {
 23             get { return m_trueColor; }
 24             set
 25             {
 26                 m_trueColor = value;
 27                 Refresh();
 28             }
 29         }
 30 
 31         private Color m_falseColor = Color.FromArgb(111, 122, 126);
 32 
 33         [Description("没有选中时颜色"), Category("自定义")]
 34         public Color FalseColor
 35         {
 36             get { return m_falseColor; }
 37             set
 38             {
 39                 m_falseColor = value;
 40                 Refresh();
 41             }
 42         }
 43 
 44         private bool m_checked;
 45 
 46         [Description("是否选中"), Category("自定义")]
 47         public bool Checked
 48         {
 49             get { return m_checked; }
 50             set
 51             {
 52                 m_checked = value;
 53                 Refresh();
 54                 if (CheckedChanged != null)
 55                 {
 56                     CheckedChanged(this, null);
 57                 }
 58             }
 59         }
 60 
 61         private string[] m_texts;
 62 
 63         [Description("文本值,当选中或没有选中时显示,必须是长度为2的数组"), Category("自定义")]
 64         public string[] Texts
 65         {
 66             get { return m_texts; }
 67             set
 68             {
 69                 m_texts = value;
 70                 Refresh();
 71             }
 72         }
 73         private SwitchType m_switchType = SwitchType.Ellipse;
 74 
 75         [Description("显示类型"), Category("自定义")]
 76         public SwitchType SwitchType
 77         {
 78             get { return m_switchType; }
 79             set
 80             {
 81                 m_switchType = value;
 82                 Refresh();
 83             }
 84         }
 85 
 86         public override Font Font
 87         {
 88             get
 89             {
 90                 return base.Font;
 91             }
 92             set
 93             {
 94                 base.Font = value;
 95                 Refresh();
 96             }
 97         }
 98 
 99 
100         public UCSwitch()
101         {
102             InitializeComponent();
103             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
104             this.SetStyle(ControlStyles.DoubleBuffer, true);
105             this.SetStyle(ControlStyles.ResizeRedraw, true);
106             this.SetStyle(ControlStyles.Selectable, true);
107             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
108             this.SetStyle(ControlStyles.UserPaint, true);
109             this.MouseDown += UCSwitch_MouseDown;
110         }
111 
112         void UCSwitch_MouseDown(object sender, MouseEventArgs e)
113         {
114             Checked = !Checked;
115         }
116 
117         protected override void OnPaint(PaintEventArgs e)
118         {
119             base.OnPaint(e);
120             var g = e.Graphics;
121             g.SetGDIHigh();
122             if (m_switchType == HZH_Controls.Controls.SwitchType.Ellipse)
123             {
124                 var fillColor = m_checked ? m_trueColor : m_falseColor;
125                 GraphicsPath path = new GraphicsPath();
126                 path.AddLine(new Point(this.Height / 2, 1), new Point(this.Width - this.Height / 2, 1));
127                 path.AddArc(new Rectangle(this.Width - this.Height - 1, 1, this.Height - 2, this.Height - 2), -90, 180);
128                 path.AddLine(new Point(this.Width - this.Height / 2, this.Height - 1), new Point(this.Height / 2, this.Height - 1));
129                 path.AddArc(new Rectangle(1, 1, this.Height - 2, this.Height - 2), 90, 180);
130                 g.FillPath(new SolidBrush(fillColor), path);
131 
132                 string strText = string.Empty;
133                 if (m_texts != null && m_texts.Length == 2)
134                 {
135                     if (m_checked)
136                     {
137                         strText = m_texts[0];
138                     }
139                     else
140                     {
141                         strText = m_texts[1];
142                     }
143                 }
144 
145                 if (m_checked)
146                 {
147                     g.FillEllipse(Brushes.White, new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
148                     if (string.IsNullOrEmpty(strText))
149                     {
150                         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));
151                     }
152                     else
153                     {
154                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
155                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
156                         g.DrawString(strText, Font, Brushes.White, new Point((this.Height - 2 - 4) / 2, intTextY));
157                     }
158                 }
159                 else
160                 {
161                     g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
162                     if (string.IsNullOrEmpty(strText))
163                     {
164                         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));
165                     }
166                     else
167                     {
168                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
169                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
170                         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));
171                     }
172                 }
173             }
174             else if (m_switchType == HZH_Controls.Controls.SwitchType.Quadrilateral)
175             {
176                 var fillColor = m_checked ? m_trueColor : m_falseColor;
177                 GraphicsPath path = new GraphicsPath();
178                 int intRadius = 5;
179                 path.AddArc(0, 0, intRadius, intRadius, 180f, 90f);
180                 path.AddArc(this.Width - intRadius - 1, 0, intRadius, intRadius, 270f, 90f);
181                 path.AddArc(this.Width - intRadius - 1, this.Height - intRadius - 1, intRadius, intRadius, 0f, 90f);
182                 path.AddArc(0, this.Height - intRadius - 1, intRadius, intRadius, 90f, 90f);
183 
184                 g.FillPath(new SolidBrush(fillColor), path);
185 
186                 string strText = string.Empty;
187                 if (m_texts != null && m_texts.Length == 2)
188                 {
189                     if (m_checked)
190                     {
191                         strText = m_texts[0];
192                     }
193                     else
194                     {
195                         strText = m_texts[1];
196                     }
197                 }
198 
199                 if (m_checked)
200                 {
201                     GraphicsPath path2 = new GraphicsPath();
202                     path2.AddArc(this.Width - this.Height - 1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f);
203                     path2.AddArc(this.Width - 1 - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f);
204                     path2.AddArc(this.Width - 1 - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f);
205                     path2.AddArc(this.Width - this.Height - 1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f);
206                     g.FillPath(Brushes.White, path2);
207 
208                     if (string.IsNullOrEmpty(strText))
209                     {
210                         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));
211                     }
212                     else
213                     {
214                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
215                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
216                         g.DrawString(strText, Font, Brushes.White, new Point((this.Height - 2 - 4) / 2, intTextY));
217                     }
218                 }
219                 else
220                 {
221                     GraphicsPath path2 = new GraphicsPath();
222                     path2.AddArc(1 + 2, 1 + 2, intRadius, intRadius, 180f, 90f);
223                     path2.AddArc(this.Height - 2 - intRadius, 1 + 2, intRadius, intRadius, 270f, 90f);
224                     path2.AddArc(this.Height - 2 - intRadius, this.Height - 2 - intRadius - 1, intRadius, intRadius, 0f, 90f);
225                     path2.AddArc(1 + 2, this.Height - 2 - intRadius - 1, intRadius, intRadius, 90f, 90f);
226                     g.FillPath(Brushes.White, path2);
227 
228                     //g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
229                     if (string.IsNullOrEmpty(strText))
230                     {
231                         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));
232                     }
233                     else
234                     {
235                         System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
236                         int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
237                         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));
238                     }
239                 }
240             }
241             else
242             {
243                 var fillColor = m_checked ? m_trueColor : m_falseColor;
244                 int intLineHeight = (this.Height - 2 - 4) / 2;
245 
246                 GraphicsPath path = new GraphicsPath();
247                 path.AddLine(new Point(this.Height / 2, (this.Height - intLineHeight) / 2), new Point(this.Width - this.Height / 2, (this.Height - intLineHeight) / 2));
248                 path.AddArc(new Rectangle(this.Width - this.Height / 2 - intLineHeight - 1, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), -90, 180);
249                 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));
250                 path.AddArc(new Rectangle(this.Height / 2, (this.Height - intLineHeight) / 2, intLineHeight, intLineHeight), 90, 180);
251                 g.FillPath(new SolidBrush(fillColor), path);
252 
253                 if (m_checked)
254                 {
255                     g.FillEllipse(new SolidBrush(fillColor), new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
256                     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));
257                 }
258                 else
259                 {
260                     g.FillEllipse(new SolidBrush(fillColor), new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
261                     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));
262                 }
263             }
264         }
265 
266     }
267 
268     public enum SwitchType
269     {
270         /// <summary>
271         /// 椭圆
272         /// </summary>
273         Ellipse,
274         /// <summary>
275         /// 四边形
276         /// </summary>
277         Quadrilateral,
278         /// <summary>
279         /// 横线
280         /// </summary>
281         Line
282     }
283 }
View Code
 1 namespace HZH_Controls.Controls
 2 {
 3     partial class UCSwitch
 4     {
 5         /// <summary> 
 6         /// 必需的设计器变量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary> 
11         /// 清理所有正在使用的资源。
12         /// </summary>
13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region 组件设计器生成的代码
24 
25         /// <summary> 
26         /// 设计器支持所需的方法 - 不要
27         /// 使用代码编辑器修改此方法的内容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.SuspendLayout();
32             // 
33             // UCSwitch
34             // 
35             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
36             this.BackColor = System.Drawing.Color.Transparent;
37             this.Name = "UCSwitch";
38             this.Size = new System.Drawing.Size(83, 31);
39             this.ResumeLayout(false);
40 
41         }
42 
43         #endregion
44     }
45 }
View Code

 

用处及效果

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

posted @ 2019-08-19 16:08  冰封一夏  阅读(4998)  评论(4编辑  收藏  举报
HZHControls控件库官网:https://www.hzhcontrols.cn