国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C# 開(kāi)發(fā)圓角控件(窗體)的具體實(shí)現(xiàn)

2020-01-24 02:46:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近在做卡片視圖的程序,要求將控件做成帶有圓角的效果,下面是我在網(wǎng)上查找的資料,經(jīng)過(guò)測(cè)試,確定可以實(shí)現(xiàn)功能。其中方法三既適應(yīng)于控件,也適應(yīng)于窗體。

先上傳效果圖:

方法一:

增加命名空間:using System.Drawing.Drawing2D; 
添加方法如下:當(dāng)然各角的點(diǎn)可根據(jù)需要確定.

復(fù)制代碼 代碼如下:

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); 
參數(shù)20和0.1也可以根據(jù)自己的需要調(diào)整到最佳效

方法二:

復(fù)制代碼 代碼如下:

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

方法三:通過(guò)Window系統(tǒng)API行數(shù),修改控件和窗體為橢圓形狀。代碼如下所示:

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 庆城县| 普陀区| 乌苏市| 乌鲁木齐县| 徐州市| 富顺县| 盘山县| 香港 | 砚山县| 宁阳县| 黔西县| 丰原市| 宿松县| 泸溪县| 紫金县| 米易县| 北票市| 同江市| 静宁县| 来凤县| 仲巴县| 屯昌县| 固安县| 马公市| 怀远县| 灵山县| 巴林右旗| 娄底市| 道孚县| 特克斯县| 勃利县| 客服| 西乌| 仙桃市| 恭城| 新民市| 若羌县| 武城县| 林周县| 深水埗区| 承德县|