您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C#如何實現自定義圓角按鈕的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
Winform中自帶的button沒有圓角屬性,所以我們繼承Button類,重寫OnPaint事件來繪制圓角按鈕。
1.繪制圓角按鈕框需要用到系統自帶的繪制方法:首先引入Gdi32.dll中的CreateRoundRectRgn方法。經過驗證此方法在大量控件情況下使用,會導致GDI+繪圖問題!現在改用自己繪制的圓角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; }
至此一個圓角按鈕就完成了^_^。效果如下:
感謝各位的閱讀!關于“C#如何實現自定義圓角按鈕”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。