Standard Windows menu bars in Windows Forms

萝らか妹 提交于 2019-11-27 12:37:37

This is easy.

Goto your Toolbox, right click anywhere inside and select "Choose Items". When the dialog loads and appears, scroll down til you see MainMenu. Add that to the toolbox, and you've got yourself a native Menu Bar!

Happy coding!

You can do this by setting your form's Menu property, like this:

private void Form1_Load(object sender, EventArgs e)
{
    this.Menu = new MainMenu();
        MenuItem item = new MenuItem("File");
        this.Menu.MenuItems.Add(item);
            item.MenuItems.Add("Save", new EventHandler(Save_Click));
            item.MenuItems.Add("Open", new EventHandler(Open_Click)); 
        item = new MenuItem("Edit");
        this.Menu.MenuItems.Add(item);
            item.MenuItems.Add("Copy", new EventHandler(Copy_Click));
            item.MenuItems.Add("Paste", new EventHandler(Paste_Click)); 
        // etc ...
}

private void Save_Click(object sender, EventArgs e)
{
    // save
}

These menus will look like "normal" system menus.

I couldn't find any designer support for this, though. In my defense, I didn't try real hard.

Instead of using a the MainMenu component you can create your own renderer for the MenuStrip component. The advantage here is being able to add images to MenuStripItem objects. Here is the pastebin for the custom renderer:

NativeRenderer

There are different themes that can be applied in the constructor of the renderer. Try them all to see the native themes. To use this renderer simply set the instance to the MenuStrip Renderer property:

menuStrip.Renderer = new NativeRenderer([theme]);

I normally set the MenuStrip's RenderMode to System which gives a minimalist, single colour menu (no gradients or anything decadent like that).

If that does not go far enough, then you'll likely have to jump through some low-level hoops to get what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!