Winform中实现自定义水晶按钮控件(附代码下载)
生活随笔
收集整理的這篇文章主要介紹了
Winform中实现自定义水晶按钮控件(附代码下载)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
效果
?
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
新建一個用戶控件TransparencyButton,修改其代碼如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D;namespace 自定義水晶按鈕控件 {public partial class TransparencyButton : UserControl{public TransparencyButton(){InitializeComponent();}#region 公共變量public static SmoothingMode sm;public static bool pub_ButtonClick = true;//判斷按鈕是否按下(false為按下)public static int pub_Degree = 0;//記錄四角弧度的大小范圍public static int pub_RGB_r0 = 0x130;//按鈕背景的R色值public static int pub_RGB_r1 = 0x99;//按鈕其它顏色的R色值#endregion#region 添加屬性private string BNText = "";[Browsable(true), Category("透明按鈕的屬性設置"), Description("設置顯示的文本")]?? //在“屬性”窗口中顯示NText屬性public string NText{get { return BNText; }set{BNText = value;if (BNText.Length > 0)Invalidate();}}private int BDegree = 1;[Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕四個角的弧度")]?? //在“屬性”窗口中顯示Degree屬性public int Degree{get { return BDegree; }set{BDegree = value;if (this.Width >= this.Height)pub_Degree = (int)(this.Height / 2);elsepub_Degree = (int)(this.Width / 2);if (BDegree <= 0)BDegree = 1;if (BDegree > pub_Degree)BDegree = pub_Degree;if (BDegree > 0)Invalidate();}}private Color DShineColor = Color.Black;[Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕的光澤度顏色")]?? //在“屬性”窗口中顯示ShineColor屬性public Color ShineColor{get { return DShineColor; }set{DShineColor = value;Invalidate();}}private Color DUndersideShine = Color.LightGray;[Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕的下部的光澤度")]?? //在“屬性”窗口中顯示UndersideShine屬性public Color UndersideShine{get { return DUndersideShine; }set{DUndersideShine = value;Invalidate();}}private int DCTransparence = 0;[Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕的透明度數")]?? //在“屬性”窗口中顯示CTransparence屬性public int CTransparence{get { return DCTransparence; }set{DCTransparence = value;if (DCTransparence > 20)DCTransparence = 20;if (DCTransparence < 0)DCTransparence = 0;if (DCTransparence >= 0)Invalidate();}}private int DCFontDeepness = 1;[Browsable(true), Category("透明按鈕的屬性設置"), Description("設置按鈕文本的深度")]?? //在“屬性”窗口中顯示CFontDeepness屬性public int CFontDeepness{get { return DCFontDeepness; }set{DCFontDeepness = value;if (DCFontDeepness > 20)DCFontDeepness = 20;if (DCFontDeepness < 0)DCFontDeepness = 0;if (DCFontDeepness >= 0)Invalidate();}}#endregion#region 事件private void TransparencyButton_Paint(object sender, PaintEventArgs e){this.BackColor = Color.Transparent;//使當前控件透明sm = e.Graphics.SmoothingMode;//設置呈現質量Color shineColor = Color.Black;Rectangle rect2 = new Rectangle(0, 0, this.Width, this.Height);//設置繪制按鈕的矩形區域Rectangle rect1 = new Rectangle(0, this.Height / 2, this.Width, this.Height / 2);//設置繪制按鈕下半部的矩形區域if (this.CTransparence == 0)//如果按鈕的透明度為0{CobOblongDown(rect2, e.Graphics);//繪制按扭的背景CobOblong(rect2, e.Graphics, this.ShineColor);//繪制按扭的背景}else{if (this.CTransparence > 0)//如果按鈕的透明度不為0{CobOblongDown(rect2, e.Graphics);//繪制按扭的背景for (int i = 0; i < CTransparence; i++){CobOblong(rect2, e.Graphics, this.ShineColor);//繪制按扭的背景顏色}}}int tem_n = (int)(this.CTransparence / 3);//獲取一個值,用于設置下半部按鈕的顏色深度if (tem_n == 0)//如果為0CobAjar(rect1, e.Graphics, this.ShineColor);//繪制按扭的下半部背景else{if (tem_n > 0)//如果不為0{for (int i = 0; i < tem_n; i++)//加深下部按鈕的顏色{CobAjar(rect1, e.Graphics, this.ShineColor);//繪制按扭的下半部背景顏色}}}CobOblong(rect2, e.Graphics, this.UndersideShine);//設置下半部按鈕的光澤度if (pub_ButtonClick == false)//判斷按鈕是否按下(false為按下){CobOblongDown(rect2, e.Graphics);//繪制按扭的背景}if (this.NText.Length > 0)//如果Text屬性中有值ProtractText(e.Graphics);//繪制透明按鈕的文本信息}private void TransparencyButton_SizeChanged(object sender, EventArgs e){Invalidate();//對控件進行重繪}private void TransparencyButton_MouseDown(object sender, MouseEventArgs e){pub_ButtonClick = false;//按下按鈕Invalidate();//對控件進行重繪}private void TransparencyButton_MouseUp(object sender, MouseEventArgs e){pub_ButtonClick = true;//松開按鈕Invalidate();//對控件進行重繪}#endregion#region 自定義方法/// <summary>/// 繪制透明按扭的文本/// </summary>/// <param g="Graphics">封裝一個繪圖的類對象</param>private void ProtractText(Graphics g){Graphics TitG = this.CreateGraphics();//創建Graphics類對象string TitS = this.NText;//獲取圖表標題的名稱SizeF TitSize = TitG.MeasureString(TitS, this.Font);//將繪制的字符串進行格式化float TitWidth = TitSize.Width;//獲取字符串的寬度float TitHeight = TitSize.Height;//獲取字符串的高度float TitX = 0;//標題的橫向坐標float TitY = 0;//標題的縱向坐標if (this.Height > TitHeight)//如果按鈕的高度大于文本的高度TitY = (this.Height - TitHeight) / 2;//使文本水平方向局中elseTitY = this.BDegree;//文本置頂if (this.Width > TitWidth)//如果按鈕的寬度大于文本的寬度TitX = (this.Width - TitWidth) / 2;//使文本水平局中elseTitX = this.BDegree;//文本置左//設置文本的繪制區域Rectangle rect = new Rectangle((int)Math.Floor(TitX), (int)Math.Floor(TitY), (int)Math.Ceiling(TitWidth), (int)Math.Ceiling(TitHeight));int opacity = pub_RGB_r1;//設置R色值opacity = (int)(.4f * opacity + .5f);//設置漸變值for (int i = 0; i < DCFontDeepness; i++)//設置文本的深度{//設置文本的漸變顏色using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity, this.ForeColor), Color.FromArgb(opacity, this.ForeColor), LinearGradientMode.Vertical)){g.DrawString(TitS, this.Font, br, new PointF(TitX, TitY));//繪制帶有漸變效果的文本}}}/// <summary>/// 繪制透明按扭的背景色/// </summary>/// <param rect="Rectangle">繪制按鈕的區域</param>/// <param g="Graphics">封裝一個繪圖的類對象</param>/// <param fillColor="Color">填充的顏色</param>private void CobOblong(Rectangle rect, Graphics g, Color fillColor){using (GraphicsPath bh = CreateCobOblong(rect, this.BDegree))//繪制一個圓角矩形{int opacity = pub_RGB_r0;//設置按鈕的R色值opacity = (int)(.4f * opacity + .5f);//設置漸變的變化值//設置按鈕的漸變顏色using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, fillColor), Color.FromArgb(opacity, fillColor), LinearGradientMode.Vertical)){g.FillPath(br, bh);//填充按鈕背景}g.SmoothingMode = sm;//設置呈現的質量}}/// <summary>/// 繪制透明按扭的下半部背景色/// </summary>/// <param rect="Rectangle">繪制按鈕的下半部區域</param>/// <param g="Graphics">封裝一個繪圖的類對象</param>/// <param fillColor="Color">填充的顏色</param>private void CobAjar(Rectangle rect, Graphics g, Color fillColor){using (GraphicsPath bh = CreateCobAjar(rect, this.BDegree)){int opacity = pub_RGB_r1;opacity = (int)(.4f * opacity + .5f);using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity, fillColor), Color.FromArgb(pub_RGB_r1 / 5, fillColor), LinearGradientMode.Vertical)){g.FillPath(br, bh);//填充按鈕背景}g.SmoothingMode = sm;//設置呈現的質量}}/// <summary>/// 繪制透明按扭按下時的效果/// </summary>/// <param rect="Rectangle">繪制按鈕的區域</param>/// <param g="Graphics">封裝一個繪圖的類對象</param>private void CobOblongDown(Rectangle rect, Graphics g){using (GraphicsPath bh = CreateCobOblong(rect, this.BDegree))//按鈕的圓角繪制{int opacity = pub_RGB_r1;//設置按鈕的R色值Color tem_Color = Color.Black;//設置按鈕的背景顏色為黑色if (pub_ButtonClick == true)//如果按鈕沒有按下{opacity = pub_RGB_r0;//設置按鈕的R色值tem_Color = Color.White;//設置按鈕的背景顏色為白色}opacity = (int)(.4f * opacity + .5f);//設置漸變的變化值//設置按鈕的漸變顏色using (LinearGradientBrush br = new LinearGradientBrush(rect, Color.FromArgb(opacity + 20, tem_Color), Color.FromArgb(opacity, tem_Color), LinearGradientMode.Vertical)){g.FillPath(br, bh);//填充按鈕背景}g.SmoothingMode = sm;//設置呈現的質量}}/// <summary>/// 按鈕的圓角繪制/// </summary>/// <param rect="Rectangle">繪制按鈕的區域</param>/// <param radius="int">圓角的度數</param>private static GraphicsPath CreateCobOblong(Rectangle rectangle, int radius){GraphicsPath path = new GraphicsPath();//實例化GraphicsPath類int l = rectangle.Left;//獲取矩形左上角的X坐標int t = rectangle.Top;//獲取矩形左上角的Y坐標int w = rectangle.Width;//獲取矩形的寬度int h = rectangle.Height;//獲取矩形的高度path.AddArc(l, t, 2 * radius, 2 * radius, 180, 90);//在矩形的左上角繪制圓角path.AddLine(l + radius, t, l + w - radius, t);//繪制左上角圓角與右上角之間的線段path.AddArc(l + w - 2 * radius, t, 2 * radius, 2 * radius, 270, 90);//繪制右上角的圓角path.AddLine(l + w, t + radius, l + w, t + h - radius);//繪制左上角、右上角和右下角所形成的三角形path.AddArc(l + w - 2 * radius, t + h - 2 * radius, 2 * radius, 2 * radius, 0, 90);//繪制右下角圓角path.AddLine(l + radius, t + h, l + w - radius, t + h);//繪制右下角圓角與左上角圓之間的線段path.AddArc(l, t + h - 2 * radius, 2 * radius, 2 * radius, 90, 90);//繪制左下角的圓角path.AddLine(l, t + radius, l, t + h - radius);//繪制左上角、左下角和右下角之間的三角形return path;}/// <summary>/// 按鈕的下半個圓角繪制/// </summary>/// <param rect="Rectangle">繪制下半部按鈕的區域</param>/// <param radius="int">圓角的度數</param>private static GraphicsPath CreateCobAjar(Rectangle rectangle, int radius){GraphicsPath path = new GraphicsPath();int l = rectangle.Left;//獲取矩形左上角的X坐標int t = rectangle.Top;//獲取矩形左上角的Y坐標int w = rectangle.Width;//獲取矩形的寬度int h = rectangle.Height;//獲取矩形的高度path.AddArc(l, t, 2 * radius, 2 * radius, 0, 0);//繪制左上角的點path.AddLine(l, t, l + w, t);//繪制左上角與右上角之間的線段path.AddArc(l + w, t, 2 * radius, 2 * radius, 0, 0);//繪制右上角的點path.AddLine(l + w, t + radius, l + w, t + h - radius);//繪制左上角、右上角和右下角所形成的三角形path.AddArc(l + w - 2 * radius, t + h - 2 * radius, 2 * radius, 2 * radius, 0, 90);//繪制右下角圓角path.AddLine(l + radius, t + h, l + w - radius, t + h);//繪制右下角圓角與左上角圓之間的線段path.AddArc(l, t + h - 2 * radius, 2 * radius, 2 * radius, 90, 90);//繪制左下角的圓角path.AddLine(l, t + radius, l, t + h - radius);//繪制左上角、左下角和右下角之間的三角形return path;}#endregion} }然后新建一個窗體Form1然后拖拽一個新建的自定義水晶控件
?
代碼下載
https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12241085
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Winform中实现自定义水晶按钮控件(附代码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Winform中使用用户控件实现带行数和
- 下一篇: Winform中实现对照片添加文字和图片