C#のWinFormsでメニューバーをつけたいのですが、なぜか一番上のコントロールが隠れてしまいます。
ちなみにButton等のコントロールはTableLayoutPanelにDockStyle.Fillで貼り付けしています。
また、Visual Studio C#は使わないという前提です。
一応Windows APIみたいにX軸やY軸等を指定して貼り付ける方法でなら今回の問題は簡単に解決します。
ですがフォームのサイズが変更された等のイベントが面倒なのでTableLayoutPanelのようなものに任せたいと思っています。
その場合、メニューバーをつけるとデフォルトでは一番上にあるコントロールがメニューバーに隠れてしまいます。
以下のコードでいえばButton button1ですね。これが隠れてしまいます。
このbutton1がメニューバーの下から開始するにはどうすればいいでしょうか?
つまり、(メニューバー等以外での)クライアント領域はメニューバーや(今回はありませんが)ステータスバーを除いた範囲にしたいのです。
C#
// main.cs using System; using System.Windows.Forms; using Sample.Forms; namespace Sample{ class EntryPoint{ [STAThread] static void Main( string[] args ){ try{ Application.Run( new MainForm() ); }catch( System.Exception err ){ MessageBox.Show( err.Message ); } } } }
C#
// MainForm.cs using System.Drawing; using System.Windows.Forms; namespace Sample{ namespace Forms{ class MainForm : Form{ public MainForm(){ var toolStripContainer = new ToolStripContainer(); toolStripContainer.Dock = DockStyle.Fill; this.Controls.Add( toolStripContainer ); menuStrip1 = new MenuStrip(); this.SuspendLayout(); menuStrip1.SuspendLayout(); toolStripContainer.Controls.Add( menuStrip1 ); var items = new ToolStripMenuItem[2]; // 最初のアイテムを生成 items[0] = new ToolStripMenuItem(); items[0].Text = "ファイル(&F)"; menuStrip1.Items.Add( items[0] ); // サブメニュー(item[0]のサブ)を生成 items[1] = new ToolStripMenuItem(); items[1].Text = "開く(&O)"; items[1].Click += items1_Click; items[0].DropDownItems.Add( items[1] ); var tableLayouPanel = new TableLayoutPanel(); tableLayouPanel.ColumnCount = 1; tableLayouPanel.RowCount = 2; tableLayouPanel.Dock = DockStyle.Fill; this.Controls.Add( tableLayouPanel ); button1 = new Button(); button1.Location = new Point( 10, 30 ); button1.Size = new Size( 100, 20 ); button1.Text = "button1(&B)"; button1.Dock = DockStyle.Fill; button1.Click += button1_Click; tableLayouPanel.Controls.Add( button1 ); // レイアウトロジック再開 menuStrip1.ResumeLayout( false ); menuStrip1.PerformLayout(); this.ResumeLayout( false ); this.PerformLayout(); } private void items1_Click( object sender, System.EventArgs e ){ MessageBox.Show( "items[1]が押された" ); } private void button1_Click( object sender, System.EventArgs e ){ MessageBox.Show( "button1が押された" ); } private Button button1; private MenuStrip menuStrip1; } } }
ToolStripの場合はToolStripContainerというクラスがあり、それを使えばいいようですが、
メニューバー系のContainerが見つかりません。
やはりメニューバーを使うにはbutton1等のコントロールはX軸やらY軸やらを指定する方法しかないのでしょうか?
[情報]
言語: C#
.NET: .NET5 ( 5.0.201 )
VS: Visual Studio C#は使わない (訳があってインストールすらできない)
まだ回答がついていません
会員登録して回答してみよう