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

前提

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

官网:https://www.hzhcontrols.cn

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

码云:HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~

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

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

目录

c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客

准备工作

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         }

完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace HZH_Controls.Controls
{
    [DefaultEvent("CheckedChanged")]
    public partial class UCSwitch : UserControl
    {
        [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();
            }
        }


        public UCSwitch()
        {
            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;
        }

        void UCSwitch_MouseDown(object sender, MouseEventArgs e)
        {
            Checked = !Checked;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SetGDIHigh();
            if (m_switchType == HZH_Controls.Controls.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 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 - (int)sizeF.Width / 2, intTextY));
                    }
                }
            }
            else if (m_switchType == HZH_Controls.Controls.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
    }
}
namespace HZH_Controls.Controls
{
    partial class UCSwitch
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // UCSwitch
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.Transparent;
            this.Name = "UCSwitch";
            this.Size = new System.Drawing.Size(83, 31);
            this.ResumeLayout(false);

        }

        #endregion
    }
}

用处及效果

最后的话

如果你喜欢的话,请到 HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~ 点个星 星吧

WinForms中的自定义控件开发允许开发者创建具有特定功能的控件,这些控件可以用于多种应用程序。仪表盘控件是一种常见的自定义控件,它模拟物理仪表盘,用于显示从简单到复杂的各种数据。在C#中开发WinForms仪表盘控件通常涉及以下几个步骤: 1. **创建控件类**:首先,你需要继承自`UserControl`类,创建一个新的类,这个类将作为基础来定义你的仪表盘控件。 2. **设计界面**:在类中,使用设计器或代码来绘制控件的用户界面。这可能包括刻度、指针和数据标签等元素。 3. **编写业务逻辑**:实现控件的数据绑定和逻辑处理,这可能包括如何读取和显示数据,以及如何响应用户的交互。 4. **属性和事件**:定义公共属性来获取和设置控件的外观和行为,如颜色、范围等,并且创建事件以允许外部代码响应特定的用户操作,如值改变等。 5. **测试和调试**:在完成开发后,需要对控件进行彻底的测试,确保在不同情况下都能正确工作。 以下是一个简单的示例代码,展示了如何创建一个基础的仪表盘控件: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class DashboardControl : UserControl { private float value = 0; // 仪表盘的当前值 // 公共属性,允许外部设置仪表盘值 public float Value { get { return value; } set { if (value != this.value) { this.value = value; Invalidate(); // 重绘控件 } } } // 绘制控件 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; // 绘制仪表盘的刻度和指针等 // ... } // 其他方法,如响应用户事件等 } ``` 开发自定义控件是一个复杂的过程,它需要深入了解WinForms框架、GDI+绘图以及可能涉及的动画和数据绑定技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值