AnchorStyles AnchorStyles AnchorStyles AnchorStyles Enum

定义

指定如何控件锚定到其容器的边缘。Specifies how a control anchors to the edges of its container.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class AnchorStyles
[System.Flags]
public enum AnchorStyles
type AnchorStyles = 
Public Enum AnchorStyles
继承
属性

字段

Bottom Bottom Bottom Bottom 2

该控件锚定到其容器的下边缘。The control is anchored to the bottom edge of its container.

Left Left Left Left 4

该控件锚定到其容器的左边缘。The control is anchored to the left edge of its container.

None None None None 0

该控件未锚定到其容器的任何边缘。The control is not anchored to any edges of its container.

Right Right Right Right 8

该控件锚定到其容器的右边缘。The control is anchored to the right edge of its container.

Top Top Top Top 1

该控件锚定到其容器上边缘。The control is anchored to the top edge of its container.

示例

下面的示例添加Button向窗体,并设置它的一些常见属性。The following example adds a Button to a form and sets some of its common properties. 示例定位到窗体右下角的按钮,以便在调整窗体大小时,它使其相对位置。The example anchors the button to the bottom-right corner of the form so it keeps its relative position as the form is resized. 接下来设置BackgroundImage调整大小的按钮为具有相同的大小和ImageNext it sets the BackgroundImage and resizes the button to the same size as the Image. 该示例然后设置TabStoptrue,并设置TabIndex属性。The example then sets the TabStop to true and sets the TabIndex property. 最后,它将添加一个事件处理程序以处理Click按钮的事件。Lastly, it adds an event handler to handle the Click event of the button. 此示例假定你拥有ImageList名为imageList1This example assumes you have an ImageList named imageList1.

   // Add a button to a form and set some of its common properties.
private:
   void AddMyButton()
   {
      // Create a button and add it to the form.
      Button^ button1 = gcnew Button;

      // Anchor the button to the bottom right corner of the form
      button1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Bottom | AnchorStyles::Right);

      // Assign a background image.
      button1->BackgroundImage = imageList1->Images[ 0 ];

      // Specify the layout style of the background image. Tile is the default.
      button1->BackgroundImageLayout = ImageLayout::Center;

      // Make the button the same size as the image.
      button1->Size = button1->BackgroundImage->Size;

      // Set the button's TabIndex and TabStop properties.
      button1->TabIndex = 1;
      button1->TabStop = true;

      // Add a delegate to handle the Click event.
      button1->Click += gcnew System::EventHandler( this, &Form1::button1_Click );

      // Add the button to the form.
      this->Controls->Add( button1 );
   }
// Add a button to a form and set some of its common properties.
private void AddMyButton()
{
   // Create a button and add it to the form.
   Button button1 = new Button();

   // Anchor the button to the bottom right corner of the form
   button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

   // Assign a background image.
   button1.BackgroundImage = imageList1.Images[0];

   // Specify the layout style of the background image. Tile is the default.
   button1.BackgroundImageLayout = ImageLayout.Center;
   
   // Make the button the same size as the image.
   button1.Size = button1.BackgroundImage.Size;

   // Set the button's TabIndex and TabStop properties.
   button1.TabIndex = 1;
   button1.TabStop = true;

   // Add a delegate to handle the Click event.
   button1.Click += new System.EventHandler(this.button1_Click);

   // Add the button to the form.
   this.Controls.Add(button1);
}
' Add a button to a form and set some of its common properties.
Private Sub AddMyButton()
   ' Create a button and add it to the form.
   Dim button1 As New Button()
   
   ' Anchor the button to the bottom right corner of the form
   button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
   
   ' Assign a background image.
   button1.BackgroundImage = imageList1.Images(0)

   ' Specify the layout style of the background image. Tile is the default.
   button1.BackgroundImageLayout = ImageLayout.Center
   
   ' Make the button the same size as the image.
   button1.Size = button1.BackgroundImage.Size
   
   ' Set the button's TabIndex and TabStop properties.
   button1.TabIndex = 1
   button1.TabStop = True

   ' Add a delegate to handle the Click event.
   AddHandler button1.Click, AddressOf Me.button1_Click
   
   ' Add the button to the form.
   Me.Controls.Add(button1)
End Sub

注解

当控件锚定到其容器的边缘时,控件和指定的边缘之间的距离将保持不变,容器在调整大小时。When a control is anchored to an edge of its container, the distance between the control and the specified edge remains constant when the container resizes. 例如,如果控件锚定到其容器的右边缘,该控件的右边缘与该容器的右边缘之间的距离保持不变调整容器大小时。For example, if a control is anchored to the right edge of its container, the distance between the right edge of the control and the right edge of the container remains constant when the container resizes. 控件可以定位到控件边缘的任意组合。A control can be anchored to any combination of control edges. 如果该控件锚定到相对边缘与其容器 (例如,若的顶部和底部),其容器调整大小时调整大小。If the control is anchored to opposite edges of its container (for example, to the top and bottom), it resizes when the container resizes. 如果控件具有其Anchor属性设置为无,该控件移动一半调整控件的容器的距离。If a control has its Anchor property set to None, the control moves half of the distance that the container of the control is resized. 例如,如果Button具有其Anchor属性设置为 None 和Form位于该控件调整大小时通过在任一方向的 20 像素,按钮为两个方向移动的 10 个像素。For example, if a Button has its Anchor property set to None and the Form that the control is located on is resized by 20 pixels in either direction, the button will be moved 10 pixels in both directions.

适用于

另请参阅