c#winform如何画圆角控件,看完这篇文章你就会了

在我们日常工作开发中,可能会经常为了让控件更加好看,对控件进行圆角处理,有时候还有可能对四个角设置不同的圆角,那么我们改怎么样做呢?楼主封装了一个类,绝对的项目实战使用,现在分享给你们

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace cloudproject.untils
{
    public class UoperaGraphics
    {

        /// <summary>
        /// 圆角矩形边框
        /// </summary>
        /// <param name="rectangle">原始矩形</param>
        /// <param name="g">Graphics 对象</param>
        /// <param name="radius">圆角半径</param>
        /// <param name="backColor">颜色</param>
        public static void FrameRoundRectangle(Rectangle rectangle, Graphics g, int radius, Color backColor)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            if (radius == 0)
            {
                g.DrawRectangle(new Pen(backColor), rectangle);
            }
            else
            {
                g.DrawPath(new Pen(backColor), DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, radius));
            }
        }
        /// <summary>
        /// 圆角边框【可设置四个角不同的圆角】
        /// </summary>
        /// <param name="rectangle">控件的范围</param>
        /// <param name="g">g</param>
        /// <param name="top_left_radius">左上角的圆角</param>
        /// <param name="top_right_radius">右上角的圆角</param>
        /// <param name="bottom_left_radius">底部左边的圆角</param>
        /// <param name="bottom_right_radius">底部右边的圆角</param>
        /// <param name="backColor">要描述的边框的背景的颜色</param>
        public static void FrameRoundRectangle_Four(Rectangle rectangle, Graphics g, int top_left_radius, int top_right_radius, int bottom_left_radius, int bottom_right_radius, Color backColor)
        {

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            if (top_left_radius == 0)
            {
                g.DrawRectangle(new Pen(backColor), rectangle);
            }
            else
            {
                g.DrawPath(new Pen(backColor), DrawRoundRect_four(rectangle.X, rectangle.Y, rectangle.Width - 1, rectangle.Height - 1, top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius));
            }
        }
        /// <summary>
        /// 圆角矩形
        /// </summary>
        /// <param name="rectangle">原始矩形</param>
        /// <param name="g">Graphics 对象</param>
        /// <param name="radius">圆角半径</param>
        /// <param name="backColor">颜色</param>
        public static void FillRoundRectangle(Rectangle rectangle, Graphics g, int radius, Color backColor)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            if (radius == 0)
            {
                g.FillRectangle(new SolidBrush(backColor), rectangle);
            }
            else
            {
                g.FillPath(new SolidBrush(backColor), DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, radius));
            }
        }

        /// <summary>
        /// 圆角矩形粗边框
        /// </summary>
        public static void FrameBoldRoundRectangle(Rectangle rectangle, Graphics g, int radius, Color backColor, int linewide)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            if (radius == 0)
            {
                g.DrawRectangle(new Pen(backColor, linewide), rectangle);
            }
            else
            {
                g.DrawPath(new Pen(backColor, linewide), DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, radius));
            }
        }

        public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(x + width - radius, y, radius, radius, 270, 90);
            gp.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);
            gp.AddArc(x, y + height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }
        public static void FillRoundRectangle_four(Rectangle rectangle, Graphics g, int radius_topleft, int radius_topright, int radius_bottom_left, int radius_bottomright, Color backColor)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            if (radius_topleft == 0)
            {
                g.FillRectangle(new SolidBrush(backColor), rectangle);
            }
            else
            {
                g.FillPath(new SolidBrush(backColor), DrawRoundRect_four(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, radius_topleft, radius_topright, radius_bottom_left, radius_bottomright));
            }
        }
        public static GraphicsPath DrawRoundRect_four(int x, int y, int width, int height, int radius_topleft, int radius_topright, int radius_bottomleft, int radius_bottomright)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius_topleft, radius_topleft, 180, 90);
            gp.AddArc(x + width - radius_topright, y, radius_topright, radius_topright, 270, 90);
            gp.AddArc(x + width - radius_bottomleft, y + height - radius_bottomleft, radius_bottomleft, radius_bottomleft, 0, 90);
            gp.AddArc(x, y + height - radius_bottomright, radius_bottomright, radius_bottomright, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }
    }
}

如果有需要可以直接粘贴复制使用,我自己的项目在用哦!

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖大师兄.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值