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

来源:互联网 发布:淘宝开店取名宝典 编辑:程序博客网 时间:2024/05/18 00:53
最近在用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 0
原创粉丝点击