Windows 窗体中的标准 Windows 菜单栏
我注意到,将 MenuStrip
(从工具箱)添加到我的表单设计中不会产生像许多本机 Windows 应用程序中那样的菜单栏。相反,我得到了一个像 Visual Studio 自己的菜单栏。 MenuStrip
的样式设置似乎都没有模仿更常见的本机菜单栏。
有没有一种方法可以向我的 Windows 窗体应用程序添加一个菜单栏,该菜单栏看起来与您在记事本、任务管理器等中看到的菜单栏相同? (最好是与设计器一起使用,但我也不介意以编程方式添加它。)
用于说明的屏幕截图:
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 MenuStrip
appear to mimic the much more common native menu bar.
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.)
Screenshot for illustration:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
转到您的工具箱,右键单击其中的任意位置并选择“选择项目”。
当对话框加载并出现时,向下滚动直到看到主菜单。将其添加到工具箱中,您就拥有了一个本机菜单栏!
Go to 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!
您可以通过设置表单的
Menu
属性来完成此操作,如下所示:这些菜单看起来像“正常”系统菜单。
但我找不到任何设计师对此的支持。在我的辩护中,我并没有真正努力。
You can do this by setting your form's
Menu
property, like this: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.
您可以为
MenuStrip
组件创建自己的渲染器,而不是使用MainMenu
组件。这里的优点是能够将图像添加到MenuStripItem
对象。以下是自定义渲染器的 Pastebin:NativeRenderer
可以在渲染器的构造函数中应用不同的主题。尝试所有这些以查看本机主题。要使用此渲染器,只需将实例设置为
MenuStrip
Renderer 属性:Instead of using a the
MainMenu
component you can create your own renderer for theMenuStrip
component. The advantage here is being able to add images toMenuStripItem
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 的
RenderMode
设置为System
,这会提供简约的单色菜单(没有渐变或任何类似的颓废内容)。如果这还不够,那么您可能需要跳过一些低级的障碍才能得到您想要的东西。
I normally set the MenuStrip's
RenderMode
toSystem
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.