欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-15 18:50:17
winform中自带的button没有圆角属性,所以我们继承button类,重写onpaint事件来绘制圆角按钮。1.绘制圆角按钮框需要用到系统自带的绘制方法:首先引入gdi32.dll中的creat...

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#实现自定义圆角按钮的方法

到此这篇关于c#实现自定义圆角按钮的文章就介绍到这了,更多相关c#圆角按钮内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: C# 圆角 按钮