田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 4 48
 123456
78910111213
14151617181920
21222324252627
282930


站点统计

最新评论



VB.net 动态控件组 ILSpy 是一个开源的.NET反编译工具,简洁强大易用是它的特征
未知 VB.net Listview group 分组折叠   [ 日期:2015-12-28 ]   [ 来自:本站原创 ]  HTML

Imports System.Runtime.InteropServices

Public Class Form1
    <Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _
    Public Structure LVGROUP
        Public cbSize As Integer
        Public mask As Integer
        <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.LPTStr)> _
        Public pszHeader As String
        Public cchHeader As Integer
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public pszFooter As String
        Public cchFooter As Integer
        Public iGroupId As Integer
        Public stateMask As Integer
        Public state As Integer
        Public uAlign As Integer
    End Structure

    Public Enum GroupState
        COLLAPSIBLE = 8
        COLLAPSED = 1
        EXPANDED = 0
    End Enum
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure LVHITTESTINFO
        Public pt As Point
        Public flags As Integer
        Public iItem As Integer
        Public iSubItem As Integer
        Public iGroup As Integer
    End Structure

    <Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal window As IntPtr, ByVal message As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function

    <Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal window As IntPtr, ByVal message As Integer, ByVal wParam As Integer, ByRef lParam As LVHITTESTINFO) As Integer
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Int16


        For j As Int16 = 0 To 5
            Me.ListView1.Groups.Add(j.ToString, j.ToString)
        Next

        For i = 0 To 9
            Me.ListView1.Items.Add(i.ToString)
            Me.ListView1.Items.Item(i).Group = Me.ListView1.Groups(0)
        Next
        For i = 10 To 19
            Me.ListView1.Items.Add(i.ToString)
            Me.ListView1.Items.Item(i).Group = Me.ListView1.Groups(1)
        Next
        For i = 20 To 29
            Me.ListView1.Items.Add(i.ToString)
            Me.ListView1.Items.Item(i).Group = Me.ListView1.Groups(3)
        Next
        setgroupCollapse(GroupState.COLLAPSIBLE)
    End Sub

    Private Sub setgroupCollapse(ByVal State As GroupState)
        For i As Int16 = 0 To Me.ListView1.Groups.Count
            Dim G As LVGROUP = New LVGROUP
            G.cbSize = Marshal.SizeOf(G)
            G.state = State
            G.mask = 4
            G.iGroupId = i
            Dim IP As IntPtr = IntPtr.Zero
            Try
                IP = Marshal.AllocHGlobal(G.cbSize)
                Marshal.StructureToPtr(G, IP, True)
                SendMessage(ListView1.Handle, &H1000 + 147, i, IP)
            Catch ex As Exception
                System.Diagnostics.Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace)
            Finally
                If IP <> Nothing Then
                    Marshal.FreeHGlobal(IP)
                End If
            End Try
        Next
    End Sub
    Private Sub SetGroupCollapseEx(ByVal id As Integer, ByVal groupState As GroupState)
        Dim i As Integer = id
        Dim group As New LVGROUP()
        group.cbSize = Marshal.SizeOf(group)
        group.state = CType(groupState, Integer)
        ' LVGS_COLLAPSIBLE 
        group.mask = 4
        ' LVGF_STATE 
        group.iGroupId = i
        Dim ip As IntPtr = IntPtr.Zero
        Try
            ip = Marshal.AllocHGlobal(group.cbSize)
            Marshal.StructureToPtr(group, ip, True)
            ' #define LVM_SETGROUPINFO (LVM_FIRST + 147) 
            SendMessage(ListView1.Handle, &H1000 + 147, i, ip)

        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace)
        Finally
            If Nothing <> ip Then
                Marshal.FreeHGlobal(ip)
            End If
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        setgroupCollapse(GroupState.COLLAPSED)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        setgroupCollapse(GroupState.COLLAPSIBLE)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        setgroupCollapse(GroupState.EXPANDED)
    End Sub

 
    Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
        Dim lvHitInfo As New LVHITTESTINFO()
        Dim p As New Point(e.X, e.Y)
        lvHitInfo.pt = p
        Try
            Dim id As Integer = SendMessage(ListView1.Handle, &H1000 + 18, -1, lvHitInfo)
            If lvHitInfo.flags = &H50000000 Then
                If ListView1.Groups(id).Tag.ToString() = "EXPANDED" Then
                    SetGroupCollapseEx(id, GroupState.COLLAPSED Or GroupState.COLLAPSIBLE)
                    ListView1.Groups(id).Tag = "COLLAPSED"
                ElseIf ListView1.Groups(id).Tag.ToString() = "COLLAPSED" Then
                    SetGroupCollapseEx(id, GroupState.EXPANDED Or GroupState.COLLAPSIBLE)
                    ListView1.Groups(id).Tag = "EXPANDED"
                End If
                MessageBox.Show(String.Format("RESULT={0}     FLAGS=0x{1:X}", id, lvHitInfo.flags))
            End If

        Catch ex As Exception
            Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace)
        Finally


        End Try
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        SetGroupCollapseEx(0, GroupState.COLLAPSED Or GroupState.COLLAPSIBLE)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        SetGroupCollapseEx(0, GroupState.EXPANDED Or GroupState.COLLAPSIBLE)
    End Sub
End Class




暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:1*2=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©