右键单击Listview大图标以打开ContextMenuStrip [英] Listview Large Icon right click to open ContextMenuStrip
问题描述
在我的项目中,我有一个ListView
,当我单击大图标中的右键时,我想打开我的ContextMenuStrip
.我尝试了很多事情,但没有成功.在ListView
的内部单击鼠标右键时,会打开ContextMenuStrip
,但是我想在右键单击大图标时看到它.
In my project I have a ListView
and I would like to open my ContextMenuStrip
when I clicked right button in the large icon. I tried many things but I am unsuccessful. When I right click inside of ListView
the ContextMenuStrip
opens, but I want to see just when I right clicked the large icon.
我还需要帮助来获取单击的图标的名称(属性).
Also I need to help about get the clicked icon's name (properties).
推荐答案
这是一种快速而肮脏的解决方案;请比我投入更多的工作.
This is a quick and dirty solution; please do put more work into it than I did..
// a class level reference, prepare it where you want..
ContextMenuStrip ms = new ContextMenuStrip();
您应该编写MouseDown
或MouseUp
事件的代码:
You should either code the MouseDown
or the MouseUp
event:
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();
}
这篇关于右键单击Listview大图标以打开ContextMenuStrip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!