C# 开发圆角控件(窗体)的具体实现

 更新时间:2014年02月28日 15:45:01   作者:  
这篇文章主要介绍了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(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#使用Socket快速判断数据库连接是否正常的方法

    C#使用Socket快速判断数据库连接是否正常的方法

    这篇文章主要介绍了C#使用Socket快速判断数据库连接是否正常的方法,涉及C#中socket操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#装饰者模式实例分析

    C#装饰者模式实例分析

    这篇文章主要介绍了C#装饰者模式,是C#中比较常见的设计模式,非常具有实用价值,需要的朋友可以参考下
    2014-10-10
  • Unity实现颜色渐变滑动条

    Unity实现颜色渐变滑动条

    这篇文章主要为大家详细介绍了Unity实现颜色渐变滑动条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 在c#中把字符串转为变量名并获取变量值的小例子

    在c#中把字符串转为变量名并获取变量值的小例子

    这篇文章介绍了在c#中把字符串转为变量名并获取变量值的小例子,有需要的朋友可以参考一下
    2013-09-09
  • 浅谈C#六大设计原则

    浅谈C#六大设计原则

    这篇文章主要介绍了C#六大设计原则的相关内容,文中代码非常细致,供大家参考和学习,感兴趣的朋友可以了解下
    2020-06-06
  • C#生成二维码的方法

    C#生成二维码的方法

    这篇文章主要介绍了C#生成二维码的方法,包括动态链接库dll文件的调用、代码的实现与使用示例,非常实用,需要的朋友可以参考下
    2014-11-11
  • C#实现数字华容道游戏

    C#实现数字华容道游戏

    这篇文章主要为大家详细介绍了C#实现数字华容道游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • C# paddlerocrsharp识别身份证号的实现示例

    C# paddlerocrsharp识别身份证号的实现示例

    paddlerocrsharp可以进行图片识别,本文主要介绍了C# paddlerocrsharp识别身份证号的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • c# for循环中创建线程执行问题

    c# for循环中创建线程执行问题

    这篇文章主要介绍了有关c# for循环中创建线程执行问题,下面文章将将以举例的方式展开for循环中创建线程执行问题的内容,需要的朋友可以参考一下,希望对你有所帮助
    2021-11-11
  • C#使用semaphore来管理异步下载请求的方法

    C#使用semaphore来管理异步下载请求的方法

    这篇文章主要介绍了C#使用semaphore来管理异步下载请求的方法,涉及C#使用semaphore实现多线程管理的技巧,需要的朋友可以参考下
    2015-06-06

最新评论