亲宝软件园·资讯

展开

C#圆角按钮 C#实现自定义圆角按钮的方法

胖达没有海 人气:0
想了解C#实现自定义圆角按钮的方法的相关内容吗,胖达没有海在本文为您仔细讲解C#圆角按钮的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#圆角按钮,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;
        }

至此一个圆角按钮就完成了^_^。效果如下:

加载全部内容

相关教程
猜你喜欢
用户评论