AnchorStyles 列挙体
この列挙体には、メンバ値のビットごとの組み合わせを可能にする FlagsAttribute 属性が含まれています。
名前空間: System.Windows.Formsアセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
<FlagsAttribute> _ Public Enumeration AnchorStyles
メンバ名 | 説明 | |
---|---|---|
Bottom | コントロールがそのコンテナの下端に固定されています。 | |
Left | コントロールがそのコンテナの左端に固定されています。 | |
None | コントロールがそのコンテナの端に固定されていません。 | |
Right | コントロールがそのコンテナの右端に固定されています。 | |
Top | コントロールがそのコンテナの上端に固定されています。 |
コントロールがコンテナの端に固定されていると、コンテナのサイズを変更してもコントロールと指定した端の距離は変わりません。たとえば、コントロールがコンテナの右端に固定されていると、コンテナのサイズを変更してもコントロールの右端からコンテナの右端までの距離は変わりません。コントロールは、コントロールの端の任意の組み合わせに対して固定できます。コントロールがコンテナの向かい合う 2 つの端 (たとえば上端と下端) に固定されている場合は、コンテナのサイズを変更するとコントロールのサイズも変更されます。コントロールの Anchor プロパティが AnchorStyles.None に設定されている場合は、コントロールのコンテナのサイズを変更すると、このコンテナの変更分の半分の距離だけコントロールが移動します。たとえば、Button の Anchor プロパティが AnchorStyles.None に設定されているときに、コントロールが配置されている Form のサイズを上下 (または左右) のどちらかの方向に 20 ピクセル変更すると、ボタンは上下 (または左右) の両方向に 10 ピクセルずつ移動します。
フォームに Button を追加し、共通プロパティの一部を設定する例を次に示します。この例では、ボタンをフォームの右下隅に固定することにより、フォームのサイズが変更されても、相対的な位置関係が維持されるようにしています。次に、BackgroundImage を設定して、ボタンを Image と同じサイズに変更します。この例では、TabStop を true に設定し、TabIndex プロパティを設定しています。最後に、ボタンの Click イベントを処理するイベント ハンドラを追加します。この例は、imageList1 という名前の ImageList を作成済みであることを前提にしています。
' 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
// 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: 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.set_Anchor(AnchorStyles.Bottom | AnchorStyles.Right); // Assign a background image. button1.set_BackgroundImage(imageList1.get_Images().get_Item(0)); // Specify the layout style of the background image. Tile is the // default. button1.set_BackgroundImageLayout(ImageLayout.Center); // Make the button the same size as the image. button1.set_Size(button1.get_BackgroundImage().get_Size()); // Set the button's TabIndex and TabStop properties. button1.set_TabIndex(1); button1.set_TabStop(true); // Add a delegate to handle the Click event. button1.add_Click(new System.EventHandler(this.button1_Click)); // Add the button to the form. this.get_Controls().Add(button1); } //AddMyButton
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- AnchorStyles 列挙体のページへのリンク