从 ToolStripMenuItem 打开 ContextMenu

发布于 2024-12-17 16:01:12 字数 371 浏览 6 评论 0原文

我试图模仿 Windows 资源管理器的行为以及收藏夹项目如何启动上下文菜单。

我目前正在使用:

contextMenu.Show((sender as ToolStripMenuItem).GetCurrentParent().PointToScreen(e.Location));

这发生在 ToolStripMenuItem 的 MouseDown 事件中。问题是右键单击后菜单立即关闭,并且我不知道在上下文菜单打开时有什么方法可以暂停它。

我尝试从 ToolStripMenuItem 派生并覆盖 MouseDown/MouseUp,但我不知道如何在单击时保持其打开状态。

有没有好的方法可以做到这一点?

I am trying to mimic the behavior of, for example, Windows Explorer and how the Favorites items can launch a context menu.

I currently am using:

contextMenu.Show((sender as ToolStripMenuItem).GetCurrentParent().PointToScreen(e.Location));

This occurs in the MouseDown event of the ToolStripMenuItem. The problem is that the menu closes immediately after right-click, and I don't know any way to suspend it while the context menu is open.

I've tried deriving from ToolStripMenuItem and overriding the MouseDown/MouseUp but I can't figure out how to keep it open on click.

Is there a good way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

旧城烟雨 2024-12-24 16:01:12

这是我很幸运的,更直接一点:

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    void MenuItemContext(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) return;

        ToolStripMenuItem mID = (ToolStripMenuItem)sender;

        ContextMenu tsmiContext = new ContextMenu();

        MenuItem Item1 = new MenuItem();
        MenuItem Item2 = new MenuItem();

        Item1.Text = "Item1";
        Item2.Text = "Item2";

        tsmiContext.MenuItems.Add(Item1);
        tsmiContext.MenuItems.Add(Item2);

        Item1.Click += new EventHandler(Item1_Click);
        Item2.Click += new EventHandler(Item2_Click);

        hndPass = mID.Text;

        tsmiContext.Show(menuStrip1, menuStrip1.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)));
    }

    private String hndPass;

    void Item1_Click(object sender, EventArgs e)
    {
       MenuItem mID = (MenuItem)sender;
        MessageBox.Show("You clicked " + mID.Text + " in the context menu of " + hndPass);
    }
    void Item2_Click(object sender, EventArgs e)
    {
        MenuItem mID = (MenuItem)sender;
        MessageBox.Show("You clicked " + mID.Text + " in the context menu of " + hndPass); ;
    }
}

This is what I've had luck with, it's a bit more direct:

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    void MenuItemContext(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) return;

        ToolStripMenuItem mID = (ToolStripMenuItem)sender;

        ContextMenu tsmiContext = new ContextMenu();

        MenuItem Item1 = new MenuItem();
        MenuItem Item2 = new MenuItem();

        Item1.Text = "Item1";
        Item2.Text = "Item2";

        tsmiContext.MenuItems.Add(Item1);
        tsmiContext.MenuItems.Add(Item2);

        Item1.Click += new EventHandler(Item1_Click);
        Item2.Click += new EventHandler(Item2_Click);

        hndPass = mID.Text;

        tsmiContext.Show(menuStrip1, menuStrip1.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)));
    }

    private String hndPass;

    void Item1_Click(object sender, EventArgs e)
    {
       MenuItem mID = (MenuItem)sender;
        MessageBox.Show("You clicked " + mID.Text + " in the context menu of " + hndPass);
    }
    void Item2_Click(object sender, EventArgs e)
    {
        MenuItem mID = (MenuItem)sender;
        MessageBox.Show("You clicked " + mID.Text + " in the context menu of " + hndPass); ;
    }
}
回梦 2024-12-24 16:01:12

实现此目的的一种方法是使用 ToolStripDropDown 控件在 ToolStripDropDown 内部托管一个 ListBox。

