C# 开发圆角控件(窗体)的具体实现
2014-11-02 23:06
453 查看
最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。先上传效果图:
方法一:增加命名空间: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(0, sender.Height / p_1),
new Point(sender.Width / p_1, 0),
new Point(sender.Width - sender.Width / p_1, 0),
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(0, 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(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; }
在窗体的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 = 1;
[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, 0, 0, this.Width - 3, this.Height - 2);
EndPath(dc);
region = PathToRegion(dc);
SetWindowRgn(this.Handle, region, false);
}
方法一:增加命名空间: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(0, sender.Height / p_1),
new Point(sender.Width / p_1, 0),
new Point(sender.Width - sender.Width / p_1, 0),
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(0, 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(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; }
在窗体的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 = 1;
[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, 0, 0, this.Width - 3, this.Height - 2);
EndPath(dc);
region = PathToRegion(dc);
SetWindowRgn(this.Handle, region, false);
}
相关文章推荐
- C# 开发圆角控件(窗体)的具体实现
- C#基础系列:开发自己的窗体设计器(实现控件的拖动)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变(转)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C#第十周任务之最后一项之创建一个如下的窗体,并在窗体上放置一个菜单、一个工具栏控件。菜单内容如第二个图所示。工具栏上有两个按钮,分别对应“打开文本文件”、“保存文本文件”。菜单和工具栏具体功能实现可
- C#基础系列:开发自己的窗体设计器(实现控件的选择)
- C# 开发圆角控件(窗体)
- C#基础系列:开发自己的窗体设计器(实现控件的拖动)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C#基础系列:开发自己的窗体设计器(实现控件的拖动)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C#基础系列:开发自己的窗体设计器(实现控件的选择)
- C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变
- C# winform 实现窗体或者控件的拖动
- 【转】C#中代码实现控件随窗体的自由变换
- C#基础系列:开发自己的窗体设计器(在容器上拖动鼠标增加控件)
- C#实现控件与窗体大小自适应
- C#仿QQ皮肤----基窗体FormBase与基用户控件FormBase1的实现