本文实例讲述了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,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,true); SendMessage(aoc.Handle,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#程序设计有所帮助。
总结以上是内存溢出为你收集整理的C#实现listview Group收缩扩展的方法全部内容,希望文章能够帮你解决C#实现listview Group收缩扩展的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)