时间:2021-05-20
本文实例讲述了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#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#使用listView增删操作的方法。分享给大家供大家参考。具体分析如下:应用场景:C#中使用listView控件,实现动态添加,选中删除等操作
本文实例讲述了C#实现ListView选中项向上或向下移动的方法。分享给大家供大家参考。具体实现方法如下:privatevoidbuttonUp_Click(o
本文实例讲述了C#获取运行平台系统信息的方法,主要可以实现C#获取系统启动经过的毫秒数,相连网络域名,系统启动经过的毫秒数等,并有关于ListView控件的相关
本文实例讲述了C#实现读取DataSet数据并显示在ListView控件中的方法。分享给大家供大家参考。具体如下:/*lvStudentList为ListVie
本文实例讲述了JS扩展方法实现技巧。分享给大家供大家参考。具体分析如下:JS扩展方法与C#的扩展方法非常相似,也是可以链式调用的,也是通过对某个类的扩展写法来实