C#實現(xiàn)自定義單選和復(fù)選按鈕樣式
更新時間:2022年12月23日 09:40:47 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)定義單選和復(fù)選按鈕樣式,文中的示例代碼講解詳細(xì),對我們學(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) //從右到左進(jìn)行讀取 tem_Aspect = true; if (this.RightToLeft == RightToLeft.No) //從左到右進(jìn)行讀取 tem_Aspect = false; return tem_Aspect; } #endregion }
到此這篇關(guān)于C#實現(xiàn)自定義單選和復(fù)選按鈕樣式的文章就介紹到這了,更多相關(guān)C#自定義按鈕樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用JavaScriptSerializer序列化時的時間類型處理
這篇文章主要為大家詳細(xì)介紹了C#使用JavaScriptSerializer序列化時的時間類型處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08C#實現(xiàn)JSON和對象之間互相轉(zhuǎn)換功能示例
這篇文章主要介紹了C#實現(xiàn)JSON和對象之間互相轉(zhuǎn)換功能,結(jié)合實例形式較為詳細(xì)的分析了C#實現(xiàn)對象與json之間相互轉(zhuǎn)換的操作技巧,需要的朋友可以參考下2017-09-09C#簡單查詢SQLite數(shù)據(jù)庫是否存在數(shù)據(jù)的方法
這篇文章主要介紹了C#簡單查詢SQLite數(shù)據(jù)庫是否存在數(shù)據(jù)的方法,涉及C#調(diào)用SQLite組件及針對SQLite數(shù)據(jù)庫基本的連接、查詢、關(guān)閉等使用技巧,需要的朋友可以參考下2016-07-07C# DataSet查看返回結(jié)果集的實現(xiàn)
這篇文章主要介紹了C# DataSet查看返回結(jié)果集的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10C#實現(xiàn)WinForm禁止最大化、最小化、雙擊標(biāo)題欄、雙擊圖標(biāo)等操作的方法
這篇文章主要介紹了C#實現(xiàn)WinForm禁止最大化、最小化、雙擊標(biāo)題欄、雙擊圖標(biāo)等操作的方法,涉及C#使用WinForm針對窗口操作的各種常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08