SoFunction
Updated on 2025-03-07

C# method to implement listview group shrinking and scaling

This article describes the method of C# to implement listview Group shrinking and scaling. Share it for your reference, as follows:

1. This example improves a limitation of the charju teacher "Add Group Collapse Behavior on a Listview Control" on codeprofect (clicking on the icon behind the grouping cannot shrink or expand);

2. This actual column applies to win2008, vista;

3. For reference only. If there is a better method, I hope everyone will communicate with you~

The complete code is as follows (just build a windows project, drag a listview control on the form, name it aoc, right-click to edit the code, and stick the following code to the window, but you need to pay attention to the event correspondence):

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace ListViewGroup
{
  public partial class MainForm : Form
  {
    public MainForm()
    {
      InitializeComponent();
    }
    [DllImport("")]
    static extern int SendMessage(IntPtr window, int message, int wParam, ref LVHITTESTINFO lParam);
    [DllImport("")]
    static extern int SendMessage(IntPtr window, int message, int wParam, IntPtr lParam);
    private void btCollapse_Click(object sender, EventArgs e)
    {
      SetGroupCollapse( | );
    }
    private void btExpand_Click(object sender, EventArgs e)
    {
      SetGroupCollapse( | );
    }
    private void SetGroupCollapse(GroupState state)
    {
      for (int i = 0; i <= ; i++)
      {
        LVGROUP group = new LVGROUP();
         = (group);
         = (int)state; // LVGS_COLLAPSIBLE 
         = 4; // LVGF_STATE 
         = i;
        IntPtr ip = ;
        try
        {
          ip = ();
          (group, ip, true);
          SendMessage(, 0x1000 + 147, i, ip); // #define LVM_SETGROUPINFO (LVM_FIRST + 147) 
        }
        catch (Exception ex)
        {
          ( +  + );
        }
        finally
        {
          if (null != ip) (ip);
        }
      }
    }
    private void MainForm_Load(object sender, EventArgs e)
    {
      SetGroupCollapse();
      for (int i = 0; i < ; i++)
      {
        [i].Tag = "EXPANDED";
      }
    }
    private void aoc_MouseDown(object sender, MouseEventArgs e)
    {
      LVHITTESTINFO lvHitInfo = new LVHITTESTINFO();
      Point p = new Point(, );
       = p;
      try
      {
        int id = SendMessage(, 0x1000 + 18, -1, ref lvHitInfo);
        if ( == 0x50000000)
        {
          if ([id].() =="EXPANDED")
          {
            SetGroupCollapseEx(id,  | );
            [id].Tag = "COLLAPSED";
          }
          else if ( [id].() == "COLLAPSED")
          {
            SetGroupCollapseEx(id,  | );
            [id].Tag = "EXPANDED";
          }
        }
        //(("RESULT={0}   FLAGS=0x{1:X}", id, ));
      }
      catch (Exception ex)
      {
        ( +  + );
      }
      finally
      {
        ;
      }
    }
    private void SetGroupCollapseEx(int id, GroupState groupState)
    {
      int i = id;
      LVGROUP group = new LVGROUP();
       = (group);
       = (int)groupState; // LVGS_COLLAPSIBLE 
       = 4; // LVGF_STATE 
       = i;
      IntPtr ip = ;
      try
      {
        ip = ();
        (group, ip, true);
        SendMessage(, 0x1000 + 147, i, ip); // #define LVM_SETGROUPINFO (LVM_FIRST + 147) 
      }
      catch (Exception ex)
      {
        ( +  + );
      }
      finally
      {
        if (null != ip) (ip);
      }
    }
  }
  [StructLayout()]
  public struct LVGROUP
  {
    public int cbSize;
    public int mask;
    [MarshalAs()]
    public string pszHeader;
    public int cchHeader;
    [MarshalAs()]
    public string pszFooter;
    public int cchFooter;
    public int iGroupId;
    public int stateMask;
    public int state;
    public int uAlign;
  }
  public enum GroupState
  {
    COLLAPSIBLE = 8,
    COLLAPSED = 1,
    EXPANDED = 0
  }
  [StructLayout()]
  public struct LVHITTESTINFO
  {
    public Point pt;
    public int flags;
    public int iItem;
    public int iSubItem;
    public int iGroup;
  }
}

For more information about C# related content, please check out the topic of this site:C# data structure and algorithm tutorial》、《Tutorial on the usage of common C# controls》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.