C# 开发圆角控件(窗体)
最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。
先上传效果图:
方法一:
增加命名空间:using System.Drawing.Drawing2D;
添加方法如下:当然各角的点可根据需要确定.
private void Type(Control sender, int p_1, double p_2)
{
GraphicsPath oPath = new GraphicsPath();
oPath.AddClosedCurve(
new Point[] {
new Point(, sender.Height / p_1),
new Point(sender.Width / p_1, ),
new Point(sender.Width - sender.Width / p_1, ),
new Point(sender.Width, sender.Height / p_1),
new Point(sender.Width, sender.Height - sender.Height / p_1),
new Point(sender.Width - sender.Width / p_1, sender.Height),
new Point(sender.Width / p_1, sender.Height),
new Point(, sender.Height - sender.Height / p_1) }, (float)p_2); sender.Region = new Region(oPath);
}
在窗体的paint和resize事件中增加:Type(this,20,0.1);
参数20和0.1也可以根据自己的需要调整到最佳效
方法二:
public void SetWindowRegion()
{ System.Drawing.Drawing2D.GraphicsPath FormPath; FormPath = new System.Drawing.Drawing2D.GraphicsPath(); Rectangle rect = new Rectangle(, , this.Width, this.Height - );//this.Left-10,this.Top-10,this.Width-10,this.Height-10); FormPath = GetRoundedRectPath(rect, ); 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, , ); // 右上角 arcRect.X = rect.Right - diameter; path.AddArc(arcRect, , ); // 右下角 arcRect.Y = rect.Bottom - diameter; path.AddArc(arcRect, , ); // 左下角 arcRect.X = rect.Left; path.AddArc(arcRect, , ); path.CloseFigure(); return path; }
在窗体的resize事件中增加:SetWindowRegion();
方法三:通过Window系统API行数,修改控件和窗体为椭圆形状。代码如下所示:
[System.Runtime.InteropServices.DllImport("gdi32")]
private static extern IntPtr BeginPath(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32")]
private static extern int SetBkMode(IntPtr hdc, int nBkMode);
const int TRANSPARENT = ;
[System.Runtime.InteropServices.DllImport("gdi32")]
private static extern IntPtr EndPath(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32")]
private static extern IntPtr PathToRegion(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32")]
private static extern int Ellipse(IntPtr hdc, int x1, int y1, int x2, int y2);
[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, bool bRedraw);
[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr GetDC(IntPtr hwnd);
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); IntPtr dc;
IntPtr region; dc = GetDC(this.Handle);
BeginPath(dc);
SetBkMode(dc, TRANSPARENT);
Ellipse(dc, , , this.Width - , this.Height - );
EndPath(dc);
region = PathToRegion(dc);
SetWindowRgn(this.Handle, region, false);
}
C# 开发圆角控件(窗体)的更多相关文章
- C# 开发圆角控件的具体实现
http://www.jb51.net/article/47433.htm 代码来源
- 一些基于jQuery开发的控件
基于jQuery开发,非常简单的水平方向折叠控件.主页:http://letmehaveblog.blogspot.com/2007/10/haccordion-simple-horizontal-a ...
- C#开发ActiveX控件
昨天写了篇博客<Winform 程序嵌入WPF程序 并发送消息>,没有说明为什么要嵌入WPF程序,那么今天就来唠叨唠叨其中的一个使用场景,开发ActiveX控件 首先,新建一个类库工程Hu ...
- Delphi 开发ActiveX控件(非ActiveForm)
Delphi 开发ActiveX控件(非ActiveForm) Q:为什么不采用ActiveForm工程?通过它可以快速开发带窗体控件,创建过程也非常简单(都不用考虑安全接口问题),很省事! A:如果 ...
- [转]使用C#开发ActiveX控件全攻略
前言: 这段时间因为工作的需要,研究了一下ActiveX控件.总结如下: 先说说ActiveX的基本概念. 根据微软权威的软件开发指南MSDN(Microsoft Developer Network) ...
- 用C#开发ActiveX控件,并使用web调用
入职差不多两个月了,由学生慢慢向职场人做转变,也慢慢的积累知识,不断的更新自己.最近的一个项目里边,涉及到的一些问题,因为SDK提供的只是winform才能使用了,但是有需求咱们必须得完成啊,所以涉及 ...
- ATL开发 ActiveX控件的 inf文件模板
ATL开发 ActiveX控件的 inf文件模板
- 使用C#开发ActiveX控件(新)
前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动态处理能力.通常ActiveX控件都是 ...
- IOS学习资源收集--开发UI控件相关
收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...
随机推荐
- Python 安装出错:Setup script exited with error: command 'gcc' failed with exit status 1
退出当前环境: logout (再重新登录进去) yum install python-devel -yyum install libevent-devel -y 把环境更新下yum instal ...
- (TOJ 4413)IP address
描述 To give you an IP address, it may be dotted decimal IP address, it may be 32-bit binary IP addres ...
- Docker Compose 版本过高(Docker版本不匹配),降低docker-compose版本
通过docker-compose启动容器,报错: ERROR: The Docker Engine version is less than the minimum required by Compo ...
- myeclipse2016破解过程
1.安装myeclipse2016 CI 7就不介绍了..只需要注意最后安装完成后取消对号.不要立即运行myeclipse2016. 2.下载破解版工具. 到以下网址下载破解工具:http://dow ...
- mysql if判断
select if(SUBSTR('06622200556',1,2)='06',0,1) from t_member_product_adb limit 2 输出结果为:0,0
- django的queryset和objects对象
1. queryset是查询集,就是传到服务器上的url里面的内容.Django会对查询返回的结果集QerySet进行缓存,这里是为了提高查询效率. 也就是说,在你创建一个QuerySet对象的时候, ...
- 微信小程序APP(商超营销类)经验总结
项目介绍 这是一款主打门店营销的小程序.包括首页.门店.营销.个人设置.登录.数据统计展示.营销设置等. 本来要独立完成整个项目,包括前后端一套的,有些意外因素,项目临时收尾(说明:只完成了前端的部分 ...
- Zabbix监控虚拟主机告警Lack of free swap space on Zabbix server解决办法
Zabbix监控虚拟机的时候有时候会报一下告警 是因为Zabbix监控没有考虑虚拟主机的交换空间情况 解决办法修改配置
- ubuntu16.04下安装Eigen
请输入以下命令进行安装: sudo apt-get install libeigen3-dev 一个库由头文件和库文件组成.Eigen头文件的默认位置在 “usr/include/eigen3” 中. ...
- Sublime2或3配置R、Scala、Python交互式环境
1.Sublime3的下载地址:http://www.sublimetext.com/3 2.刚刚安装的软件是没有PackageControl的,需要在新安装使用 (1) 以前没有安装过Packa ...