縦長画面でもスクロールバーを表示して全て見えるようにする
昔あったようなレイアウト。以下備忘録。
イメージ
サンプルソース
using System.Drawing; using System.Windows.Forms; /* * Form に下地として以下を張り付けています。 * * ToolStrip, Dock = Top, 認識しやすいように ToolStripButton を1つ登録 * Panel, Dock = Fill, AutoScroll = true, Name = Panel1 * StatusStrip, Dock = Bottom * * の状態で、 * Panel1 に Panel を子コントロールで登録して、 * 縦長サイズを表示するサンプル(縦スクロールバーが出る) * * * */ namespace WindowsFormsApp33 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 独自画面のつもり // 幅は、自動的に縦スクロールバーが表示されるので、その幅分減らした値をセットする var panel2 = new Panel(); panel2.Width = panel1.Width - SystemInformation.VerticalScrollBarWidth; panel2.Height = 3000; // 左上 var btn1 = new Button(); btn1.Text = nameof(btn1); btn1.Size = new Size(200, 100); btn1.Location = new Point(0, 0); // 左下 var btn2 = new Button(); btn2.Text = nameof(btn2); btn2.Size = new Size(200, 100); btn2.Location = new Point(0, panel2.Bottom - btn2.Height); btn2.Anchor = AnchorStyles.Left | AnchorStyles.Bottom; // 右上 var btn3 = new Button(); btn3.Text = nameof(btn3); btn3.Size = new Size(200, 100); btn3.Location = new Point(panel2.Right - btn3.Width, 0); btn2.Anchor = AnchorStyles.Right | AnchorStyles.Top; // 右下 var btn4 = new Button(); btn4.Text = nameof(btn4); btn4.Size = new Size(200, 100); btn4.Location = new Point(panel2.Right - btn3.Width, panel2.Bottom - btn2.Height); btn2.Anchor = AnchorStyles.Right | AnchorStyles.Bottom; panel2.Controls.AddRange(new Control[] { btn1, btn2, btn3, btn4 }); panel1.Controls.Add(panel2); // 下地のパネル(表示領域)がサイズ変更したら、連動して独自画面レイアウトも更新する panel1.SizeChanged += (_, __) => { panel2.Width = panel1.Width - SystemInformation.VerticalScrollBarWidth; //btn1.Location = new Point(0, 0); btn2.Location = new Point(0, panel2.Bottom - btn2.Height); btn3.Location = new Point(panel2.Right - btn3.Width, 0); btn4.Location = new Point(panel2.Right - btn3.Width, panel2.Bottom - btn2.Height); }; } } }