C#绘画圆角矩形的两种方式

最近在用C#进行编程,重写CheckBox,需要绘画圆角矩形,在网上查找了许多资料,用C#FillPath的方式绘画总感觉太麻烦,需要算坐标,不如直接调用C++的方法绘画圆角矩形。
声明:本代码借鉴网上代码,在此仅做整理出来供大家参考使用
        [DllImport("user32.dll")]
        public static extern int SetWindowRgn(IntPtr hWnd,
            IntPtr hRgn, bool bRedraw);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateRoundRectRgn(int x1, int y1,
            int x2, int y2, int cx, int cy);
        [DllImport("gdi32.dll")]
        public static extern bool RoundRect(IntPtr hDC, int x1,
            int y1, int x2, int y2,
            int x3, int y3);
        [DllImport("user32")]
        public static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreatePen(
            int fnPenStyle,    // pen style
            int nWidth,        // pen width
            int crColor        // pen color
            );
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateSolidBrush(int crColor);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport("gdi32.dll")]
        public static extern IntPtr GetStockObject(int fnObject);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
            int nWidth, int nHeight);
        [DllImport("gdi32.dll")]
        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
            int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc,
            UInt32 dwRop);
        /// <summary>
        /// 绘画矩形(利用C++API绘画圆角矩形)
        /// </summary>
        public static bool DrawRoundRectC(Graphics e,
            Pen pen, SolidBrush Br,
            int X1, int Y1, int X2,
            int Y2, int X3, int Y3)
        {
            IntPtr hPen, oldpen;
            IntPtr hBr, oldBr;
            IntPtr hMemDc;
            IntPtr hBitmap, hOldBitmap;

            hPen = CreatePen((int)pen.DashStyle,
                (int)pen.Width,
                ColorTranslator.ToWin32(pen.Color)
                );
            //hPen = GetStockObject(8);//空画笔
            hBr = CreateSolidBrush(ColorTranslator.ToWin32(Br.Color));
            //hbrush = GetStockObject(5);//空画刷
            IntPtr hDC = e.GetHdc();
            //创建兼容DC
            ///////////////////////////////////////////////////////////////
            int iWidth = X2 - X1;
            int iHeight = Y2 - Y1;
            hMemDc = CreateCompatibleDC(hDC);
            hBitmap = CreateCompatibleBitmap(hDC, iWidth, iHeight);
            hOldBitmap = SelectObject(hMemDc, hBitmap);
            BitBlt(hMemDc, 0, 0, iWidth, iHeight, hDC, 0, 0, (UInt32)0xcc0020);

            /////////////////////////////////////////////////////////////
            //选入画刷和画笔
            oldBr = SelectObject(hMemDc, hBr);
            oldpen = SelectObject(hMemDc, hPen);
            bool bRet = RoundRect(hMemDc, X1, Y1, X2, Y2, X3, Y3);

            BitBlt(hDC, 0, 0, iWidth, iHeight, hMemDc, 0, 0, (UInt32)0xcc0020);

            //资源释放
            SelectObject(hMemDc, oldpen);
            SelectObject(hMemDc, oldBr);
            SelectObject(hMemDc, hOldBitmap);

            DeleteObject(hBitmap);
            DeleteObject(hMemDc);
            DeleteObject(hBr);
            DeleteObject(hPen);
            e.ReleaseHdc(hDC);

            return bRet;

        }

还有一种方法是网上利用C#的另一种方法

        /// <summary>
        /// 填充圆角矩形
        /// </summary>
        private void FillRound(Rectangle rectangle,
            Graphics g, Brush br, int _radius)
        {
            g.FillPath(br, DrawRoundRect(rectangle.X,
                rectangle.Y, rectangle.Width - 2,
                rectangle.Height - 1, _radius));
        }
        /// <summary>
        /// 生成圆角矩形路径
        /// </summary>
        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(width - radius, y, radius, radius, 270, 90);
            gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            gp.AddArc(x, height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }

在编写过程中,若担心pen,brush资源释放问题,可以使用

using(Pen p = new Pen(Color.Red))
{
}

这种方式资源会自动释放。

本人编写了重绘了CheckBox,使其为一种可滑动的安妞形状,类似手机的滑动按钮方式

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值