这可能需要对 AutoClose 行为进行一些调整,但它应该可以帮助您开始:

首先在主窗体中,将以下行添加到 ToolStripDropDropDown 项中,

toolStripDropDownButton1.DropDown = new CustomListDropDown();

然后创建一个自定义下拉类,如下所示

public class CustomListDropDown : ToolStripDropDown
{
    private ContextMenuStrip contextMenuStrip1;
    private ToolStripMenuItem toolStripMenuItem1;
    private ToolStripMenuItem toolStripMenuItem2;
    private ToolStripMenuItem toolStripMenuItem3;
    private System.ComponentModel.IContainer components;

    public ListBox ListBox { get; private set; }

    public CustomListDropDown()
    {
        InitializeComponent();

        this.ListBox = new ListBox() { Width = 200, Height = 600 };
        this.Items.Add(new ToolStripControlHost(this.ListBox));

        this.ListBox.ContextMenuStrip = contextMenuStrip1;
        this.ListBox.MouseDown += new MouseEventHandler(ListBox_MouseDown);

        contextMenuStrip1.Closing += new ToolStripDropDownClosingEventHandler(contextMenuStrip1_Closing);

        //add sample items
        this.ListBox.Items.Add("Item1");
        this.ListBox.Items.Add("Item2");
        this.ListBox.Items.Add("Item3");
        this.ListBox.Items.Add("Item4");
    }

    void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
    {
        this.Close();
        this.AutoClose = true;
    }

    void ListBox_MouseDown(object sender, MouseEventArgs e)
    {
        this.AutoClose = false;
        this.ListBox.SelectedIndex = this.ListBox.IndexFromPoint(e.Location);
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
        this.contextMenuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // contextMenuStrip1
        // 
        this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.toolStripMenuItem1,
        this.toolStripMenuItem2,
        this.toolStripMenuItem3});
        this.contextMenuStrip1.Name = "contextMenuStrip1";
        // 
        // contextMenuStrip1.ContextMenuStrip
        // 
        this.contextMenuStrip1.Size = new System.Drawing.Size(181, 48);
        // 
        // toolStripMenuItem1
        // 
        this.toolStripMenuItem1.Name = "toolStripMenuItem1";
        this.toolStripMenuItem1.Size = new System.Drawing.Size(180, 22);
        this.toolStripMenuItem1.Text = "toolStripMenuItem1";
        // 
        // toolStripMenuItem2
        // 
        this.toolStripMenuItem2.Name = "toolStripMenuItem2";
        this.toolStripMenuItem2.Size = new System.Drawing.Size(180, 22);
        this.toolStripMenuItem2.Text = "toolStripMenuItem2";
        // 
        // toolStripMenuItem3
        // 
        this.toolStripMenuItem3.Name = "toolStripMenuItem3";
        this.toolStripMenuItem3.Size = new System.Drawing.Size(180, 22);
        this.toolStripMenuItem3.Text = "toolStripMenuItem3";
        // 
        // CustomListDropDown
        // 
        this.Size = new System.Drawing.Size(2, 4);
        this.contextMenuStrip1.ResumeLayout(false);
        this.ResumeLayout(false);

    }
}

:测试这效果相当好。让我知道进展如何。

One way that you could accomplish this is by using the ToolStripDropDown control to host a ListBox inside of the ToolStripDropDown.

This may require some tweaking regarding the AutoClose behavior, but it should get you started:

First in your main form, add the following line to your ToolStripDropDropDown item

toolStripDropDownButton1.DropDown = new CustomListDropDown();

Then create a custom drop down class as follows:

public class CustomListDropDown : ToolStripDropDown
{
    private ContextMenuStrip contextMenuStrip1;
    private ToolStripMenuItem toolStripMenuItem1;
    private ToolStripMenuItem toolStripMenuItem2;
    private ToolStripMenuItem toolStripMenuItem3;
    private System.ComponentModel.IContainer components;

    public ListBox ListBox { get; private set; }

