C#實現滑動開關效果
更新時間:2021年07月26日 11:08:10 作者:qq53716684
這篇文章主要為大家詳細介紹了C#實現滑動開關效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
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);
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#實現給DevExpress中GridView表格指定列添加進度條
這篇文章主要為大家詳細介紹了如何利用C#實現給DevExpress中GridView表格指定列添加進度條顯示效果,感興趣的小伙伴可以嘗試一下2022-06-06
C# 將透明圖片的非透明區(qū)域轉換成Region的實例代碼
以下代碼實現將一張帶透明度的png圖片的非透明部分轉換成Region輸出的方法,有需要的朋友可以參考一下2013-10-10

