Windows 窗体中的标准 Windows 菜单栏

发布于 2024-09-01 04:42:20 字数 343 浏览 3 评论 0原文

我注意到,将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

五里雾 2024-09-08 04:42:20

转到您的工具箱,右键单击其中的任意位置并选择“选择项目”。

当对话框加载并出现时,向下滚动直到看到主菜单。将其添加到工具箱中,您就拥有了一个本机菜单栏!

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!

泪冰清 2024-09-08 04:42:20

您可以通过设置表单的 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
}

这些菜单看起来像“正常”系统菜单。

但我找不到任何设计师对此的支持。在我的辩护中,我并没有真正努力。

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.

可爱咩 2024-09-08 04:42:20

您可以为 MenuStrip 组件创建自己的渲染器,而不是使用 MainMenu 组件。这里的优点是能够将图像添加到 MenuStripItem 对象。以下是自定义渲染器的 Pastebin:

NativeRenderer

可以在渲染器的构造函数中应用不同的主题。尝试所有这些以查看本机主题。要使用此渲染器,只需将实例设置为 MenuStrip Renderer 属性:

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

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]);
离线来电— 2024-09-08 04:42:20

我通常将 MenuStrip 的 RenderMode 设置为 System,这会提供简约的单色菜单(没有渐变或任何类似的颓废内容)。

如果这还不够,那么您可能需要跳过一些低级的障碍才能得到您想要的东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文