开发者

Creating Rounded corner container in .net winform

开发者 https://www.devze.com 2023-02-15 16:25 出处:网络
I want to create the rounded corner container in winform .net. M开发者_StackOverflowy aim is to create a container such that if I dropped any other control within it, that control will also become rou

I want to create the rounded corner container in winform .net. M开发者_StackOverflowy aim is to create a container such that if I dropped any other control within it, that control will also become round shape.

Is this possible?


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


Control.ControlAdded Event

Control.Region

Control.DesignMode

You can alter the Region of child controls when they are added to the parent control.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号