C#實(shí)現(xiàn)滑動(dòng)開關(guān)效果
更新時(shí)間:2021年07月26日 11:08:10 作者:qq53716684
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)滑動(dòng)開關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
C#重繪checkbox生成滑動(dòng)開關(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);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)Word文檔轉(zhuǎn)Markdown格式的示例代碼
文檔格式的多樣性豐富了我們的信息交流手段,其中Word文檔因其強(qiáng)大的功能性而廣受歡迎,Markdown因其簡(jiǎn)潔、易于閱讀和編輯的特性而展現(xiàn)出獨(dú)特的優(yōu)勢(shì),本文將介紹如何在.NET平臺(tái)使用C#代碼實(shí)現(xiàn)Word文檔到Markdown格式的轉(zhuǎn)換,需要的朋友可以參考下2024-04-04
C#使用Socket進(jìn)行簡(jiǎn)單的通訊的示例代碼
Socket 類是基于與 Linux、macOS 或 Windows 的本機(jī)互操作性提供的托管代碼版本的套接字服務(wù),提供了一系列的接口來支持應(yīng)用層的調(diào)用,下面我們就來學(xué)習(xí)一下如何使用Socket進(jìn)行簡(jiǎn)單的通訊,需要的可以參考下2023-12-12
C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解
這篇文章主要介紹了C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06

