C#實現(xiàn)自定義圓角按鈕的方法
Winform中自帶的button沒有圓角屬性,所以我們繼承Button類,重寫OnPaint事件來繪制圓角按鈕。
1.繪制圓角按鈕框需要用到系統(tǒng)自帶的繪制方法:首先引入Gdi32.dll中的CreateRoundRectRgn方法。經(jīng)過驗證此方法在大量控件情況下使用,會導(dǎo)致GDI+繪圖問題!現(xiàn)在改用自己繪制的圓角GraphicsPath進行填充顯示,效果更好,圓角更圓潤了!
重寫OnPaint方法:
protected override void OnPaint(PaintEventArgs pe) { if (!roundCorner) { base.OnPaint(pe); return; } Graphics g = pe.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; //g.SmoothingMode = SmoothingMode.HighQuality; //g.CompositingQuality = CompositingQuality.HighQuality; //g.InterpolationMode = InterpolationMode.HighQualityBicubic; Rectangle rect = this.ClientRectangle; Brush brhBorder = new SolidBrush(crBorderPainting); Brush brhRect = new SolidBrush(BackColor); Brush b0 = new SolidBrush(this.Parent.BackColor); Brush bfont = new SolidBrush(ForeColor); try { g.Clear(this.Parent.BackColor); int borderSize = FlatAppearance.BorderSize; try { GraphicsPath path = CreateRoundRect(rect.Left, rect.Top, rect.Left + rect.Width - borderSize, rect.Top + rect.Height - borderSize); g.FillPath(brhBorder, path); path.Dispose(); path = CreateRoundRect(rect.Left + borderSize /2f, rect.Top + borderSize / 2f, rect.Left + rect.Width - borderSize * 2, rect.Top + rect.Height - borderSize * 2); g.FillPath(brhRect, path); path.Dispose(); } catch (Exception e) { Console.WriteLine("FillPath:" + e.Message); } if (this.Text != string.Empty) { StringFormat sf = StringFormat.GenericTypographic; sf.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; SizeF sizeoftext = g.MeasureString(this.Text, Font); float tx = (float)((this.Width - sizeoftext.Width) / 2.0); float ty = (float)((this.Height - sizeoftext.Height) / 2.0); g.DrawString(this.Text, Font, bfont, tx, ty); } } finally { b0.Dispose(); brhBorder.Dispose(); brhRect.Dispose(); bfont.Dispose(); } }
private GraphicsPath CreateRoundRect(float rleft, float rtop, float rwidth, float rheight) { float r = radius; if (rwidth < rheight) { if (radius > rwidth / 2f) r = rwidth / 2f; } else { if (radius > rheight / 2f) r = rheight / 2f; } GraphicsPath path; RectangleF rectRow = new RectangleF(rleft, rtop + r, rwidth, rheight - r * 2); RectangleF rectColumn = new RectangleF(rleft + r, rtop, rwidth - r * 2, rheight); path = new GraphicsPath(FillMode.Winding); path.AddRectangle(rectRow); path.AddRectangle(rectColumn); //左上 path.AddEllipse(rleft, rtop, r * 2, r * 2); //右上 path.AddEllipse(rleft + rwidth - r * 2, rtop, r * 2, r * 2); //左下 path.AddEllipse(rleft, rtop + rheight - r * 2, r * 2, r * 2); //右下 path.AddEllipse(rleft + rwidth - r * 2, rtop + rheight - r * 2, r * 2, r * 2); return path; }
2.如果需要增加懸浮效果,可以重寫OnMouseEnter、OnMouseLeave事件來改變邊界及背景色。
private Color crBorderActive = Color.FromArgb(Convert.ToInt32("FF3283C4", 16)); private Color crRectActive = Color.FromArgb(Convert.ToInt32("FFE3F3FB", 16)); private Color crBorderDefault = Color.FromArgb(215, 215, 215); protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); this.BackColor = crRectActive; this.FlatAppearance.BorderColor = crBorderActive; } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); this.BackColor = Color.White; this.FlatAppearance.BorderColor = crBorderDefault; }
至此一個圓角按鈕就完成了^_^。效果如下:
到此這篇關(guān)于C#實現(xiàn)自定義圓角按鈕的文章就介紹到這了,更多相關(guān)C#圓角按鈕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 漢字轉(zhuǎn)拼音實例(支持GB2312字符集中所有漢字)
本篇文章主要介紹了C# 漢字轉(zhuǎn)拼音實例(支持GB2312字符集中所有漢字) ,非常具有實用價值,需要的朋友可以參考下。2016-12-12如何在C#中使用 CancellationToken 處理異步任務(wù)
這篇文章主要介紹了如何在C#中使用 CancellationToken 處理異步任務(wù),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#反射調(diào)用dll文件中的方法操作泛型與屬性字段
這篇文章介紹了C#反射調(diào)用dll文件中的方法操作泛型與屬性字段,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05Silverlight將圖片轉(zhuǎn)換為byte的實現(xiàn)代碼
這篇文章主要介紹了Silverlight將圖片轉(zhuǎn)換為byte的實現(xiàn)代碼,需要的朋友可以參考下2015-11-11C#延時關(guān)閉電腦、取消關(guān)閉電腦操作方法(需管理員權(quán)限)
在C#中,如果想實現(xiàn)延時關(guān)閉電腦和取消關(guān)閉的功能,可以有多種方法,下面給大家分享C#延時關(guān)閉電腦、取消關(guān)閉電腦操作方法,感興趣的朋友一起看看吧2024-06-06WPF實現(xiàn)XAML轉(zhuǎn)圖片的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用WPF實現(xiàn)XAML轉(zhuǎn)圖片,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-11-11