C#Checkbox滑动开关效果

本文详细介绍了如何通过VS开发中使用代码自定义WinForm的Checkbox控件,包括引入所需库、绘制圆角矩形的方法、以及切换状态时的颜色和样式变化,使控件外观更美观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平时使用VS开发winform程序的时候难免需要用到checkbox这个控件,但是它并不美观。所以我们就给它美化一下,直接上代码!

首先要引入这两行头部代码,要不然会报错。

using system.drawing.drawing2d;
using system.drawing;

然后就是写入这行代码,这主要是绘制圆角

绘制圆角矩形方法:.
 /// <summary>
        /// 填充圆角矩形
        /// </summary>
        /// <param name="g"></param>
        /// <param name="brush"></param>
        /// <param name="rect"></param>
        /// <param name="cornerRadius"></param>
        public static void FillRoundRectangle(Graphics g, Brush brush, Rectangle rect, int cornerRadius)
        {
            using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
            {
                g.FillPath(brush, path);
            }
        }
        /// <summary>
        /// 圆角矩形路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="cornerRadius"></param>
        /// <returns></returns>
        internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
        {
            GraphicsPath roundedRect = new GraphicsPath();
            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
            roundedRect.CloseFigure();
            return roundedRect;
        }

然后接下来就是checkbox的开关颜色以及样式

重绘代码:
private void RectangleCheckBoxButton(object sender, PaintEventArgs e)
{
            CheckBox rButton = (CheckBox)sender;
            Graphics g = e.Graphics;
            g.Clear(this.BackColor);

            Rectangle RoundRect = new Rectangle(0, 0, 50, 30);            
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //FillRoundRectangle(g, Brushes.White, radioButtonrect, 15);
            

            if (rButton.Checked)
            {
 
                Color color =Color.FromArgb( 55, 197, 90);
                Brush b1 = new SolidBrush(color);
                FillRoundRectangle(g, b1, RoundRect, 15);
                
                using (Pen pen = new Pen(Color.FromArgb(255, 255, 255)))
                {             
                    FillRoundRectangle(g, Brushes.White, new Rectangle(22, 2,26, 26), 13);
                }
            }
            else
            {
                using (Pen pen = new Pen(Color.FromArgb(255, 255, 255)))
                {
                    FillRoundRectangle(g, Brushes.Silver,  RoundRect, 15);
                    FillRoundRectangle(g,Brushes.White, new Rectangle(2, 2, 26, 26), 13);
                    
                }
            }
            Font f = new Font("微软雅黑", 12);
            g.DrawString(((CheckBox)sender).Text,f ,  Brushes.White, new PointF(60, 6));
}

最后在Paint添加事件

private void checkBox1_Paint(object sender, PaintEventArgs e)
{
 RectangleCheckBoxButton(sender, e);//引入上面写好的样式
}

美化后的checkbox就是这样,就比原来好看很多了!

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值