C# 开发圆角控件(窗体)
最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。
先上传效果图:
方法一:
增加命名空间:using System.Drawing.Drawing2D;
添加方法如下:当然各角的点可根据需要确定.
1 private void Type(Control sender, int p_1, double p_2) 2 { 3 GraphicsPath oPath = new GraphicsPath(); 4 oPath.AddClosedCurve( 5 new Point[] { 6 new Point(0, sender.Height / p_1), 7 new Point(sender.Width / p_1, 0), 8 new Point(sender.Width - sender.Width / p_1, 0), 9 new Point(sender.Width, sender.Height / p_1), 10 new Point(sender.Width, sender.Height - sender.Height / p_1), 11 new Point(sender.Width - sender.Width / p_1, sender.Height), 12 new Point(sender.Width / p_1, sender.Height), 13 new Point(0, sender.Height - sender.Height / p_1) }, 14 15 (float)p_2); 16 17 sender.Region = new Region(oPath); 18 }
在窗体的paint和resize事件中增加:Type(this,20,0.1);
参数20和0.1也可以根据自己的需要调整到最佳效
方法二:
1 public void SetWindowRegion() 2 { 3 4 System.Drawing.Drawing2D.GraphicsPath FormPath; 5 6 FormPath = new System.Drawing.Drawing2D.GraphicsPath(); 7 8 Rectangle rect = new Rectangle(0, 22, this.Width, this.Height - 22);//this.Left-10,this.Top-10,this.Width-10,this.Height-10); 9 10 FormPath = GetRoundedRectPath(rect, 30); 11 12 this.Region = new Region(FormPath); 13 14 } 15 16 private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) 17 { 18 19 int diameter = radius; 20 21 Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter)); 22 23 GraphicsPath path = new GraphicsPath(); 24 25 // 左上角 26 27 path.AddArc(arcRect, 180, 90); 28 29 // 右上角 30 31 arcRect.X = rect.Right - diameter; 32 33 path.AddArc(arcRect, 270, 90); 34 35 // 右下角 36 37 arcRect.Y = rect.Bottom - diameter; 38 39 path.AddArc(arcRect, 0, 90); 40 41 // 左下角 42 43 arcRect.X = rect.Left; 44 45 path.AddArc(arcRect, 90, 90); 46 47 path.CloseFigure(); 48 49 return path; 50 51 }
在窗体的resize事件中增加:SetWindowRegion();
方法三:通过Window系统API行数,修改控件和窗体为椭圆形状。代码如下所示:
1 [System.Runtime.InteropServices.DllImport("gdi32")] 2 private static extern IntPtr BeginPath(IntPtr hdc); 3 [System.Runtime.InteropServices.DllImport("gdi32")] 4 private static extern int SetBkMode(IntPtr hdc, int nBkMode); 5 const int TRANSPARENT = 1; 6 [System.Runtime.InteropServices.DllImport("gdi32")] 7 private static extern IntPtr EndPath(IntPtr hdc); 8 [System.Runtime.InteropServices.DllImport("gdi32")] 9 private static extern IntPtr PathToRegion(IntPtr hdc); 10 [System.Runtime.InteropServices.DllImport("gdi32")] 11 private static extern int Ellipse(IntPtr hdc, int x1, int y1, int x2, int y2); 12 [System.Runtime.InteropServices.DllImport("user32")] 13 private static extern IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, bool bRedraw); 14 [System.Runtime.InteropServices.DllImport("user32")] 15 private static extern IntPtr GetDC(IntPtr hwnd);
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 5 IntPtr dc; 6 IntPtr region; 7 8 dc = GetDC(this.Handle); 9 BeginPath(dc); 10 SetBkMode(dc, TRANSPARENT); 11 Ellipse(dc, 0, 0, this.Width - 3, this.Height - 2); 12 EndPath(dc); 13 region = PathToRegion(dc); 14 SetWindowRgn(this.Handle, region, false); 15 }
作者:Peter Luo
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构