1. This instance is complete on codeprofectCharjuThe instructor's "add group Collapse Behavior on a listview control" restriction (clicking the icon next to the group cannot contract or expand );
2. This real column applies to win2008 and Vista;
3. It is for your reference only. If you have a better method, I hope you will continue to talk about it ~
The complete code is as follows (you only need to create a Windows project, drag a listview control on the form, name it AOC, right-click to edit the code, and paste the following code to the window ~, But pay attention to the corresponding event ):
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)
{
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;
[Financialas (unmanagedtype. lptstr)]
Public String pszheader;
Public int cchheader;
[Financialas (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;
}
}