
C#でメニューバーの項目を作成できる?多階層や拡張など作成方法を解説
- SE
- C#でメニューバーの項目を作成できるそうですが、実際のどのようにコードを書いていくのでしょうか?
- PM
- メニューバーを階層構造にすることも可能です。では、実際のソースコードで詳しくみていきましょう。
目次
C#のメニューバーについて
今回は、C#のメニューバーについて説明します。C#では、メニューを独自に作成できるため、ショートカットキーを割り当てたり、多階層のメニューにすることができます。また、メニュー自体を非表示にすることもできます。
C#の「メニューバー」に興味のある方は、ぜひご覧ください。
メニューバーの作成
C#では、独自にメニューバーを作成できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { MenuStrip menuStrip; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { // MenuStrip(メニューバー)を作成する this.menuStrip = new MenuStrip(); //レイアウトロジックを停止する this.SuspendLayout(); this.menuStrip.SuspendLayout(); // ToolStripMenuItemを作成する ToolStripMenuItem fileMenuItem = new ToolStripMenuItem(); //「ファイル(&F)」 ToolStripMenuItem openMenuItem = new ToolStripMenuItem(); //「開く(&O)...」 ToolStripMenuItem saveMenuItem = new ToolStripMenuItem(); //「上書き保存(&S)」 ToolStripMenuItem saveAsMenuItem = new ToolStripMenuItem(); //「名前を付けて保存(&A)...」 ToolStripMenuItem exitMenuItem = new ToolStripMenuItem(); //「終了(&X)」メニュー // テキストを設定する fileMenuItem.Text = ""ファイル(&F)""; openMenuItem.Text = ""開く(&O)...""; saveMenuItem.Text = ""上書き保存(&S)""; saveAsMenuItem.Text = ""名前を付けて保存(&A)...""; exitMenuItem.Text = ""終了(&X)""; // Clickイベントハンドラを追加する openMenuItem.Click += OpenMenuItem_Click; saveMenuItem.Click += SaveMenuItem_Click; saveAsMenuItem.Click += SaveAsMenuItem_Click; exitMenuItem.Click += ExitMenuItem_Click; // MenuStripに追加する this.menuStrip.Items.Add(fileMenuItem); fileMenuItem.DropDownItems.Add(openMenuItem); fileMenuItem.DropDownItems.Add(saveMenuItem); fileMenuItem.DropDownItems.Add(saveAsMenuItem); fileMenuItem.DropDownItems.Add(new ToolStripSeparator()); // セパレータ fileMenuItem.DropDownItems.Add(exitMenuItem); // フォームにMenuStripを追加する this.Controls.Add(this.menuStrip); this.MainMenuStrip = this.menuStrip; // レイアウトロジックを再開する this.menuStrip.ResumeLayout(false); this.menuStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } private void OpenMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「開く」が選択されました。""); } private void SaveMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「上書き保存」が選択されました。""); } private void SaveAsMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「名前を付けて保存」が選択されました。""); } private void ExitMenuItem_Click(object sender, EventArgs e) { // プログラムを終了する this.Close(); } } } |
メニューバーが表示され、メニュー項目を選択すると「MessageBox」が表示されることが分かります。「終了」を選択すれば、プログラムが終了されます。
このように、C#では独自にメニューバーを作成できます。
ショートカットキー
C#では、「ShortcutKeysプロパティ」でもショートカットキーを指定できます。また、メニューにショートカットキーも表示したいときは「ShowShortcutKeysプロパティ」を使用します。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 |
// ショートカットキーの設定 openMenuItem.ShortcutKeys = Keys.Control | Keys.O; openMenuItem.ShowShortcutKeys = true; saveMenuItem.ShortcutKeys = Keys.Control | Keys.S; saveMenuItem.ShowShortcutKeys = true; |
このとき、Ctrl-OやCtrl-Sを実行すると「ショートカットキー」が有効になっていることが分かります。
また、メニュー項目にショートカットキーが表示されていることも分かります。このように、C#では、「ShortcutKeysプロパティ」でショートカットキーを指定できます。
メニューバーの非表示
C#では、「MenuStrip」の「Visibleプロパティ」で、メニューバーの表示/非表示を切り替えられます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { MenuStrip menuStrip; CheckBox checkBox; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { // MenuStrip(メニューバー)を作成する this.menuStrip = new MenuStrip(); //レイアウトロジックを停止する this.SuspendLayout(); this.menuStrip.SuspendLayout(); // ToolStripMenuItemを作成する ToolStripMenuItem fileMenuItem = new ToolStripMenuItem(); //「ファイル(&F)」 ToolStripMenuItem openMenuItem = new ToolStripMenuItem(); //「開く(&O)...」 ToolStripMenuItem saveMenuItem = new ToolStripMenuItem(); //「上書き保存(&S)」 ToolStripMenuItem saveAsMenuItem = new ToolStripMenuItem(); //「名前を付けて保存(&A)...」 ToolStripMenuItem exitMenuItem = new ToolStripMenuItem(); //「終了(&X)」メニュー // テキストを設定する fileMenuItem.Text = ""ファイル(&F)""; openMenuItem.Text = ""開く(&O)...""; saveMenuItem.Text = ""上書き保存(&S)""; saveAsMenuItem.Text = ""名前を付けて保存(&A)...""; exitMenuItem.Text = ""終了(&X)""; // ショートカットキーの設定 openMenuItem.ShortcutKeys = Keys.Control | Keys.O; openMenuItem.ShowShortcutKeys = true; saveMenuItem.ShortcutKeys = Keys.Control | Keys.S; saveMenuItem.ShowShortcutKeys = true; // Clickイベントハンドラを追加する openMenuItem.Click += OpenMenuItem_Click; saveMenuItem.Click += SaveMenuItem_Click; saveAsMenuItem.Click += SaveAsMenuItem_Click; exitMenuItem.Click += ExitMenuItem_Click; // MenuStripに追加する this.menuStrip.Items.Add(fileMenuItem); fileMenuItem.DropDownItems.Add(openMenuItem); fileMenuItem.DropDownItems.Add(saveMenuItem); fileMenuItem.DropDownItems.Add(saveAsMenuItem); fileMenuItem.DropDownItems.Add(new ToolStripSeparator()); // セパレータ fileMenuItem.DropDownItems.Add(exitMenuItem); // チェックボックスをトグルボタンにする checkBox = new CheckBox(); checkBox.Location = new Point(10, 30); checkBox.Text = ""メニュー非表示""; checkBox.TextAlign = ContentAlignment.MiddleCenter; checkBox.Appearance = Appearance.Button; checkBox.CheckedChanged += CheckBox_CheckedChanged; // フォームにMenuStripを追加する this.Controls.Add(this.menuStrip); this.Controls.Add(this.checkBox); this.MainMenuStrip = this.menuStrip; // レイアウトロジックを再開する this.menuStrip.ResumeLayout(false); this.menuStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } private void OpenMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「開く」が選択されました。""); } private void SaveMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「上書き保存」が選択されました。""); } private void SaveAsMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「名前を付けて保存」が選択されました。""); } private void ExitMenuItem_Click(object sender, EventArgs e) { // プログラムを終了する this.Close(); } private void CheckBox_CheckedChanged(object sender, EventArgs e) { if (checkBox.Checked) { menuStrip.Visible = false; checkBox.Text = ""メニュー表示""; } else { menuStrip.Visible = true; checkBox.Text = ""メニュー非表示""; } } } } |
チェックボックスを「トグルボタン」にすることでメニューの表示/非表示を切り替えていることが分かります。
このように、C#では「MenuStrip」の「Visibleプロパティ」で、メニューバーの表示/非表示を切り替えられます。
メニューバーの拡張
C#では、メニューを右に追加することや多階層にすること、さらにチェックを付けることができます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { MenuStrip menuStrip; CheckBox checkBox; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { // MenuStrip(メニューバー)を作成する this.menuStrip = new MenuStrip(); //レイアウトロジックを停止する this.SuspendLayout(); this.menuStrip.SuspendLayout(); // ToolStripMenuItemを作成する ToolStripMenuItem fileMenuItem = new ToolStripMenuItem(); //「ファイル(&F)」 ToolStripMenuItem openMenuItem = new ToolStripMenuItem(); //「開く(&O)...」 ToolStripMenuItem saveMenuItem = new ToolStripMenuItem(); //「上書き保存(&S)」 ToolStripMenuItem saveAsMenuItem = new ToolStripMenuItem(); //「名前を付けて保存(&A)...」 ToolStripMenuItem exitMenuItem = new ToolStripMenuItem(); //「終了(&X)」メニュー ToolStripMenuItem settingMenuItem = new ToolStripMenuItem(); //「設定(&S)」 ToolStripMenuItem colorMenuItem = new ToolStripMenuItem(); //「色」 ToolStripMenuItem colorRedMenuItem = new ToolStripMenuItem(); //「赤」 ToolStripMenuItem colorGreenMenuItem = new ToolStripMenuItem(); //「緑」 ToolStripMenuItem colorBlueMenuItem = new ToolStripMenuItem(); //「青」 // テキストを設定する fileMenuItem.Text = ""ファイル(&F)""; openMenuItem.Text = ""開く(&O)...""; saveMenuItem.Text = ""上書き保存(&S)""; saveAsMenuItem.Text = ""名前を付けて保存(&A)...""; exitMenuItem.Text = ""終了(&X)""; settingMenuItem.Text = ""設定(&S)""; colorMenuItem.Text = ""色""; colorRedMenuItem.Text = ""赤""; colorGreenMenuItem.Text = ""緑""; colorBlueMenuItem.Text = ""青""; // ショートカットキーの設定 openMenuItem.ShortcutKeys = Keys.Control | Keys.O; openMenuItem.ShowShortcutKeys = true; saveMenuItem.ShortcutKeys = Keys.Control | Keys.S; saveMenuItem.ShowShortcutKeys = true; // Clickイベントハンドラを追加する openMenuItem.Click += OpenMenuItem_Click; saveMenuItem.Click += SaveMenuItem_Click; saveAsMenuItem.Click += SaveAsMenuItem_Click; exitMenuItem.Click += ExitMenuItem_Click; colorRedMenuItem.Click += ColorRedMenuItem_Click; colorGreenMenuItem.Click += ColorGreenMenuItem_Click; colorBlueMenuItem.Click += ColorBlueMenuItem_Click; // MenuStripに追加する this.menuStrip.Items.Add(fileMenuItem); this.menuStrip.Items.Add(settingMenuItem); fileMenuItem.DropDownItems.Add(openMenuItem); fileMenuItem.DropDownItems.Add(saveMenuItem); fileMenuItem.DropDownItems.Add(saveAsMenuItem); fileMenuItem.DropDownItems.Add(new ToolStripSeparator()); // セパレータ fileMenuItem.DropDownItems.Add(exitMenuItem); settingMenuItem.DropDownItems.Add(colorMenuItem); colorMenuItem.DropDownItems.Add(colorRedMenuItem); colorMenuItem.DropDownItems.Add(colorGreenMenuItem); colorMenuItem.DropDownItems.Add(colorBlueMenuItem); // チェックボックスをトグルボタンにする checkBox = new CheckBox(); checkBox.Location = new Point(10, 30); checkBox.Text = ""メニュー非表示""; checkBox.TextAlign = ContentAlignment.MiddleCenter; checkBox.Appearance = Appearance.Button; checkBox.CheckedChanged += CheckBox_CheckedChanged; // フォームにMenuStripを追加する this.Controls.Add(this.menuStrip); this.Controls.Add(this.checkBox); this.MainMenuStrip = this.menuStrip; // レイアウトロジックを再開する this.menuStrip.ResumeLayout(false); this.menuStrip.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } private void OpenMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「開く」が選択されました。""); } private void SaveMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「上書き保存」が選択されました。""); } private void SaveAsMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(""「名前を付けて保存」が選択されました。""); } private void ExitMenuItem_Click(object sender, EventArgs e) { // プログラムを終了する this.Close(); } private void CheckBox_CheckedChanged(object sender, EventArgs e) { if (checkBox.Checked) { menuStrip.Visible = false; checkBox.Text = ""メニュー表示""; } else { menuStrip.Visible = true; checkBox.Text = ""メニュー非表示""; } } private void ColorBlueMenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem item = (ToolStripMenuItem)sender; // チェック状態を反転させる item.Checked = !item.Checked; } private void ColorGreenMenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem item = (ToolStripMenuItem)sender; // チェック状態を反転させる item.Checked = !item.Checked; } private void ColorRedMenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem item = (ToolStripMenuItem)sender; // チェック状態を反転させる item.Checked = !item.Checked; } } } |
「設定」メニューが追加され、多階層のメニューになっていることが分かります。また、色のアイテムを選択すると、チェックが付けられます。
このように、C#ではメニューを右に追加したり、多階層にしたり、チェックを付けたりすることができます。
- SE
- メニューの追加や表示/非表示の切り替えまでできるのですね。'
- PM
- ショートカットキーの割り当てなど様々なコントロールが可能ですので、実際に書いて理解を深めていきましょう。
C#でメニューバーを作成しよう
いかがでしたか。C#では、ショートカットキーを割り当てたり、多階層のメニューにすることができます。また、メニューバー自体を非表示にすることもできます。
ここで紹介した方法以外にも様々なコントロールができますので、自分自身の手でC#のソースコードを書いて、理解を深めましょう。