程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 開辟圓角控件(窗體)的詳細完成

C# 開辟圓角控件(窗體)的詳細完成

編輯:C#入門知識

C# 開辟圓角控件(窗體)的詳細完成。本站提示廣大學習愛好者:(C# 開辟圓角控件(窗體)的詳細完成)文章只能為提供參考,不一定能成為您想要的結果。以下是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);
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved