【问题标题】:C# Form with custom border and rounded edges [duplicate]具有自定义边框和圆角边缘的 C# 表单 [重复]
【发布时间】:2011-07-02 19:25:09
【问题描述】:

我正在使用此代码使我的表单 (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));
}

这是在 Paint 事件上设置自定义边框:

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

但是看到这个。

内部窗体矩形没有圆角。

我怎样才能使蓝色的内部矩形也有圆角边缘,这样它就不会像屏幕截图那样?

【问题讨论】:

    标签: c# winforms rounded-corners formborderstyle


    【解决方案1】:

    注意你正在泄漏CreateRoundRectRgn()返回的句柄,你应该在使用后用DeleteObject()释放它。

    Region.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);
    }
    

    (将作为评论添加,但声誉已被删除)

    【讨论】:

      【解决方案2】:

      Region 属性只是切断了角落。要获得真正的圆角,您必须绘制圆角矩形。

      Drawing rounded rectangles

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

      【讨论】:

        猜你喜欢
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-02
        • 1970-01-01
        • 1970-01-01
        • 2016-02-20
        • 1970-01-01
        相关资源
        最近更新 更多