我要创建在WinForm的.NET圆角容器。我的目标是创建一个容器,这样,如果我放弃其中的任何其他控制,即控制也将成为圆形。
I want to create the rounded corner container in winform .net. My aim is to create a container such that if I dropped any other control within it, that control will also become round shape.
这可能吗?
您正在寻找的Control.Region物业,它允许你设置与特定控制关联的窗口区域。操作系统不会画或显示躺着一个窗口区域外的窗口的任何部分。
You're looking for the Control.Region
property, which allows you to set the window region associated with a particular control. The operating system will not draw or display any portion of a window that lies outside of a window region.
该文件提供了如何使用区域
属性来创建一个圆形按钮示例:
The documentation gives a sample of how to use the Region
property to create a round button:
// 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, 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);
}