C#实现listview Group收缩扩展的方法
本文实例讲述了C#实现listviewGroup收缩扩展的方法。分享给大家供大家参考,具体如下:
1、本实例是完善了codeprofect上面charju老师“AddGroupCollapseBehavioronaListviewControl”的一个限制(点击分组后面的图标不能收缩和扩展);
2、本实列适用于win2008,vista;
3、仅供参考,如有更好的方法,望大家不吝交流~
完整代码如下(只需建一个windows工程,在窗体上拖一个listview控件,取名为aoc,右击编辑代码,把下面的代码粘到窗口就可以了~,但需要注意事件对应):
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Runtime.InteropServices; usingSystem.Diagnostics; namespaceListViewGroup { publicpartialclassMainForm:Form { publicMainForm() { InitializeComponent(); } [DllImport("user32.dll")] staticexternintSendMessage(IntPtrwindow,intmessage,intwParam,refLVHITTESTINFOlParam); [DllImport("user32.dll")] staticexternintSendMessage(IntPtrwindow,intmessage,intwParam,IntPtrlParam); privatevoidbtCollapse_Click(objectsender,EventArgse) { SetGroupCollapse(GroupState.COLLAPSED|GroupState.COLLAPSIBLE); } privatevoidbtExpand_Click(objectsender,EventArgse) { SetGroupCollapse(GroupState.EXPANDED|GroupState.COLLAPSIBLE); } privatevoidSetGroupCollapse(GroupStatestate) { for(inti=0;i<=aoc.Groups.Count;i++) { LVGROUPgroup=newLVGROUP(); group.cbSize=Marshal.SizeOf(group); group.state=(int)state;//LVGS_COLLAPSIBLE group.mask=4;//LVGF_STATE group.iGroupId=i; IntPtrip=IntPtr.Zero; try { ip=Marshal.AllocHGlobal(group.cbSize); Marshal.StructureToPtr(group,ip,true); SendMessage(aoc.Handle,0x1000+147,i,ip);//#defineLVM_SETGROUPINFO(LVM_FIRST+147) } catch(Exceptionex) { System.Diagnostics.Trace.WriteLine(ex.Message+Environment.NewLine+ex.StackTrace); } finally { if(null!=ip)Marshal.FreeHGlobal(ip); } } } privatevoidMainForm_Load(objectsender,EventArgse) { SetGroupCollapse(GroupState.COLLAPSIBLE); for(inti=0;i<aoc.Groups.Count;i++) { aoc.Groups[i].Tag="EXPANDED"; } } privatevoidaoc_MouseDown(objectsender,MouseEventArgse) { LVHITTESTINFOlvHitInfo=newLVHITTESTINFO(); Pointp=newPoint(e.X,e.Y); lvHitInfo.pt=p; try { intid=SendMessage(aoc.Handle,0x1000+18,-1,reflvHitInfo); if(lvHitInfo.flags==0x50000000) { if(aoc.Groups[id].Tag.ToString()=="EXPANDED") { SetGroupCollapseEx(id,GroupState.COLLAPSED|GroupState.COLLAPSIBLE); aoc.Groups[id].Tag="COLLAPSED"; } elseif(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(Exceptionex) { Trace.WriteLine(ex.Message+Environment.NewLine+ex.StackTrace); } finally { ; } } privatevoidSetGroupCollapseEx(intid,GroupStategroupState) { inti=id; LVGROUPgroup=newLVGROUP(); group.cbSize=Marshal.SizeOf(group); group.state=(int)groupState;//LVGS_COLLAPSIBLE group.mask=4;//LVGF_STATE group.iGroupId=i; IntPtrip=IntPtr.Zero; try { ip=Marshal.AllocHGlobal(group.cbSize); Marshal.StructureToPtr(group,ip,true); SendMessage(aoc.Handle,0x1000+147,i,ip);//#defineLVM_SETGROUPINFO(LVM_FIRST+147) } catch(Exceptionex) { System.Diagnostics.Trace.WriteLine(ex.Message+Environment.NewLine+ex.StackTrace); } finally { if(null!=ip)Marshal.FreeHGlobal(ip); } } } [StructLayout(LayoutKind.Sequential)] publicstructLVGROUP { publicintcbSize; publicintmask; [MarshalAs(UnmanagedType.LPTStr)] publicstringpszHeader; publicintcchHeader; [MarshalAs(UnmanagedType.LPTStr)] publicstringpszFooter; publicintcchFooter; publicintiGroupId; publicintstateMask; publicintstate; publicintuAlign; } publicenumGroupState { COLLAPSIBLE=8, COLLAPSED=1, EXPANDED=0 } [StructLayout(LayoutKind.Sequential)] publicstructLVHITTESTINFO { publicPointpt; publicintflags; publicintiItem; publicintiSubItem; publicintiGroup; } }
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#数据结构与算法教程》、《C#常见控件用法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。