发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
留言交流
最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。
先上传效果图:
方法一:
增加命名空间: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 }
来自: 昵称10504424 > 《工作》
0条评论
发表
请遵守用户 评论公约
抓取窗体或控件图片窗体 - 『Delphi』中国 --Delphi源码,Delphi控件,...
抓取窗体或控件图片窗体。type TForm2 = class(TForm) procedure FormCreate(Sender: TObject);procedure FormActivate(Sender: TObject);procedure FormDestroy(Sender: TObject);procedure FormP...
C# 爬虫:疫情实时信息图
} public void Form1_Resize(object sender, EventArgs e) { float[] scale = (float[])this._form.需求:当窗体尺寸动态改变时,窗体中...
跟我做WinForm开发(1)
WinForm开发中,我们都知道窗体和控件的作用,实际上,以上的UI实现也是通过自定义窗体和用户控件实现,那该如何做,才能让窗体变成我们...
用C#做一个悬浮窗口[含三种移动无标题窗体的办法]
第一步:建立一个窗体,设置其属性:再添加三个鼠标事件: private void Form2_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) ...
c#winform自定义窗体(含源码)
c#winform自定义窗体(含源码)先将原生窗体设置成无边框,FormBoderStyle设置成None然后添加一个panpel控件设置成窗体的的标题栏,并将pa...
Win32 API对文本框发送消息(多个文本Edit,动态 控件 ID) - Soma -...
最近在群里有人说用Win32 Api不能对文本框设置内容(是别人写的一个程序,设置它的文本框的值).但是搞过win32的人都会说.这个应该不难啊,...
VB.NET 2005编写定时关机程序
VB.NET 2005编写定时关机程序VB.NET 2005编写定时关机程序 最近在网上搜索了一些关于VB.net实现关机、重启、注销的文章,发现大多...
VB窗体动态创建控件及控件事件
VB窗体动态创建较少,今天,试着弄一个,专为解答之用。
[原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
private void OnRunClick(object sender, EventArgs e) { // 查找主界面句柄 IntPtr mainHandle = WindowsApi.FindWindow(null, "...
微信扫码,在手机上查看选中内容