圓角按鈕c#


介紹

不久前,我試圖找到一個不錯的圓形按鈕控件。但是我找不到一個符合我需要的,所以像一個無聊的程序員一樣,我決定寫我自己的。我“幾乎”完成了它,我猜它可以有更多的功能,但它對我的特殊情況有效,所以我決定利用那個傳統,即“如果它有效,就不要碰它”。

雖然我自己說過,但我覺得這些扣子看起來不錯——你得自己判斷!

Image 1

它是如何工作的

我的類“RoundedButton”繼承自“System.Windows.Forms”。包含一個新屬性列表,用於自定義按鈕的外觀,並且覆蓋了一些方法。

新特性:

  • “邊框半徑”-按鈕的邊緣有多圓潤。
  • " -指定邊緣的寬度。"
  • " -指定邊緣的顏色。"
  • 屬性在鼠標上: “-指定當鼠標在按鈕上時邊緣的寬度。”“-指定當鼠標在按鈕上時邊緣的顏色。” 鼠標點擊屬性: 當按鈕觸發事件“OnMouseDown”時,指定邊緣的顏色

此外,繼承屬性“BackColor”、“FlatAppearance”。MouseDownBackColor”或FlatAppearance。可用於指定按鈕可能狀態下的背景顏色。

值得注意的代碼部分

