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
- C#绘画圆角矩形的两种方式
- mapx5.0+C# 绘画矩形、圆、椭圆
- ios-day15-01(Quartz 2D之画圆的两种方式、画矩形的4种方式)
- C#调用WPS的两种方式
- c#里压缩文件的两种方式
- C#替换桌面的两种方式
- C#调用WPS的两种方式
- C#文件操作的两种方式
- C#两种方式重载
- vb.net中获取圆角矩形的两种方法
- C#绘制圆角矩形
- html5笔记3 — canvas绘画矩形、圆
- 使用 C# 进行 label 打印 的两种方式
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- 使用c#连接到IBM db2的两种方式
- C#中定义常量的两种方式
- apache&tomcat access访问日志
- 健康检查
- SQL--考试错题分析
- 算法竞赛入门经典 习题2-9 分数化小数(decimal)
- Hibernate— 投影查询
- C#绘画圆角矩形的两种方式
- 一读一写情况下,无锁环形队列如何实现?
- 【iOS开发】OC变量和基本数据类型-精度
- std::list的pop_front()及front()
- text、html、val、attr(Jquery取值方法)
- Java匿名内部类访问外部类局部变量,为何需被标志为final?
- Android笔记:Dialog详解
- 紫书 例题10-18 概率 UVa 11346
- 日期间的天数