C#實現(xiàn)自定義單選和復(fù)選按鈕樣式
更新時間:2022年12月23日 09:40:47 作者:芝麻粒兒
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)定義單選和復(fù)選按鈕樣式,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實踐過程
效果

代碼
public partial class GlorifyCheckBox : CheckBox
{
public GlorifyCheckBox()
{
InitializeComponent();
FontAspect = getAspect(); //獲取當(dāng)前控件文本的讀取方向
}
#region 變量
private bool FontAspect = false; //判斷字體的方向
private int Measurement = 255; //設(shè)置漸變的初始值
LinearGradientBrush Periphery_br; //外圓的顏色
LinearGradientBrush Central_br; //移入控件時中圓的顏色
LinearGradientBrush NoCentral_br; //無操作時中圓的顏色
#endregion
#region 添加屬性
public enum StyleSort
{
Null = 0, //無
Integer = 1, //整數(shù)
Decimal = 2, //小數(shù)
}
private Color TPeripheryColor = Color.DarkBlue;
[Browsable(true), Category("設(shè)置填充顏色"), Description("外圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color PeripheryColor
{
get { return TPeripheryColor; }
set
{
TPeripheryColor = value;
this.Invalidate();
}
}
private Color TCentralColor = Color.CornflowerBlue;
[Browsable(true), Category("設(shè)置填充顏色"), Description("移入控件時中圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color CentralColor
{
get { return TCentralColor; }
set
{
TCentralColor = value;
this.Invalidate();
}
}
private Color TNoCentralColor = Color.PowderBlue;
[Browsable(true), Category("設(shè)置填充顏色"), Description("無操作時中圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color NoCentralColor
{
get { return TNoCentralColor; }
set
{
TNoCentralColor = value;
this.Invalidate();
}
}
private Color TStippleColor = Color.DarkBlue;
[Browsable(true), Category("設(shè)置填充顏色"), Description("選中后內(nèi)圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color StippleColor
{
get { return TStippleColor; }
set
{
TStippleColor = value;
this.Invalidate();
}
}
#endregion
#region 事件
/// <summary>
/// 控件在需要重繪時觸發(fā)
/// </summary>
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.FillRectangle(SystemBrushes.Control, e.ClipRectangle); //填充矩形
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //清除鋸齒
//獲取左面圖標(biāo)的區(qū)域
Rectangle boxrect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y,
SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
//獲取繪制的文本的區(qū)域
Rectangle strrect = new Rectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width,
e.ClipRectangle.Y, e.ClipRectangle.Width + 5 - SystemInformation.SmallIconSize.Width,
e.ClipRectangle.Height);
if (FontAspect) //判斷字體的讀取方式
{
boxrect.X = e.ClipRectangle.X + e.ClipRectangle.Width - SystemInformation.SmallIconSize.Width; //設(shè)置橢圓的位置
strrect.X = e.ClipRectangle.X; //設(shè)置字體位置
}
Point MousePos = this.PointToClient(Control.MousePosition); //獲取鼠標(biāo)的位置
bool Above = e.ClipRectangle.Contains(MousePos); //獲取鼠標(biāo)是否在當(dāng)前控件上
DrawBox(e.Graphics, boxrect, Above); //繪制單選圖案
DrawText(e.Graphics, strrect); //繪制文字
if (!Enabled)
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), e.ClipRectangle);
}
/// <summary>
/// 鼠標(biāo)移入控件的可見區(qū)域時觸發(fā)
/// </summary>
protected override void OnMouseEnter(System.EventArgs e)
{
base.OnMouseEnter(e);
this.Invalidate();
}
/// <summary>
/// 鼠標(biāo)移出控件的可見區(qū)域時觸發(fā)
/// </summary>
protected override void OnMouseLeave(System.EventArgs e)
{
base.OnMouseLeave(e);
this.Invalidate();
}
/// <summary>
/// RightToLeft屬性值更改時發(fā)生
/// </summary>
protected override void OnRightToLeftChanged(System.EventArgs e)
{
base.OnRightToLeftChanged(e);
FontAspect = getAspect();
this.Invalidate();
}
#endregion
#region 方法
/// <summary>
/// 繪制單選控件的圖案
/// </summary>
/// <param g="Graphics">封裝一個繪圖的類對象</param>
/// <param rect="Rectangle">單選圖案的繪制區(qū)域</param>
/// <param Above="bool">斷判鼠標(biāo)是否在控件上方</param>
private void DrawBox(Graphics g, Rectangle rect, bool Above)
{
//設(shè)置外橢圓的漸變色
int opacity = Measurement;
Periphery_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 2, PeripheryColor),
Color.FromArgb(opacity, PeripheryColor), LinearGradientMode.ForwardDiagonal);
//設(shè)置中間橢圓形選中時的漸變色
opacity = (int) (.4f * opacity + .5f);
Central_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, CentralColor),
Color.FromArgb(opacity, CentralColor), LinearGradientMode.ForwardDiagonal);
//設(shè)置中間橢圓形無操作時的漸變色
opacity = (int) (.4f * opacity + .5f);
NoCentral_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, NoCentralColor),
Color.FromArgb(opacity, NoCentralColor), LinearGradientMode.ForwardDiagonal);
int size = this.Font.Height; //獲取字體的高度
//獲取外橢圓的區(qū)域
Rectangle box = new Rectangle(rect.X + ((rect.Width - size) / 2), rect.Y + ((rect.Height - size) / 2),
size - 2, size - 2);
Rectangle glyph = new Rectangle(box.X + 3, box.Y + 3, box.Width - 6, box.Height - 6); //設(shè)置內(nèi)圓的繪制區(qū)域
Rectangle right = new Rectangle(box.X, box.Y - 1, box.Width + 2, box.Height + 2);
g.FillEllipse(new SolidBrush(SystemColors.Window), box); //以白色填充單選圖案
if (this.CheckState != CheckState.Unchecked) //如果是選中狀態(tài)
{
base.ForeColor = Color.DarkBlue;
ControlPaint.DrawMenuGlyph(g, right, MenuGlyph.Checkmark, this.StippleColor, Color.White); //繪制對號
g.DrawRectangle(new Pen(new SolidBrush(SystemColors.Control), (float) (3)), box); //繪制外橢圓
}
if (this.CheckState == CheckState.Indeterminate)
g.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), right);
if (Above && this.Enabled) //如果鼠標(biāo)移入該控件
{
g.DrawRectangle(new Pen(Central_br, 2),
new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //繪制中心橢圓
}
else
{
g.DrawRectangle(new Pen(NoCentral_br, 2),
new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //繪制中心橢圓
}
g.DrawRectangle(new Pen(Periphery_br, (float) (1.5)), box); //繪制外橢圓
}
/// <summary>
/// 繪制文本
/// </summary>
/// <param g="Graphics">封裝一個繪圖的類對象</param>
/// <param rect="Rectangle">繪制文本的區(qū)域</param>
private void DrawText(Graphics g, Rectangle rect)
{
StringFormat tem_StringF = new StringFormat();
tem_StringF.Alignment = StringAlignment.Near;
tem_StringF.LineAlignment = StringAlignment.Center; //文本居中對齊
if (FontAspect)
tem_StringF.FormatFlags = StringFormatFlags.DirectionRightToLeft; //按從左到右的順序顯示文本
//g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);//繪制文本
if (!FontAspect)
g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF); //繪制文本
else
{
rect.X = rect.X - SystemInformation.SmallIconSize.Width / 2 + 2;
g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);
}
}
/// <summary>
/// 獲取文本的讀取方向
/// </summary>
/// <return>布爾型</return>
private bool getAspect()
{
bool tem_Aspect = SystemInformation.RightAlignedMenus;
if (this.RightToLeft == RightToLeft.Yes) //從右到左進行讀取
tem_Aspect = true;
if (this.RightToLeft == RightToLeft.No) //從左到右進行讀取
tem_Aspect = false;
return tem_Aspect;
}
#endregion
}
到此這篇關(guān)于C#實現(xiàn)自定義單選和復(fù)選按鈕樣式的文章就介紹到這了,更多相關(guān)C#自定義按鈕樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
UnityShader3實現(xiàn)轉(zhuǎn)圈與冷卻效果
這篇文章主要為大家詳細介紹了UnityShader3實現(xiàn)轉(zhuǎn)圈與冷卻效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
WPF+ASP.NET?SignalR實現(xiàn)簡易在線聊天功能的示例代碼
這篇文章將以一個簡單的聊天示例,簡述如何通過WPF+ASP.NET?SignalR實現(xiàn)消息后臺通知,僅供學(xué)習(xí)分享使用,如有不足之處,還請指正2022-09-09
C#使用HtmlAgilityPack抓取糗事百科內(nèi)容實例
這篇文章主要介紹了C#使用HtmlAgilityPack抓取糗事百科內(nèi)容的方法,實例分析了C#中HtmlAgilityPack的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07

