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

方法一:
增加命名空間:using System.Drawing.Drawing2D;
添加方法如下:當然各角的點可根據(jù)需要確定.
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ù)自己的需要調整到最佳效
方法二:
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系統(tǒng)API行數(shù),修改控件和窗體為橢圓形狀。代碼如下所示:
[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);
}
相關文章
C#并發(fā)實戰(zhàn)記錄之Parallel.ForEach使用
這篇文章主要給大家介紹了關于C#并發(fā)實戰(zhàn)記錄之Parallel.ForEach使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用C#具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08
C#使用oledb導出數(shù)據(jù)到excel的方法
這篇文章主要介紹了C#使用oledb導出數(shù)據(jù)到excel的方法,結合實例形式分析了C#操作oledb導出數(shù)據(jù)的相關技巧與注意事項,需要的朋友可以參考下2016-06-06
C#編程調用Cards.dll實現(xiàn)圖形化發(fā)牌功能示例
這篇文章主要介紹了C#編程調用Cards.dll實現(xiàn)圖形化發(fā)牌功能,結合實例形式分析了C#動態(tài)鏈接庫調用及圖形操作技巧,需要的朋友可以參考下2017-06-06
C#開發(fā)WinForm清空DataGridView控件綁定的數(shù)據(jù)
本文詳細講解了C#開發(fā)WinForm清空DataGridView控件綁定數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
C#簡單嵌套flash讀取數(shù)據(jù)的實現(xiàn)代碼
這篇文章主要介紹了C#簡單嵌套flash讀取數(shù)據(jù)的實現(xiàn)代碼,有需要的朋友可以參考一下2013-11-11

