C#實(shí)現(xiàn)滑動開關(guān)效果
C#重繪checkbox生成滑動開關(guān),供大家參考,具體內(nèi)容如下
通過調(diào)用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));
}
調(diào)用:
private void checkBox1_Paint(object sender, PaintEventArgs e)
{
RectangleCheckBoxButton(sender, e);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條顯示效果,感興趣的小伙伴可以嘗試一下2022-06-06
Dictionary擴(kuò)展基礎(chǔ)類向字典中添加鍵和值
Dictionary<TKey, TValue> 類是常用的一個基礎(chǔ)類,但用起來有時確不是很方便。本文逐一討論,并使用擴(kuò)展方法解決2013-11-11
unity使用socket編程實(shí)現(xiàn)聊天室功能
這篇文章主要為大家詳細(xì)介紹了unity使用socket編程實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
總結(jié)C#動態(tài)調(diào)用WCF接口的兩種方法
這篇文章給大家總結(jié)了C#動態(tài)調(diào)用WCF接口的兩種方法,大家可以根據(jù)自己的需求選擇對應(yīng)的方式,下面來一起看看。2016-09-09
C#使用Json.Net進(jìn)行序列化和反序列化及定制化
在本篇文章里小編給大家分享了關(guān)于C#使用Json.Net進(jìn)行序列化和反序列化及定制化的知識點(diǎn)總結(jié),需要的朋友們參考學(xué)習(xí)下。2019-05-05
C# 將透明圖片的非透明區(qū)域轉(zhuǎn)換成Region的實(shí)例代碼
以下代碼實(shí)現(xiàn)將一張帶透明度的png圖片的非透明部分轉(zhuǎn)換成Region輸出的方法,有需要的朋友可以參考一下2013-10-10
判斷一個整數(shù)是否是2的N次冪實(shí)現(xiàn)方法
下面小編就為大家分享一篇判斷一個整數(shù)是否是2的N次冪實(shí)現(xiàn)方法,實(shí)例簡潔,具有很好的參考價值。希望對大家有所幫助2017-11-11
C#實(shí)現(xiàn)寫入文本文件內(nèi)容的方法
這篇文章主要介紹了C#實(shí)現(xiàn)寫入文本文件內(nèi)容的方法,涉及C#針對文本文件的判斷、創(chuàng)建及寫入等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07

