C#中带圆角边框的表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10674228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form with Rounded Borders in C#?
提问by Hunter Mitchell
I am using this code to make the form have no border style:
我正在使用此代码使表单没有边框样式:
this.FormBorderStyle = FormBorderStyle.None;
I need to make rounded edges on the form.
我需要在表格上做圆边。
Is there an easy way? How do I do it?
有没有简单的方法?我该怎么做?
回答by eyossi
Take a look at this: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx
看看这个:http: //msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx
The Form class inherits from the Control class, so try doing the same sample that you have on the link to the Form's Region property(and do it on the form event of course):
Form 类继承自 Control 类,因此请尝试执行与Form 的 Region 属性的链接相同的示例(当然,并在表单事件上执行此操作):
// 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);
}
回答by Milad Akbary
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();
}
回答by TweakBox
I know the question was already answered, I would like to add an alternative and silly BUT a not really recommended practice since your question does not restrict the answer into being codes...
我知道这个问题已经回答了,我想添加一个替代的和愚蠢的但不是真正推荐的做法,因为你的问题并没有将答案限制为代码......
- Create a blank, square image with your background color as fill, then erase the top-left rounded corners to be transparent, repeat this to all corners
- Set a very unlikely color as your form background color
- Set this color as the
TransparencyKeyon your form - Add the images as
PictureBoxand put them to the corresponding corners
- 用你的背景色作为填充创建一个空白的方形图像,然后擦除左上角的圆角使其透明,对所有角重复此操作
- 设置一个非常不可能的颜色作为表单背景颜色
- 将此颜色设置为
TransparencyKey表单上的 - 将图像添加为
PictureBox并将它们放在相应的角落
Viola!
中提琴!

