最近在做卡片視圖的程序,要求將控件做成帶有圓角的效果,下面是我在網上查找的資料,經過測試,確定可以實現功能。其中方法三既適應於控件,也適應於窗體。
先上傳效果圖:
方法一:
增加命名空間:using System.Drawing.Drawing2D;
添加方法如下:當然各角的點可根據需要確定.
Type(Control sender, p_1, GraphicsPath oPath = Point(, sender.Height / Point(sender.Width / p_1, Point(sender.Width - sender.Width / p_1, Point(sender.Width, sender.Height / Point(sender.Width, sender.Height - sender.Height / Point(sender.Width - sender.Width / Point(sender.Width / Point(, sender.Height - sender.Height / ( sender.Region = }
在窗體的paint和resize事件中增加:Type(this,20,0.1);
參數20和0.1也可以根據自己的需要調整到最佳效
方法二:
FormPath = Rectangle rect = Rectangle(, , .Width, .Height - ); FormPath = GetRoundedRectPath(rect, .Region = GraphicsPath GetRoundedRectPath(Rectangle rect, diameter = Rectangle arcRect = Rectangle(rect.Location, GraphicsPath path = path.AddArc(arcRect, , arcRect.X = rect.Right - path.AddArc(arcRect, , arcRect.Y = rect.Bottom - path.AddArc(arcRect, , arcRect.X = path.AddArc(arcRect, , }
在窗體的resize事件中增加:SetWindowRegion();
方法三:通過Window系統API行數,修改控件和窗體為橢圓形狀。代碼如下所示:
[System.Runtime.InteropServices.DllImport( [System.Runtime.InteropServices.DllImport( SetBkMode(IntPtr hdc, TRANSPARENT = [System.Runtime.InteropServices.DllImport( [System.Runtime.InteropServices.DllImport( [System.Runtime.InteropServices.DllImport( Ellipse(IntPtr hdc, x1, y1, x2, [System.Runtime.InteropServices.DllImport( IntPtr SetWindowRgn(IntPtr hwnd, IntPtr hRgn, [System.Runtime.InteropServices.DllImport( IntPtr GetDC(IntPtr hwnd);
dc = GetDC( Ellipse(dc, , , .Width - , .Height - region = SetWindowRgn(.Handle, region, }