How to get the explorer right click menu in Winforms ListView?
I am using a Winforms ListView to show some files, but other than showing the files like explorer does, I want to have the same explorer开发者_如何学编程 right click menu when you get when you right click an item inside.
Is this possible? How do I enable it for my ListView app?
The only way I know of to do this is to use pinvoke and COM to do it. The unmanaged API you want, I think, is SHCreateDefaultContextMenu(). Once you get the interop done (check pinvoke.net first), you can do the interop for all the other things required by DEFCONTEXTMENU. It won't be easy. Welcome to the land of PIDLs.
I was actually having trouble getting a custom context menu to show with a right click: Erratic Behavior from ContextMenu
I still implemented slightly different:
private void lstModules_MouseDown(object sender , MouseEventArgs e)
{
hitTest = lstModules.HitTest(e.Location);
switch (e.Button)
{
case MouseButtons.Right:
if (hitTest != null && hitTest.Item != null)
{
// right clicking an item in the listview
selectedModule = hitTest.Item.Name;
lstModules.ContextMenuStrip = mnuContext_OptionsA;
}
else
{
// right clicking in white area of listview
lstModules.ContextMenuStrip = mnuContext_OptionsB;
}
break;
}
}
You would need the IContextMenu interface. Also see this very useful series.
精彩评论