通过


AnchorStyles 枚举

定义

指定控件如何定位到其容器边缘。

此枚举支持其成员值的按位组合。

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

字段

名称 说明
None 0

控件未定位到其容器的任何边缘。

Top 1

控件定位到其容器的上边缘。

Bottom 2

控件定位到其容器的下边缘。

Left 4

控件定位到其容器的左边缘。

Right 8

控件定位在其容器的右边缘。

示例

以下示例向窗体添加一个 Button 值,并设置其一些常见属性。 该示例将按钮定位到窗体的右下角,以便在调整窗体大小时保持其相对位置。 接下来, BackgroundImage 它将按钮的大小设置为与 相同的大小并重设其大小 Image。 然后,该示例设置该 TabStop 属性 true 并设置该 TabIndex 属性。 最后,它会添加事件处理程序来处理 Click 按钮的事件。 此示例假定你有一个 ImageList 命名名称 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

注解

当控件定位到其容器的边缘时,当容器调整大小时,控件与指定边缘之间的距离保持不变。 例如,如果控件锚定在其容器的右边缘,则当容器调整大小时,控件的右边缘与容器右边缘之间的距离保持不变。 控件可以定位到控件边缘的任意组合。 如果控件定位到其容器的相反边缘(例如,顶部和底部),则当容器调整大小时,它将调整大小。 如果控件的属性 Anchor 设置为“无”,控件将移动控件容器调整大小的一半距离。 例如,如果某个ButtonAnchor属性设置为“无”,并且Form控件位于的任意方向的大小将调整为 20 像素,则按钮将在两个方向上移动 10 像素。

适用于

另请参阅