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控件 正文 ...
随机推荐
- OGG遇到相关问题汇总
OGG初始化加载数据时遇到的问题 1.target端拒绝source端访问 2016-12-13 14:31:03 INFO OGG-00963 Oracle GoldenGate Manager f ...
- nginx 日志文件
默认日志格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status ...
- virgo-tomcat-server最大并发连接数的修改
首先,我们如果需要修改tomcat 7的最大连接数,我们可以去tomcat官方网站,查看Documentation 进入tomcat的官方网站http://tomcat.apache.org我们点击左 ...
- [SQL] SQL 日常检查脚本
--sqlserver 日常检查脚本 print '----------------------------' print ' 0.sqlserver all information ' print ...
- java使用AES256解密
网上关于java用AES加密解密的文章有很多,我这里只用到解密(加密是服务器那边做^_^),所以更简洁一些: public class AES256Utils { private static fin ...
- Linux wc
命令参数: -c 统计字节数. -l 统计行数. -m 统计字符数.这个标志不能与 -c 标志一起使用. -w 统计字数.一个字被定义为由空白.跳格或换行字符分隔的字符串. -L 打印最长行的长度. ...
- 9.7 Django 书单列表页面
昨天的迭代版本,增加了编辑出版社,编辑列表,增添了返回页面! 具体的看 github : https://github.com/TrueNewBee/pythonDemo 看一下效果图: 整体来说还是 ...
- Google Drive 里的文件下载的方法
Google Drive 里并不提供创建直接下载链接的选项,但是可以通过小小的更改链接形式就能把分享的内容保存到本地.例如,一份通过 Google Drive 分享的文件链接形式为: https:// ...
- php代码不支持多维数组,注释和没有缓存功能。
php代码:simplet.class.php<?phpclass SimpleT {private $t_vars;private $templates_dir;private $templa ...
- poj1850Code
Code Transmitting and memorizing information is a task that requires different coding systems for th ...