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. 例如,如果您的 Form 具有 Anchor 屬性值設定為 TopBottomButton,則 Button 會伸展,以維持 Form 的上而下邊緣的錨定,這會增加 HeightFormFor 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.

適用於

另請參閱