Listview大图标右键打开ContextMenuStrip

Listview Large Icon right click to open ContextMenuStrip

在我的项目中,我有一个 ListView,当我在大图标中单击右键时,我想打开我的 ContextMenuStrip。我尝试了很多事情,但我没有成功。当我右键单击 ListView 内部时,ContextMenuStrip 打开,但我想查看我右键单击大图标的时间。

我还需要帮助获取点击图标的名称(属性)。

这是一个快速而肮脏的解决方案;请比我付出更多的努力..

// a class level reference, prepare it where you want..
ContextMenuStrip ms = new ContextMenuStrip();

您应该为 MouseDownMouseUp 事件编写代码:

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    // disassociate from listview at first:
    listView1.ContextMenuStrip = null;

    // check for right button
    if (e.Button != System.Windows.Forms.MouseButtons.Right) return;

    // get item info:
    ListViewHitTestInfo hi = listView1.HitTest(e.Location);

    // no item hit:
    if (hi.Item == null) return;

    // calculate the image rectangle:

    // this contains the unscrolled y coordinate:
    Point iloc = listView1.GetItemRect(hi.Item.Index).Location;
    // we combine it with the x-position:
    Rectangle r = new Rectangle(new Point (hi.Item.Position.X,  iloc.Y),
                                imageList1.ImageSize);
    // no image hit:
    if ( !r.Contains(e.Location) ) return;

    // maybe prepare or change the menue now..
    // here I display the image name from the keys array:
    ms.Items[0].Text = imageList1.Images.Keys[hi.Item.ImageIndex];
    ms.Location = e.Location;

    // associate with listview and show
    listView1.ContextMenuStrip = ms;
    ms.Show();
}

能否请您尝试以下操作,看看它是否有效... private void listView1_MouseClick(object sender, MouseEventArgs e) {
如果 (e.Button == MouseButtons.Right) { 如果 (listView1.FocusedItem.Bounds.Contains(e.Location) == 真) { contextMenuStrip1.Show(Cursor.Position); } }
}

这应该有效

private void listView1_MouseClick(object sender, MouseEventArgs e) { ListView listView = sender as ListView; if (e.Button == System.Windows.Forms.MouseButtons.Right) { ListViewItem item = listView.GetItemAt(e.X, e.Y); if (item != null) { item.Selected = true; contextMenuStrip1.Show(listView , e.Location); } } }

在鼠标点击位置搜索列表视图项目。如果存在,请显示菜单.........