Introduction
This is a short tip for a collapsible ListView
.
Background
For some reasons, I had to create a Windows Form application and I needed to have a ListView
control on it. Then I decided to make its groups collapsible and I found that although there are several pieces around the web, no one had put them into a class that a DEV can just put to use with no hooks and tricks around.
Using the Code
Without any further ado, here is the code. Just add it to your project and replace your existing ListView
control references with this and all should work as before with collapsible groups.
public class CollapsibleListView : ListView
{
private const int LVGF_STATE = 0x00000004;
private const int LVGS_COLLAPSIBLE = 0x00000008;
private const int LVM_FIRST = 0x1000;
private const int LVM_SETGROUPINFO = (LVM_FIRST + 147);
private const int LVM_INSERTGROUP = (LVM_FIRST + 145);
private const int WM_LBUTTONUP = 0x0202;
protected override void WndProc(ref Message message)
{
switch (message.Msg)
{
case WM_LBUTTONUP:
base.DefWndProc(ref message);
break;
case LVM_INSERTGROUP:
base.WndProc(ref message);
var group = Marshal.PtrToStructure<LvGroup>(message.LParam);
SetGroupCollapsible(group.iGroupId);
return;
}
base.WndProc(ref message);
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr handle, int message, int wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
private struct LvGroup
{
public int cbSize;
public int mask;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszHeader;
public int cchHeader;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszFooter;
public int cchFooter;
public int iGroupId;
public int stateMask;
public int state;
public int uAlign;
}
private void SetGroupCollapsible(int groupItemIndex)
{
LvGroup group = new LvGroup();
group.cbSize = Marshal.SizeOf(group);
group.state = LVGS_COLLAPSIBLE;
group.mask = LVGF_STATE;
group.iGroupId = groupItemIndex;
IntPtr ip = IntPtr.Zero;
try
{
ip = Marshal.AllocHGlobal(group.cbSize);
Marshal.StructureToPtr(group, ip, false);
SendMessage(Handle, LVM_SETGROUPINFO, groupItemIndex, ip);
}
finally
{
if (ip != IntPtr.Zero)
{
Marshal.FreeHGlobal(ip);
}
}
}
}