欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-24 12:36:58
最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能。其中方法三既适应于控件,也适应于窗体。 先上传效果图:...

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

先上传效果图:

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);
        }