最近在作卡片視圖的程序,要求將控件作成帶有圓角的效果,下面是我在網上查找的資料,通過測試,肯定能夠實現功能。其中方法三既適應於控件,也適應於窗體。ide
先上傳效果圖:測試
方法一:this
增長命名空間:using System.Drawing.Drawing2D;
添加方法以下:固然各角的點可根據須要肯定. spa
1 private void Type(Control sender, int p_1, double p_2) 2 { 3 GraphicsPath oPath = new GraphicsPath(); 4 oPath.AddClosedCurve( 5 new Point[] { 6 new Point(0, sender.Height / p_1), 7 new Point(sender.Width / p_1, 0), 8 new Point(sender.Width - sender.Width / p_1, 0), 9 new Point(sender.Width, sender.Height / p_1), 10 new Point(sender.Width, sender.Height - sender.Height / p_1), 11 new Point(sender.Width - sender.Width / p_1, sender.Height), 12 new Point(sender.Width / p_1, sender.Height), 13 new Point(0, sender.Height - sender.Height / p_1) }, 14 15 (float)p_2); 16 17 sender.Region = new Region(oPath); 18 }
在窗體的paint和resize事件中增長:Type(this,20,0.1);
參數20和0.1也能夠根據本身的須要調整到最佳效code
方法二:orm
1 public void SetWindowRegion() 2 { 3 4 System.Drawing.Drawing2D.GraphicsPath FormPath; 5 6 FormPath = new System.Drawing.Drawing2D.GraphicsPath(); 7 8 Rectangle rect = new Rectangle(0, 22, this.Width, this.Height - 22);//this.Left-10,this.Top-10,this.Width-10,this.Height-10); 9 10 FormPath = GetRoundedRectPath(rect, 30); 11 12 this.Region = new Region(FormPath); 13 14 } 15 16 private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) 17 { 18 19 int diameter = radius; 20 21 Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter)); 22 23 GraphicsPath path = new GraphicsPath(); 24 25 // 左上角 26 27 path.AddArc(arcRect, 180, 90); 28 29 // 右上角 30 31 arcRect.X = rect.Right - diameter; 32 33 path.AddArc(arcRect, 270, 90); 34 35 // 右下角 36 37 arcRect.Y = rect.Bottom - diameter; 38 39 path.AddArc(arcRect, 0, 90); 40 41 // 左下角 42 43 arcRect.X = rect.Left; 44 45 path.AddArc(arcRect, 90, 90); 46 47 path.CloseFigure(); 48 49 return path; 50 51 }
在窗體的resize事件中增長:SetWindowRegion(); blog
方法三:經過Window系統API行數,修改控件和窗體爲橢圓形狀。代碼以下所示:事件
1 [System.Runtime.InteropServices.DllImport("gdi32")] 2 private static extern IntPtr BeginPath(IntPtr hdc); 3 [System.Runtime.InteropServices.DllImport("gdi32")] 4 private static extern int SetBkMode(IntPtr hdc, int nBkMode); 5 const int TRANSPARENT = 1; 6 [System.Runtime.InteropServices.DllImport("gdi32")] 7 private static extern IntPtr EndPath(IntPtr hdc); 8 [System.Runtime.InteropServices.DllImport("gdi32")] 9 private static extern IntPtr PathToRegion(IntPtr hdc); 10 [System.Runtime.InteropServices.DllImport("gdi32")] 11 private static extern int Ellipse(IntPtr hdc, int x1, int y1, int x2, int y2); 12 [System.Runtime.InteropServices.DllImport("user32")] 13 private static extern IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, bool bRedraw); 14 [System.Runtime.InteropServices.DllImport("user32")] 15 private static extern IntPtr GetDC(IntPtr hwnd);
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 5 IntPtr dc; 6 IntPtr region; 7 8 dc = GetDC(this.Handle); 9 BeginPath(dc); 10 SetBkMode(dc, TRANSPARENT); 11 Ellipse(dc, 0, 0, this.Width - 3, this.Height - 2); 12 EndPath(dc); 13 region = PathToRegion(dc); 14 SetWindowRgn(this.Handle, region, false); 15 }