[原创]c#自定义绘制矩形的正确姿势

public static void drawRoundRect(Graphics graphics, string solidColor, string borderColor, int x, int y, int width, int height, float radius, float bordersize)
        {
            GraphicsPath gpPath = new GraphicsPath();
            gpPath.AddArc(x, y, radius, radius, 180, 90);//从180度 开始顺时针画90度 ,左上角
            gpPath.AddArc(width - radius, y, radius, radius, 270, 90);//右上角
            gpPath.AddArc(width - radius, height - radius, radius, radius, 0, 90);//右下角
            gpPath.AddArc(x, height - radius, radius, radius, 90, 90);//左下角 x顺时针
            gpPath.CloseAllFigures();
            bool hasBorder = bordersize > 0 && borderColor != null && RegHelper.isColorValue(borderColor);
            if (hasBorder)//绘制边框
            {
                using (Pen shadowPen = new Pen(AppUtil.parseColor(borderColor)))//清理资源的using
                {
                    shadowPen.Alignment = PenAlignment.Inset;
                    shadowPen.Width = bordersize * 2;
                    shadowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//shixian
                    graphics.DrawPath(shadowPen, gpPath);

                }

            }
            //绘制填充颜色。
            if (!AppUtil.textIsEmpty(solidColor) && RegHelper.isColorValue(solidColor))
            {
                if (hasBorder)
                {
                    width = (int)(width - bordersize);
                    height = (int)(height - bordersize);
                    x = (int)(x + bordersize);
                    y = (int)(y + bordersize);

                }
                GraphicsPath solidPath = new GraphicsPath();
                if (bordersize > 2&&radius>5&&width>50)
                {
                    radius= radius * 0.7f;
                }

                solidPath.AddArc(x, y, radius, radius, 180, 90);//从180度 开始顺时针画90度 ,左上角
                solidPath.AddArc(width - radius, y, radius, radius, 270, 90);//右上角
                solidPath.AddArc(width - radius, height - radius, radius, radius, 0, 90);//右下角
                solidPath.AddArc(x, height - radius, radius, radius, 90, 90);//左下角 x顺时针
                solidPath.CloseAllFigures();

                SolidBrush brush = new SolidBrush(AppUtil.parseColor(solidColor));
                graphics.FillPath(brush, solidPath);

            }
        }
image.png

如果画笔的方式不是insert,那么会出现一些奇怪的,情况,特别是圆角比较大的时候。https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics?view=netframework-4.7.2

http://st233.com/blog.php?group=1

http://www.cnblogs.com/stg609/archive/2008/03/16/1108333.html

http://www.cnblogs.com/isaboy/articles/ebook_custom_control_winform.html

目前找不到方法实现的是实现 自定义按钮 阴影并且可设置阴影x,y大小,颜色,以及模糊值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值