我可以创建.NET Windows窗体多列的上下文菜单?上下文、窗体、菜单、NET

2023-09-03 01:37:51 作者:国服熬夜冠军

我想创建一个具有几列的上下文菜单。基本上,它会是这样的:

I want to create a context menu that has several columns. Basically it would go like this:

First item  | [common option] | All Options >
Second item | [common option] | All Options >
Third item  | [common option] | All Options >
Fourth item | [common option] | All Options >

因此​​,基本上有一堆物品(在运行时产生的),每个项目可以自行启动;或与常用的选择;或者你可以得到所有可能的选项的子菜单。

So basically there are a bunch of items (generated at runtime), each item can be launched on its own; or with a commonly used option; or you can get a submenu with all possible options.

我怎样才能做到这一点?我试图滥用这两种的ContextMenuStrip 文本菜单,但他们似乎没有任何这样的选项。不过我似乎记得有某个地方看到多列菜单...

How can I do this? I'm trying to abuse both ContextMenuStrip and ContextMenu, yet they don't seem to have any such options. Still I seem to recall having seen multi-column menus somewhere...

我preFER Windows窗体的解决方案,因为我没有任何的WPF经验。哦,这个上下文菜单将在通知区域(也称为系统托盘)图标时,点击打开。

I'd prefer a Windows Forms solution, because I don't have any WPF experience. Oh, and this context menu will open when clicking on an icon in the Notification Area (aka systray).

推荐答案

我不知道的ContextMenuStrip ,这完全是在.NET $ C $内置菜单C,但你绝对可以用文本菜单做到这一点,这是一个包装了原生系统菜单。

I don't know about ContextMenuStrip, which is a menu built entirely in .NET code, but you can definitely do this with ContextMenu, which is a wrapper over the native system menus.

关键是设置 MFT_MENUBREAK MFT_MENUBARBREAK 标记为单独的菜单项(S),其暴露如 菜单项类包装:的 MenuItem.Break 和的 MenuItem.BarBreak ,分别为。

The key is setting the MFT_MENUBREAK or MFT_MENUBARBREAK flags for the individual menu item(s), which are exposed as properties in the MenuItem class wrapper: MenuItem.Break and MenuItem.BarBreak, respectively.

前者只是放置在新的一列的菜单项,而后者的地方的项目到一个新的柱,并且被蚀刻的垂直线分开的列。

The former just places the menu item in a new column, while the latter places the item into a new column and separates the column with an etched vertical line.

从MSDN的例子:

public void CreateMyMenus()
{
    // Create three top-level menu items.
    MenuItem menuItem1 = new MenuItem("&File");
    MenuItem menuItem2 = new MenuItem("&New");
    MenuItem menuItem3 = new MenuItem("&Open");

    // Set the BarBreak property to display horizontally.
    menuItem2.BarBreak = true;
    menuItem3.BarBreak = true;

    // Add menuItem2 and menuItem3 to the menuItem1's list of menu items.
    menuItem1.MenuItems.Add(menuItem2);
    menuItem1.MenuItems.Add(menuItem3);
}