ウィンドウのサイズを変えたときのボタンの振る舞い

ウィンドウのサイズをマウスでいじるときに、配置されたコントロールの動きというか振る舞いを規定したい。それらはボタンのプロパティの中の「Layout」の「Anchor」で調整することができる。

今の場合だと、このボタン(Button1)はウィンドウの左側と上側からの距離が固定されていることになる。そのため、画面の大きさを変えてもボタンの位置は左上に固定されたまま変わらず。ボタンのサイズも変わらない。

また、Button2は、ウィンドウの右側と下側からの距離を固定した状態に設定する。

そうすると、ウィンドウの大きさを変えた場合には、ボタンの位置は右下に固定されたまま変わらず。実行例を下図に示す。



作成されたソースコードは以下の通り。

import datetime
import System.Drawing
import System.Drawing.Printing
import System.Windows.Forms

from System.Drawing import *
from System.Drawing.Printing import *
from System.Windows.Forms import *

class MainForm(Form):
    def __init__(self):
        self.InitializeComponent()
   
    def InitializeComponent(self):
        self._button1 = System.Windows.Forms.Button()
        self._button2 = System.Windows.Forms.Button()
        self.SuspendLayout()
        #
        # button1
        #
        self._button1.Font = System.Drawing.Font("メイリオ", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 128)
        self._button1.Location = System.Drawing.Point(50, 25)
        self._button1.Name = "button1"
        self._button1.Size = System.Drawing.Size(180, 45)
        self._button1.TabIndex = 2
        self._button1.Text = "Button1"
        self._button1.UseVisualStyleBackColor = True
        #
        # button2
        #
        self._button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right
        self._button2.Font = System.Drawing.Font("メイリオ", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 128)
        self._button2.Location = System.Drawing.Point(50, 90)
        self._button2.Name = "button2"
        self._button2.Size = System.Drawing.Size(180, 45)
        self._button2.TabIndex = 3
        self._button2.Text = "Button2"
        self._button2.UseVisualStyleBackColor = True
        #
        # MainForm
        #
        self.ClientSize = System.Drawing.Size(283, 163)
        self.Controls.Add(self._button2)
        self.Controls.Add(self._button1)
        self.Name = "MainForm"
        self.Text = "Test"
        self.ResumeLayout(False)