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

首頁 > 學院 > 開發設計 > 正文

C# 開發圓角控件(窗體)

2019-11-17 03:20:01
字體:
來源:轉載
供稿:網友

C# 開發圓角控件(窗體)

最近在做卡片視圖的程序,要求將控件做成帶有圓角的效果,下面是我在網上查找的資料,經過測試,確定可以實現功能。其中方法三既適應于控件,也適應于窗體。

先上傳效果圖:

方法一:

增加命名空間:usingSystem.Drawing.Drawing2D;添加方法如下:當然各角的點可根據需要確定.

 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也可以根據自己的需要調整到最佳效

方法二:

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

方法三:通過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         }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 固阳县| 雅江县| 临潭县| 闻喜县| 罗甸县| 文成县| 中卫市| 达拉特旗| 基隆市| 武城县| 清徐县| 顺平县| 古蔺县| 上犹县| 辽源市| 新余市| 思茅市| 江华| 博野县| 武宣县| 大石桥市| 景泰县| 廉江市| 道真| 辉南县| 双辽市| 台北县| 南郑县| 武定县| 南投县| 荔波县| 绥滨县| 任丘市| 拉萨市| 离岛区| 伊川县| 西华县| 营山县| 甘德县| 宁明县| 双峰县|