AnchorStyles 列舉

定義

指定控制項如何在其容器 (Container) 的邊緣錨定。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. 這個範例假設您有一個名為 imageList1ImageListThis 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. 例如,如果 Button 將其 [Anchor] 屬性設定為 [無],而控制項所在的 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.

適用於

另請參閱