C# Winform自定义Switch控件

1、创建Switch控件
在控件库中添加用户控件(Window窗体),控件名UcSwitch;
在属性/布局栏中的Size设置为70,30。
2、修改UcSwitch.cs

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

namespace UcLib
{
    [DefaultEvent("CheckedChanged")]
    public partial class UcSwitch : UserControl
    {
        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;
            Texts=new string[2] { "开","关"};
        }
        [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();
            }
        }

        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;
                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));
                    }
                }
            
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用自定义 WinForm 控件来创建自己的 MessageBox,以下是一些实现思路: 1. 创建一个继承自 Form 的类,用于作为自定义 MessageBox 的主界面; 2. 在该界面中添加需要的控件,比如 Label 用于显示提示信息,Button 用于处理用户的选择等; 3. 在调用自定义 MessageBox 的代码中,实例化该 Form 类,并设置其属性和事件处理程序; 4. 在事件处理程序中,获取用户的选择并返回给调用方。 下面是一个简单的示例代码: ```csharp public partial class CustomMessageBox : Form { public CustomMessageBox(string message, string title, MessageBoxButtons buttons) { InitializeComponent(); this.lblMessage.Text = message; this.Text = title; switch (buttons) { case MessageBoxButtons.OK: this.btnOK.Visible = true; break; case MessageBoxButtons.OKCancel: this.btnOK.Visible = true; this.btnCancel.Visible = true; break; case MessageBoxButtons.YesNo: this.btnYes.Visible = true; this.btnNo.Visible = true; break; case MessageBoxButtons.YesNoCancel: this.btnYes.Visible = true; this.btnNo.Visible = true; this.btnCancel.Visible = true; break; } } public DialogResult Result { get; private set; } private void btnOK_Click(object sender, EventArgs e) { this.Result = DialogResult.OK; this.Close(); } private void btnCancel_Click(object sender, EventArgs e) { this.Result = DialogResult.Cancel; this.Close(); } private void btnYes_Click(object sender, EventArgs e) { this.Result = DialogResult.Yes; this.Close(); } private void btnNo_Click(object sender, EventArgs e) { this.Result = DialogResult.No; this.Close(); } } ``` 调用该自定义 MessageBox 的代码示例如下: ```csharp var result = new CustomMessageBox("Are you sure?", "Confirm", MessageBoxButtons.YesNo).ShowDialog(); if (result == DialogResult.Yes) { // Do something } else { // Do something else } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值