C#实现滑动开关效果
作者:qq53716684 时间:2023-11-26 22:18:29
C#重绘checkbox生成滑动开关,供大家参考,具体内容如下
通过调用checkbox控件的paint事件,在重绘事件里判断checked属性,如果选中绘制选中图形,如果未选中绘制未选中图形。
效果图:
绘制圆角矩形方法:
/// <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;
}
重绘代码:
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));
}
调用:
private void checkBox1_Paint(object sender, PaintEventArgs e)
{
RectangleCheckBoxButton(sender, e);
}
来源:https://blog.csdn.net/yx1234321/article/details/106207819
标签:C#,滑动开关
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现的具有GUI的校园导航系统的完整代码
2022-06-28 03:43:56
![](https://img.aspxhome.com/file/2023/2/104012_0s.png)
一文搞懂Java中的注解和反射
2022-04-04 20:55:31
Java 高并发十: JDK8对并发的新支持详解
2022-12-02 02:43:09
![](https://img.aspxhome.com/file/2023/6/75506_0s.png)
Javassist之一秒理解java动态编程
2023-11-09 09:36:33
![](https://img.aspxhome.com/file/2023/7/58897_0s.png)
spring boot 动态生成接口实现类的场景分析
2022-12-14 05:09:33
![](https://img.aspxhome.com/file/2023/7/64207_0s.png)
Java毕业设计实战之在线网盘系统的实现
2023-03-05 05:42:47
![](https://img.aspxhome.com/file/2023/3/72523_0s.jpg)
mybatis报错元素内容必须由格式正确的字符数据或标记组成异常的解决办法
2023-01-10 15:46:37
Java日常练习题,每天进步一点点(24)
2022-11-17 06:40:40
![](https://img.aspxhome.com/file/2023/3/96043_0s.png)
java处理按钮点击事件的方法
2021-08-24 16:28:10
Android为View添加拖放效果的方法实例
2023-06-25 18:53:45
C++ 中二分查找递归非递归实现并分析
2023-06-19 06:51:31
![](https://img.aspxhome.com/file/2023/9/128169_0s.png)
Java开启JMX远程监控服务配置
2021-11-02 01:14:43
![](https://img.aspxhome.com/file/2023/2/72332_0s.png)
SpringBoot配置actuator的代码
2023-02-04 04:45:06
SpringBoot入坑笔记之spring-boot-starter-web 配置文件的使用
2021-12-15 13:11:46
![](https://img.aspxhome.com/file/2023/0/78370_0s.png)
Spring Boot 集成Dubbo框架实例
2022-02-03 21:23:27
C语言运算符及其优先级汇总表口诀
2021-12-12 21:14:27
基于AForge实现C#摄像头视频录制功能
2023-08-25 02:21:26
![](https://img.aspxhome.com/file/2023/6/89746_0s.png)
详解配置spring-boot-actuator时候遇到的一些小问题
2022-03-17 17:04:08
Android自定义processor实现bindView功能的实例
2023-09-07 17:16:37
![](https://img.aspxhome.com/file/2023/1/139621_0s.png)
Java递归运行的机制:递归的微观解读图文分析
2022-09-24 20:04:09
![](https://img.aspxhome.com/file/2023/5/73035_0s.png)