2011-03-08 77 views
2

我想在winform.net中創建圓角容器。我的目標是創建一個容器,以便在其中放置任何其他控件時,該控件也將變成圓形。在.net winform中創建圓角容器

這可能嗎?

+0

添加一些圖片來準確解釋你的問題 – 2011-03-08 06:35:28

回答

5

您正在尋找Control.Region property,它允許您設置與特定控件關聯的窗口區域。操作系統不會繪製或顯示位於窗口區域之外的窗口的任何部分。

文檔給出瞭如何使用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, 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); 
}