C#中带有圆角边框的表单?

12
我正在使用这段代码使表单没有边框样式:
this.FormBorderStyle = FormBorderStyle.None;

我需要在表单上制作圆角。

有没有简单的方法?我该如何做?


这个问题的答案可能会有所帮助:https://dev59.com/9m445IYBdhLWcg3wAFcC - Ian Gregory
看起来很棒,但是...我是新手,所以...我不知道把所有的东西放在哪里。我知道在form()下面放代码的位置,但其他的很难。你能帮我吗? - Hunter Mitchell
3个回答

2
请看这个链接:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx Form类继承自Control类,因此请尝试在窗体事件中完成与链接中“Form的Region属性”相同的示例。请注意保留HTML标签。
    // This method will change the square button to a circular button by 
// creating a new circle-shaped GraphicsPath object and setting it 
// to the RoundButton objects region.
private void roundButton_Paint(object sender, 
    System.Windows.Forms.PaintEventArgs e)
{

    System.Drawing.Drawing2D.GraphicsPath buttonPath = 
        new System.Drawing.Drawing2D.GraphicsPath();

    // Set a new rectangle to the same size as the button's 
    // ClientRectangle property.
    System.Drawing.Rectangle newRectangle = roundButton.ClientRectangle;

    // Decrease the size of the rectangle.
    newRectangle.Inflate(-10, -10);

    // Draw the button's border.
    e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);

    // Increase the size of the rectangle to include the border.
    newRectangle.Inflate( 1,  1);

    // Create a circle within the new rectangle.
    buttonPath.AddEllipse(newRectangle);

    // Set the button's Region property to the newly created 
    // circle region.
    roundButton.Region = new System.Drawing.Region(buttonPath);

}

1
我知道这个问题已经有答案了,但是我想补充一种替代方案,虽然很愚蠢但并不推荐,因为你的问题并没有限制答案只能是代码...
  • 创建一个空白的正方形图像,将其背景颜色填充,然后把左上角的圆角擦成透明,重复此步骤到所有角落
  • 将一个非常不可能出现的颜色设置为您表单的背景颜色
  • 将此颜色设置为您表单上的TransparencyKey
  • 将图像作为PictureBox添加,并将它们放置在相应的角落

哇!


1
    public static void RoundBorderForm(Form frm)
    {

        Rectangle Bounds = new Rectangle(0, 0, frm.Width, frm.Height);
        int CornerRadius = 20;
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
        path.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        path.CloseAllFigures();

        frm.Region = new Region(path);
        frm.Show();
    }

3
请提供为什么这个解决方案能解决问题的上下文;代码不应该替代文字!感谢你对[so]的贡献。 - jpaugh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接