Control.Anchor Control.Anchor Control.Anchor Control.Anchor Property

定义

获取或设置控件绑定到的容器的边缘并确定控件如何随其父级一起调整大小。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. 此示例要求您具有ImageList名为imageList1的。This 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. 例如, Form如果有Button一个,其Anchor Top 属性值Bottom设置为和,则拉伸以保持与的上边缘和下边缘之间的定位距离Button Form随着的增加, Height FormFor 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. 不需要重写get Anchor属性的和set访问器; 如果需要,只能重写一个。You are not required to override both the get and set accessors of the Anchor property; you can override only one if needed.

适用于

另请参阅