带有自定义边框和圆角的C#形

带有自定义边框和圆角的C#形,c#,winforms,rounded-corners,formborderstyle,C#,Winforms,Rounded Corners,Formborderstyle,我正在使用此代码使我的表单(FormBorderStyle=none)具有圆角边缘: [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn ( int nLeftRect, // x-coordinate of upper-left corner int nTopRect, // y-coordinate of uppe

我正在使用此代码使我的表单(FormBorderStyle=none)具有圆角边缘:

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
    int nLeftRect, // x-coordinate of upper-left corner
    int nTopRect, // y-coordinate of upper-left corner
    int nRightRect, // x-coordinate of lower-right corner
    int nBottomRect, // y-coordinate of lower-right corner
    int nWidthEllipse, // height of ellipse
    int nHeightEllipse // width of ellipse
 );

public Form1()
{
    InitializeComponent();
    Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
要在绘制事件上设置自定义边框,请执行以下操作:

    ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid);
但是看看这个

内部表单矩形没有圆角边


如何使蓝色的内部形状矩形也具有圆形边缘,使其看起来不像屏幕截图?

区域属性只是将角切掉。要获得真正的圆角,您必须绘制圆角矩形


绘制所需形状的图像并将其放置在透明窗体上可能更容易。易于绘制,但无法调整大小。

注意,您正在泄漏返回的句柄,使用后应将其释放

FromHrgn()复制定义,因此不会释放句柄

[DllImport("Gdi32.dll", EntryPoint = "DeleteObject")]
public static extern bool DeleteObject(IntPtr hObject);

public Form1()
{
    InitializeComponent();
    IntPtr handle = CreateRoundRectRgn(0, 0, Width, Height, 20, 20);
    if (handle == IntPtr.Zero)
        ; // error with CreateRoundRectRgn
    Region = System.Drawing.Region.FromHrgn(handle);
    DeleteObject(handle);
}
(将添加评论,但声誉受损)