最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。
先上传效果图:
方法一:
增加命名空间:using system.drawing.drawing2d;
添加方法如下:当然各角的点可根据需要确定.
(float)p_2);
sender.region = new region(opath);
}
在窗体的paint和resize事件中增加:type(this,20,0.1);
参数20和0.1也可以根据自己的需要调整到最佳效
方法二:
system.drawing.drawing2d.graphicspath formpath;
formpath = new system.drawing.drawing2d.graphicspath();
rectangle rect = new rectangle(0, 22, this.width, this.height - 22);//this.left-10,this.top-10,this.width-10,this.height-10);
formpath = getroundedrectpath(rect, 30);
this.region = new region(formpath);
}
private graphicspath getroundedrectpath(rectangle rect, int radius)
{
int diameter = radius;
rectangle arcrect = new rectangle(rect.location, new size(diameter, diameter));
graphicspath path = new graphicspath();
// 左上角
path.addarc(arcrect, 180, 90);
// 右上角
arcrect.x = rect.right - diameter;
path.addarc(arcrect, 270, 90);
// 右下角
arcrect.y = rect.bottom - diameter;
path.addarc(arcrect, 0, 90);
// 左下角
arcrect.x = rect.left;
path.addarc(arcrect, 90, 90);
path.closefigure();
return path;
}
方法三:通过window系统api行数,修改控件和窗体为椭圆形状。代码如下所示:
intptr dc;
intptr region;
dc = getdc(this.handle);
beginpath(dc);
setbkmode(dc, transparent);
ellipse(dc, 0, 0, this.width - 3, this.height - 2);
endpath(dc);
region = pathtoregion(dc);
setwindowrgn(this.handle, region, false);
}
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论