有三種方法值得一看:

  • 首先“GetRoundPath”: 隱藏,復制CodeGraphicsPath GetRoundPath(矩形矩形,int半徑,浮動寬度) 考慮到邊緣的厚度為“寬度”,此方法返回一個適合“Rect”的GraphicsPath,該路徑具有指定的“半徑”。 隱藏,縮小,復制CodeGraphicsPath GetRoundPath(矩形矩形,int半徑,浮動寬度) { //修復半徑rect大小 radius = (int) Math.Max( (Math.Min(半徑, Math.Min(矩形。寬度,矩形高度))-寬度),1); 浮動r2 =半徑/ 2f; float w2 = width / 2f; GraphicsPath GraphPath = new GraphicsPath(); / /左上的弧 GraphPath.AddArc(矩形。X + w2, Rect.Y + w2,半徑,半徑,180,90); / /右上的弧 GraphPath.AddArc(矩形。X +矩形。寬度-半徑- w2,矩形。y + w2,半徑, 半徑、270、90); / /右下角弧 GraphPath.AddArc(矩形。X +矩形。寬度- w2 -半徑, 矩形。y +矩形。高度- w2 -半徑,半徑,半徑,0,90); / /左下側弧 GraphPath.AddArc(矩形。X + w2,矩形y - w2 +矩形高度-半徑, 半徑、90、90); //關閉線(左) GraphPath.AddLine(矩形。X + w2 = y +高度- r2 - w2 = X + w2,矩形。Y + r2 + w2); 返回GraphPath; } 首先,我使用了GraphicsPath的方法“CloseFigure()”,但其行為遠遠超出了我的預期。增加邊緣厚度的措施是靠近中心的,我不知道原因。
  • 其次,“DrawText”: 隱藏,(Graphics g,RectangleF Rect) 當我試圖通過base.OnPaint()繪制背景顏色時,它沒有像我預期的那樣工作,所以我決定自己繪制背景顏色,但隨后我被迫編寫這個方法,目的是繪制按鈕內的文本。 隱藏,縮小,(Graphics g,RectangleF Rect) { float r2 = BorderRadius / 4f; float w2 = BorderWidth / 2f; Point = new Point(); StringFormat format = new StringFormat(); 開關(TextAlign) { 案例ContentAlignment.TopLeft: 點。X = (int)(矩形。X + r2/2 + w2 + Padding.Left); 點。Y = (int)(矩形。Y + r2/2 + w2 + Padding.Top); 格式。LineAlignment = StringAlignment.Center; 打破; 案例ContentAlignment.TopCenter: 點。X = (int)(矩形。X + Rect.Width / 2 f); 點。Y = (int)(矩形。Y + r2/2 + w2 + Padding.Top); 格式。LineAlignment = StringAlignment.Center; 格式。對齊= StringAlignment.Center; 打破; 案例ContentAlignment.TopRight: 點。X = (int)(矩形。X + Rect.Width - r2/2 - w2 - Padding.Right); 點。Y = (int)(矩形。(Y + r2 / 2 + w2 + Padding.Top); 格式。LineAlignment = StringAlignment.Center; 格式。對齊= StringAlignment.Far; 打破; 案例ContentAlignment.MiddleLeft: 點。X = (int)(矩形。(X + r2 / 2 + w2 + padding .左); 點。Y = (int)(矩形。Y + Rect.Height / 2); 格式。LineAlignment = StringAlignment.Center; 打破; 案例ContentAlignment.MiddleCenter: 點。X = (int)(矩形。X +矩形。寬/ 2); 點。Y = (int)(矩形。Y +矩形. height / 2); 格式。LineAlignment = StringAlignment.Center; 格式。對齊= StringAlignment.Center; 打破; 案例ContentAlignment.MiddleRight: 點。X = (int)(矩形。X + Rect.Width - r2 / 2 - w2 - paddent . right); 點。Y = (int)(矩形。Y +矩形. height / 2); 格式。LineAlignment = StringAlignment.Center; 格式。對齊= StringAlignment.Far; 打破; 案例ContentAlignment.BottomLeft: 點。X = (int)(矩形。(X + r2 / 2 + w2 + padding .左); 點。Y = (int)(矩形。Y + Rect.Height - r2 / 2 - w2 - Padding.Bottom); 格式。LineAlignment = StringAlignment.Center; 打破; 案例ContentAlignment.BottomCenter: 點。X = (int)(矩形。X + Rect.Width / 2); 點。Y = (int)(矩形。Y + Rect.Height - r2 / 2 - w2 - Padding.Bottom); 格式。LineAlignment = StringAlignment.Center; 格式。對齊= StringAlignment.Center; 打破; 案例ContentAlignment.BottomRight: 點。X = (int)(矩形。X + Rect.Width - r2 / 2 - w2 - paddent . right); 點。Y = (int)(矩形。Y + Rect.Height - r2 / 2 - w2 - Padding.Bottom); 格式。LineAlignment = StringAlignment.Center; 格式。對齊= StringAlignment.Far; 打破; 默認值: 打破; } / *調試 使用鋼筆鋼筆=新鋼筆(顏色)。黑色,1)) { g。划線(筆,新點(0,0),點); g。畫直線(筆、點。X 0點。X, point.Y); g。畫直線(筆,0點。Y,點。X, point.Y); } * / 使用(Brush Brush = new SolidBrush(ForeColor)) g。拉繩(文本、字體、筆刷、點、格式); }
  • 最后也是最重要的“OnPaint”是: 此方法被System.Windows.Forms覆蓋。按鈕,並負責繪制按鈕。 隱藏,縮小,Copy Codeprotected override void OnPaint(PaintEventArgs e) { e.Graphics。SmoothingMode = SmoothingMode.HighQuality; RectangleF Rect = new RectangleF(0,0, this)寬度,this.Height); new SolidBrush(this.BackColor); GraphicsPath GraphPath = GetRoundPath(Rect, BorderRadius); 這一點。新建區域(GraphPath); / /畫顏色 如果(IsMouseDown,,! FlatAppearance.MouseDownBackColor.IsEmpty) 使用(Brush mouseDownBrush = new SolidBrush(FlatAppearance.MouseDownBackColor)) e.Graphics。FillPath (mouseDownBrush GraphPath); 否則如果(懸停)! FlatAppearance.MouseOverBackColor.IsEmpty) 使用(Brush overBrush = new SolidBrush(FlatAppearance.MouseOverBackColor)) e.Graphics。FillPath (overBrush GraphPath); 其他的 e.Graphics。FillPath(刷,GraphPath); / /畫邊界 #地區DrawBorder GraphicsPath GraphInnerPath; 鋼筆筆; 如果(IsMouseDown,,! BorderDownColor.IsEmpty) { GraphInnerPath = GetRoundPath(Rect, BorderRadius, BorderDownWidth); (BorderDownColor, BorderDownWidth); } 否則如果(懸停)! BorderOverColor.IsEmpty) { GraphInnerPath = GetRoundPath(Rect, BorderRadius, BorderOverWidth); (BorderOverColor, BorderOverWidth); } 其他的 { GraphInnerPath = GetRoundPath(矩形,邊界半徑,邊界寬度); 鋼筆=新鋼筆(邊框顏色,邊框寬度); } 鋼筆。對齊= PenAlignment.Inset; 如果(pen.Width> 0) e.Graphics。DrawPath(筆,GraphInnerPath); # endregion / /繪制文本 DrawText (e.Graphics矩形); }

如何使用

使用Visual Studio:

    將“圓形按鈕”從工具箱拖放到控件中,在屬性面板上對其進行自定義

如果你想通過代碼使用它,它和System.Windows中的按鈕是一樣的。表單,只需使用包含在RoundedButton中的屬性來定制它。

本文轉載於:http://www.diyabc.com/frontweb/news14662.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM