AnchorStyles 枚举

定义

指定控件如何锚定到其容器的边缘。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
继承
AnchorStyles
属性

字段

Bottom 2

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

Left 4

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

None 0

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

Right 8

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

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将设置按钮的大小, 并将其大小Image调整为与相同。Next it sets the BackgroundImage and resizes the button to the same size as the Image. 然后, 该示例将TabStop设置true为并设置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. 此示例假设你有一个ImageListimageList1为的。This 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. 例如, 如果将ButtonAnchor属性设置为 "无", 并且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.

适用于

另请参阅