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
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |