Control.Anchor 属性

定义

获取或设置控件绑定到的容器的边缘并确定控件如何随其父级一起调整大小。Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent.

public:
 virtual property System::Windows::Forms::AnchorStyles Anchor { System::Windows::Forms::AnchorStyles get(); void set(System::Windows::Forms::AnchorStyles value); };
public virtual System.Windows.Forms.AnchorStyles Anchor { get; set; }
member this.Anchor : System.Windows.Forms.AnchorStyles with get, set
Public Overridable Property Anchor As AnchorStyles

属性值

AnchorStyles 值的按位组合。A bitwise combination of the AnchorStyles values. 默认值是 TopLeftThe default is Top and Left.

示例

下面的代码示例将 Button 添加到窗体中,并设置其一些公共属性。The following code 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. 此示例要求你有一个名为 imageList1ImageListThis example requires that 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

注解

使用 Anchor 属性可定义控件在其父控件调整大小时如何自动调整大小。Use the Anchor property to define how a control is automatically resized as its parent control is resized. 将控件锚定到其父控件可确保在调整父控件的大小时,定位边缘与父控件边缘的相对位置保持不变。Anchoring a control to its parent control ensures that the anchored edges remain in the same position relative to the edges of the parent control when the parent control is resized.

可以将控件锚定到其容器的一个或多个边缘。You can anchor a control to one or more edges of its container. 例如,如果您的 FormButtonAnchor 属性值设置为 TopBottom,则拉伸 Button 以保持与 Form 的上边缘和下边缘之间的定位距离 @no__Form 增加了 t_7_。For example, if you have a Form with a Button whose Anchor property value is set to Top and Bottom, the Button is stretched to maintain the anchored distance to the top and bottom edges of the Form as the Height of the Form is increased.

备注

AnchorDock 属性互相排斥。The Anchor and Dock properties are mutually exclusive. 一次只能设置一个,而最后一个集则优先。Only one can be set at a time, and the last one set takes precedence.

继承者说明

在派生类中重写 Anchor 属性时,请使用基类的 Anchor 属性来扩展基实现。When overriding the Anchor property in a derived class, use the base class's Anchor property to extend the base implementation. 否则,必须提供所有实现。Otherwise, you must provide all the implementation. 不需要重写 Anchor 属性的 getset 访问器;如果需要,只能重写一个。You are not required to override both the get and set accessors of the Anchor property; you can override only one if needed.

适用于

另请参阅