    public CustomListDropDown()
    {
        InitializeComponent();

        this.ListBox = new ListBox() { Width = 200, Height = 600 };
        this.Items.Add(new ToolStripControlHost(this.ListBox));

        this.ListBox.ContextMenuStrip = contextMenuStrip1;
        this.ListBox.MouseDown += new MouseEventHandler(ListBox_MouseDown);

        contextMenuStrip1.Closing += new ToolStripDropDownClosingEventHandler(contextMenuStrip1_Closing);

        //add sample items
        this.ListBox.Items.Add("Item1");
        this.ListBox.Items.Add("Item2");
        this.ListBox.Items.Add("Item3");
        this.ListBox.Items.Add("Item4");
    }

    void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
    {
        this.Close();
        this.AutoClose = true;
    }

    void ListBox_MouseDown(object sender, MouseEventArgs e)
    {
        this.AutoClose = false;
        this.ListBox.SelectedIndex = this.ListBox.IndexFromPoint(e.Location);
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
        this.contextMenuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // contextMenuStrip1
        // 
        this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.toolStripMenuItem1,
        this.toolStripMenuItem2,
        this.toolStripMenuItem3});
        this.contextMenuStrip1.Name = "contextMenuStrip1";
        // 
        // contextMenuStrip1.ContextMenuStrip
        // 
        this.contextMenuStrip1.Size = new System.Drawing.Size(181, 48);
        // 
        // toolStripMenuItem1
        // 
        this.toolStripMenuItem1.Name = "toolStripMenuItem1";
        this.toolStripMenuItem1.Size = new System.Drawing.Size(180, 22);
        this.toolStripMenuItem1.Text = "toolStripMenuItem1";
        // 
        // toolStripMenuItem2
        // 
        this.toolStripMenuItem2.Name = "toolStripMenuItem2";
        this.toolStripMenuItem2.Size = new System.Drawing.Size(180, 22);
        this.toolStripMenuItem2.Text = "toolStripMenuItem2";
        // 
        // toolStripMenuItem3
        // 
        this.toolStripMenuItem3.Name = "toolStripMenuItem3";
        this.toolStripMenuItem3.Size = new System.Drawing.Size(180, 22);
        this.toolStripMenuItem3.Text = "toolStripMenuItem3";
        // 
        // CustomListDropDown
        // 
        this.Size = new System.Drawing.Size(2, 4);
        this.contextMenuStrip1.ResumeLayout(false);
        this.ResumeLayout(false);

    }
}

In my tests this worked reasonably well. Let me know how it goes.

风筝有风,海豚有海 2024-12-24 16:01:12

由于 ContextMenuStrip 派生自 ToolStripDropDown,您可以这样做:

    private ContextMenuStrip CopyToContextMenu(ToolStripMenuItem mnuItemSource)
    {
        var mnuContextDestination =  new ContextMenuStrip();

        //Move itens from ToolStripMenuItem to ContextMenuStrip
        while (mnuItemSource.DropDown.Items.Count > 0)
        {
            mnuContextDestination.Items.Add(mnuItemSource.DropDown.Items[0]);
        }

        //Put ContextMenuStrip in ToolStripMenuItem using DropDown property
        mnuItemSource.DropDown = mnuContextDestination;

        return mnuContextDestination;
    }

As ContextMenuStrip is derived from ToolStripDropDown, you could do this:

    private ContextMenuStrip CopyToContextMenu(ToolStripMenuItem mnuItemSource)
    {
        var mnuContextDestination =  new ContextMenuStrip();

        //Move itens from ToolStripMenuItem to ContextMenuStrip
        while (mnuItemSource.DropDown.Items.Count > 0)
        {
            mnuContextDestination.Items.Add(mnuItemSource.DropDown.Items[0]);
        }

        //Put ContextMenuStrip in ToolStripMenuItem using DropDown property
        mnuItemSource.DropDown = mnuContextDestination;

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