LoginSignup
0
0

More than 3 years have passed since last update.

C# - メニューをつくろう。MenuStripのサンプル コードべた書き(Visual Studio不使用)

Last updated at Posted at 2020-10-23

目次: C# - Windows Formsでよく使うコントロールたち (Visual Studioなし環境向け) - Qiita

まえがき

MenuStripを使うサンプルソースはサクッと見つかったけど、重なり防止の部分のケアが欲しかったので、メモ代わりに置いておきます。

画面キャプチャ

image.png

image.png

ソースコード


using System;
using System.Drawing;
using System.Windows.Forms;

class MenuStripSample:Form
{
    MenuStripSample()
    {
        ClientSize = new Size(500, 300);


        var menuStrip = new MenuStrip();

        SuspendLayout();
        menuStrip.SuspendLayout();


        var menuItemFile       = new ToolStripMenuItem(){ Text = "ファイル(&F)"};
        var menuItemFileExport = new ToolStripMenuItem(){ Text = "エクスポート"};
        var menuItemEdit       = new ToolStripMenuItem(){ Text = "編集(&E)"};
        menuStrip.Items.Add(menuItemFile);
        menuStrip.Items.Add(menuItemEdit);

        menuItemFile.DropDownItems.Add( new ToolStripMenuItem("開く(&O)...", null, (s,e)=>{MessageBox.Show("ひらく!");}, Keys.Control | Keys.O) );
        menuItemFile.DropDownItems.Add( new ToolStripSeparator() );
        menuItemFile.DropDownItems.Add( new ToolStripMenuItem("保存(&S)", null, (s,e)=>{MessageBox.Show("ほぞん!");}, Keys.Control | Keys.S) );
        menuItemFile.DropDownItems.Add( new ToolStripSeparator() );
        menuItemFile.DropDownItems.Add( menuItemFileExport );

        menuItemFileExport.DropDownItems.Add(new ToolStripMenuItem("bmpファイルとしてエクスポート", null, (s,e)=>{MessageBox.Show("えくすぽーと その1");}, null) );
        menuItemFileExport.DropDownItems.Add(new ToolStripMenuItem("pngファイルとしてエクスポート", null, (s,e)=>{MessageBox.Show("えくすぽーと その2");}, null) );

        menuItemEdit.DropDownItems.Add( new ToolStripMenuItem("ほげほげ", null, (s,e)=>{MessageBox.Show("ほげ");}, null) );
        menuItemEdit.DropDownItems.Add( new ToolStripMenuItem("Foo Bar",  null, (s,e)=>{MessageBox.Show("Foo");}, null) );


        ///////
        // メニュー以外を作成しているコード部分
        var panel = new Panel(){Dock = DockStyle.Fill};
        Controls.Add(panel);

        var btn1 = new Button(){Location = new Point(0,0), Size = new Size(100,30), Text = "ボタン1"};
        var btn2 = new Button(){Dock = DockStyle.Bottom,   Height = 30,             Text = "ボタン2"};
        btn1.Click += (s,e)=>{MessageBox.Show("ボタン1が押されました");};
        btn2.Click += (s,e)=>{MessageBox.Show("ボタン2が押されました");};
        panel.Controls.Add(btn1);
        panel.Controls.Add(btn2);
        //
        ///////

        Controls.Add(menuStrip);   // 注意:panel より先に登録してしまうと、panelとmenuStripが重なって表示されてしまう。
        MainMenuStrip = menuStrip;

        menuStrip.ResumeLayout(false);
        menuStrip.PerformLayout();
        ResumeLayout(false);
        PerformLayout();
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new MenuStripSample());
    }
}

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0