Control.Region 属性

获取或设置与控件关联的窗口区域。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property Region As Region
用法
Dim instance As Control
Dim value As Region

value = instance.Region

instance.Region = value
public Region Region { get; set; }
public:
property Region^ Region {
    Region^ get ();
    void set (Region^ value);
}
/** @property */
public Region get_Region ()

/** @property */
public void set_Region (Region value)
public function get Region () : Region

public function set Region (value : Region)

属性值

与控件关联的窗口 Region

备注

窗口区域是操作系统允许在其中进行绘制的窗口内的像素的集合。操作系统不显示位于窗口区域之外的窗口的任何部分。控件的区域的坐标相对于控件的左上角,而不是相对于控件的工作区。

提示

该区域包含的像素的集合可以是不连续的。

示例

下面的代码示例阐释了如何通过创建圆形按钮来使用 Region 属性。若要运行该示例,请将下面的代码粘贴到一个包含 roundButton 按钮的窗体中。该示例要求 Paint 事件已连接到该示例中定义的事件处理程序。

' 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 Sub roundButton_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles roundButton.Paint

    Dim buttonPath As New System.Drawing.Drawing2D.GraphicsPath

    ' Set a new rectangle to the same size as the button's 
    ' ClientRectangle property.
    Dim newRectangle As Rectangle = 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)
    e.Graphics.DrawPath(Pens.Black, buttonPath)
    ' Set the button's Region property to the newly created 
    ' circle region.
    roundButton.Region = New System.Drawing.Region(buttonPath)

End Sub
// 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);

}
private:
   // 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.
   void roundButton_Paint( Object^ sender,
      System::Windows::Forms::PaintEventArgs^ e )
   {
      System::Drawing::Drawing2D::GraphicsPath^ buttonPath =
         gcnew 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 = gcnew System::Drawing::Region( buttonPath );
   }
// 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.get_ClientRectangle();
    // Decrease the size of the rectangle.
    newRectangle.Inflate(-10, -10);
    // Draw the button's border.
    e.get_Graphics().DrawEllipse(System.Drawing.Pens.get_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.set_Region(new System.Drawing.Region(buttonPath));
} //roundButton_Paint 

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
Control.ClientRectangle 属性
Control.Bounds 属性