.net Windows 窗体中的标准 Windows 菜单栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2778109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Standard Windows menu bars in Windows Forms
提问by BoltClock
I noticed that adding a MenuStrip(from the Toolbox) to my form design doesn't yield a menu bar like the one seen in many native Windows applications. Instead I get a menu bar like Visual Studio's own. None of the style settings for MenuStripappear to mimic the much more common native menu bar.
我注意到MenuStrip在我的表单设计中添加一个(从工具箱中)不会产生像在许多本机 Windows 应用程序中看到的那样的菜单栏。相反,我得到了一个类似于 Visual Studio 自己的菜单栏。没有任何样式设置MenuStrip似乎模仿更常见的本机菜单栏。
Is there a way to add a menu bar to my Windows Forms application that looks the same as the one you see in Notepad, Task Manager and others? (Preferably with the designer, but I wouldn't mind adding it programmatically either.)
有没有办法将菜单栏添加到我的 Windows 窗体应用程序中,该菜单栏看起来与您在记事本、任务管理器和其他应用程序中看到的相同?(最好与设计师一起,但我也不介意以编程方式添加它。)
Screenshot for illustration:
截图说明:


回答by
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!
转到您的工具箱,右键单击其中的任意位置并选择“选择项目”。当对话框加载并出现时,向下滚动直到看到 MainMenu。将其添加到工具箱中,您就拥有了一个本机菜单栏!
Happy coding!
快乐编码!
回答by MusiGenesis
You can do this by setting your form's Menuproperty, like this:
您可以通过设置表单的Menu属性来做到这一点,如下所示:
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.
不过,我找不到任何设计师对此的支持。在我的辩护中,我并没有真正努力。
回答by Cris McLaughlin
Instead of using a the MainMenucomponent you can create your own renderer for the MenuStripcomponent. The advantage here is being able to add images to MenuStripItemobjects. Here is the pastebin for the custom renderer:
MainMenu您可以为组件创建自己的渲染器,而不是使用MenuStrip组件。这里的优点是能够向MenuStripItem对象添加图像。这是自定义渲染器的 pastebin:
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 MenuStripRenderer property:
可以在渲染器的构造函数中应用不同的主题。尝试所有这些以查看原生主题。要使用此渲染器,只需将实例设置为MenuStripRenderer 属性:
menuStrip.Renderer = new NativeRenderer([theme]);
回答by Charlie Salts
I normally set the MenuStrip's RenderModeto Systemwhich gives a minimalist, single colour menu (no gradients or anything decadent like that).
我通常设置的MenuStrip的RenderMode,以System赋予简约的单色菜单(无梯度或任何颓废类似)。
If that does not go far enough, then you'll likely have to jump through some low-level hoops to get what you want.
如果这还不够,那么您可能必须跳过一些低级的圈套才能得到您想要的东西。

