最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。

先上传效果图:

方法一:

增加命名空间: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# 开发圆角控件(窗体)的更多相关文章

  1. C# 开发圆角控件的具体实现

    http://www.jb51.net/article/47433.htm 代码来源

  2. 一些基于jQuery开发的控件

    基于jQuery开发,非常简单的水平方向折叠控件.主页:http://letmehaveblog.blogspot.com/2007/10/haccordion-simple-horizontal-a ...

  3. C#开发ActiveX控件

    昨天写了篇博客<Winform 程序嵌入WPF程序 并发送消息>,没有说明为什么要嵌入WPF程序,那么今天就来唠叨唠叨其中的一个使用场景,开发ActiveX控件 首先,新建一个类库工程Hu ...

  4. Delphi 开发ActiveX控件(非ActiveForm)

    Delphi 开发ActiveX控件(非ActiveForm) Q:为什么不采用ActiveForm工程?通过它可以快速开发带窗体控件,创建过程也非常简单(都不用考虑安全接口问题),很省事! A:如果 ...

  5. [转]使用C#开发ActiveX控件全攻略

    前言: 这段时间因为工作的需要,研究了一下ActiveX控件.总结如下: 先说说ActiveX的基本概念. 根据微软权威的软件开发指南MSDN(Microsoft Developer Network) ...

  6. 用C#开发ActiveX控件,并使用web调用

    入职差不多两个月了,由学生慢慢向职场人做转变,也慢慢的积累知识,不断的更新自己.最近的一个项目里边,涉及到的一些问题,因为SDK提供的只是winform才能使用了,但是有需求咱们必须得完成啊,所以涉及 ...

  7. ATL开发 ActiveX控件的 inf文件模板

    ATL开发 ActiveX控件的 inf文件模板    

  8. 使用C#开发ActiveX控件(新)

    前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动态处理能力.通常ActiveX控件都是 ...

  9. IOS学习资源收集--开发UI控件相关

    收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...

随机推荐

  1. Linux 如何开启SFTP

    一.SFTP讲解 SFTP 是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法. SFTP 与 FTP有着几乎一样的语法和功能. ...

  2. Visual Studio 2013安装Update 3启动crash的解决方法

    Visual Studio 2013安装完Update 3后启动立刻crash,异常信息为: System.InvalidOperationException was unhandled Messag ...

  3. 【CF884F】Anti-Palindromize 费用流

    [CF884F]Anti-Palindromize 题意:定义一个串是反回文的,当且仅当对于1<=i<=len,$a_i!=a_{len-i+1}$. 现在给出一个长度为n的串S(n是偶数 ...

  4. ios atomic nonatomic区别

    atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作.         atomic 设置成员变量的@property属性时,默认为atomic,提供多线程安全 ...

  5. MVC @RenderBody、@RenderSection、@RenderPage、@Html.RenderPartial、@Html.RenderAction

    1.@RenderBody() 作用和母版页中的服务器控件类似,当创建基于此布局页面的视图时,视图的内容会和布局页面合并,而新创建视图的内容会通过布局页面的@RenderBody()方法呈现在标签之间 ...

  6. SOA架构商城二 框架搭建

    1.创建父工程 创建Maven工程pingyougou-parent,选择packaging类型为pom ,在pom.xml文件中添加锁定版本信息dependencyManagement与plugin ...

  7. mysql union查询

    1.mysql总是通过创建并填充临时表来执行union查询; 2.除非要服务器消除重复的行,否则一定要用union all.如果没有all关键字,mysql会在临时表加个distinct选项,会导致临 ...

  8. 从本机IIS中管理 远程服务器 IIS

    有时候,一般情况下,我们对服务器上 IIS 上的管理局限于 使用远程桌面:现在介绍一种,通过  本机 管理管理远程IIS 的方法! 1. 服务器端设置: 服务器管理器 ==>增加角色和功能向导= ...

  9. oracle listagg函数、lag函数、lead函数 实例

    Oracle大师Thomas Kyte在他的经典著作中,反复强调过一个实现需求方案选取顺序: “如果你可以使用一句SQL解决的需求,就使用一句SQL:如果不可以,就考虑PL/SQL是否可以:如果PL/ ...

  10. MySQL异步复制

    准备:主备库版本一致,正常安装软件. 1.主库上设置一个复制使用的账户: mysql> grant replication slave on *.* to 'rep1'@'192.168.100 ...