欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现listview Group收缩扩展的方法

程序员文章站 2022-11-24 07:55:57
本文实例讲述了c#实现listview group收缩扩展的方法。分享给大家供大家参考,具体如下: 1、本实例是完善了codeprofect上面charju老师“add...

本文实例讲述了c#实现listview group收缩扩展的方法。分享给大家供大家参考,具体如下:

1、本实例是完善了codeprofect上面charju老师“add group collapse behavior on a listview control”的一个限制(点击分组后面的图标不能收缩和扩展);

2、本实列适用于win2008,vista;

3、仅供参考,如有更好的方法,望大家不吝交流~

完整代码如下(只需建一个windows工程,在窗体上拖一个listview控件,取名为aoc,右击编辑代码,把下面的代码粘到窗口就可以了~,但需要注意事件对应):

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;
using system.diagnostics;
namespace listviewgroup
{
  public partial class mainform : form
  {
    public mainform()
    {
      initializecomponent();
    }
    [dllimport("user32.dll")]
    static extern int sendmessage(intptr window, int message, int wparam, ref lvhittestinfo lparam);
    [dllimport("user32.dll")]
    static extern int sendmessage(intptr window, int message, int wparam, intptr lparam);
    private void btcollapse_click(object sender, eventargs e)
    {
      setgroupcollapse(groupstate.collapsed | groupstate.collapsible);
    }
    private void btexpand_click(object sender, eventargs e)
    {
      setgroupcollapse(groupstate.expanded | groupstate.collapsible);
    }
    private void setgroupcollapse(groupstate state)
    {
      for (int i = 0; i <= aoc.groups.count; i++)
      {
        lvgroup group = new lvgroup();
        group.cbsize = marshal.sizeof(group);
        group.state = (int)state; // lvgs_collapsible 
        group.mask = 4; // lvgf_state 
        group.igroupid = i;
        intptr ip = intptr.zero;
        try
        {
          ip = marshal.allochglobal(group.cbsize);
          marshal.structuretoptr(group, ip, true);
          sendmessage(aoc.handle, 0x1000 + 147, i, ip); // #define lvm_setgroupinfo (lvm_first + 147) 
        }
        catch (exception ex)
        {
          system.diagnostics.trace.writeline(ex.message + environment.newline + ex.stacktrace);
        }
        finally
        {
          if (null != ip) marshal.freehglobal(ip);
        }
      }
    }
    private void mainform_load(object sender, eventargs e)
    {
      setgroupcollapse(groupstate.collapsible);
      for (int i = 0; i < aoc.groups.count; i++)
      {
        aoc.groups[i].tag = "expanded";
      }
    }
    private void aoc_mousedown(object sender, mouseeventargs e)
    {
      lvhittestinfo lvhitinfo = new lvhittestinfo();
      point p = new point(e.x, e.y);
      lvhitinfo.pt = p;
      try
      {
        int id = sendmessage(aoc.handle, 0x1000 + 18, -1, ref lvhitinfo);
        if (lvhitinfo.flags == 0x50000000)
        {
          if (aoc.groups[id].tag.tostring() =="expanded")
          {
            setgroupcollapseex(id, groupstate.collapsed | groupstate.collapsible);
            aoc.groups[id].tag = "collapsed";
          }
          else if ( aoc.groups[id].tag.tostring() == "collapsed")
          {
            setgroupcollapseex(id, groupstate.expanded | groupstate.collapsible);
            aoc.groups[id].tag = "expanded";
          }
        }
        //messagebox.show(string.format("result={0}   flags=0x{1:x}", id, lvhitinfo.flags));
      }
      catch (exception ex)
      {
        trace.writeline(ex.message + environment.newline + ex.stacktrace);
      }
      finally
      {
        ;
      }
    }
    private void setgroupcollapseex(int id, groupstate groupstate)
    {
      int i = id;
      lvgroup group = new lvgroup();
      group.cbsize = marshal.sizeof(group);
      group.state = (int)groupstate; // lvgs_collapsible 
      group.mask = 4; // lvgf_state 
      group.igroupid = i;
      intptr ip = intptr.zero;
      try
      {
        ip = marshal.allochglobal(group.cbsize);
        marshal.structuretoptr(group, ip, true);
        sendmessage(aoc.handle, 0x1000 + 147, i, ip); // #define lvm_setgroupinfo (lvm_first + 147) 
      }
      catch (exception ex)
      {
        system.diagnostics.trace.writeline(ex.message + environment.newline + ex.stacktrace);
      }
      finally
      {
        if (null != ip) marshal.freehglobal(ip);
      }
    }
  }
  [structlayout(layoutkind.sequential)]
  public 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;
  }
  public enum groupstate
  {
    collapsible = 8,
    collapsed = 1,
    expanded = 0
  }
  [structlayout(layoutkind.sequential)]
  public struct lvhittestinfo
  {
    public point pt;
    public int flags;
    public int iitem;
    public int isubitem;
    public int igroup;
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#数据结构与算法教程》、《c#常见控件用法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。