網站首頁 編程語言 正文
實踐過程
效果
代碼
public partial class GlorifyCheckBox : CheckBox
{
public GlorifyCheckBox()
{
InitializeComponent();
FontAspect = getAspect(); //獲取當前控件文本的讀取方向
}
#region 變量
private bool FontAspect = false; //判斷字體的方向
private int Measurement = 255; //設置漸變的初始值
LinearGradientBrush Periphery_br; //外圓的顏色
LinearGradientBrush Central_br; //移入控件時中圓的顏色
LinearGradientBrush NoCentral_br; //無操作時中圓的顏色
#endregion
#region 添加屬性
public enum StyleSort
{
Null = 0, //無
Integer = 1, //整數
Decimal = 2, //小數
}
private Color TPeripheryColor = Color.DarkBlue;
[Browsable(true), Category("設置填充顏色"), Description("外圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color PeripheryColor
{
get { return TPeripheryColor; }
set
{
TPeripheryColor = value;
this.Invalidate();
}
}
private Color TCentralColor = Color.CornflowerBlue;
[Browsable(true), Category("設置填充顏色"), Description("移入控件時中圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color CentralColor
{
get { return TCentralColor; }
set
{
TCentralColor = value;
this.Invalidate();
}
}
private Color TNoCentralColor = Color.PowderBlue;
[Browsable(true), Category("設置填充顏色"), Description("無操作時中圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color NoCentralColor
{
get { return TNoCentralColor; }
set
{
TNoCentralColor = value;
this.Invalidate();
}
}
private Color TStippleColor = Color.DarkBlue;
[Browsable(true), Category("設置填充顏色"), Description("選中后內圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color StippleColor
{
get { return TStippleColor; }
set
{
TStippleColor = value;
this.Invalidate();
}
}
#endregion
#region 事件
/// <summary>
/// 控件在需要重繪時觸發
/// </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; //清除鋸齒
//獲取左面圖標的區域
Rectangle boxrect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y,
SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
//獲取繪制的文本的區域
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; //設置橢圓的位置
strrect.X = e.ClipRectangle.X; //設置字體位置
}
Point MousePos = this.PointToClient(Control.MousePosition); //獲取鼠標的位置
bool Above = e.ClipRectangle.Contains(MousePos); //獲取鼠標是否在當前控件上
DrawBox(e.Graphics, boxrect, Above); //繪制單選圖案
DrawText(e.Graphics, strrect); //繪制文字
if (!Enabled)
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), e.ClipRectangle);
}
/// <summary>
/// 鼠標移入控件的可見區域時觸發
/// </summary>
protected override void OnMouseEnter(System.EventArgs e)
{
base.OnMouseEnter(e);
this.Invalidate();
}
/// <summary>
/// 鼠標移出控件的可見區域時觸發
/// </summary>
protected override void OnMouseLeave(System.EventArgs e)
{
base.OnMouseLeave(e);
this.Invalidate();
}
/// <summary>
/// RightToLeft屬性值更改時發生
/// </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">單選圖案的繪制區域</param>
/// <param Above="bool">斷判鼠標是否在控件上方</param>
private void DrawBox(Graphics g, Rectangle rect, bool Above)
{
//設置外橢圓的漸變色
int opacity = Measurement;
Periphery_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 2, PeripheryColor),
Color.FromArgb(opacity, PeripheryColor), LinearGradientMode.ForwardDiagonal);
//設置中間橢圓形選中時的漸變色
opacity = (int) (.4f * opacity + .5f);
Central_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, CentralColor),
Color.FromArgb(opacity, CentralColor), LinearGradientMode.ForwardDiagonal);
//設置中間橢圓形無操作時的漸變色
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; //獲取字體的高度
//獲取外橢圓的區域
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); //設置內圓的繪制區域
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) //如果是選中狀態
{
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) //如果鼠標移入該控件
{
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">繪制文本的區域</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
}
原文鏈接:https://zhima.blog.csdn.net/article/details/128101882
相關推薦
- 2022-06-04 Jenkins初級應用Publish?Over?SSH插件配置_安裝教程
- 2023-04-12 Blazor實現組件嵌套傳遞值的示例詳解_其它綜合
- 2022-07-29 C++超詳細講解函數對象_C 語言
- 2022-08-14 Android?中TextureView和SurfaceView的屬性方法及示例說明_Android
- 2023-10-14 C/C++實現操作系統進程調度算法,FCFS, RR, SPN, SRT, HRRN
- 2022-05-27 使用Jedis線程池returnResource異常注意事項_Redis
- 2021-12-09 C語言求兩個正整數的最大公約數示例代碼_C 語言
- 2022-09-03 PyQt5實現tableWidget?居中顯示_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支