'窗体上一个listview,两个按钮:
Option Explicit
Dim HideFlag As Boolean, OldWidth As Single
Private Sub Command1_Click()
HideFlag = True
OldWidth = ListView1.ColumnHeaders(2).Width
ListView1.ColumnHeaders(2).Width = 0
End Sub
Private Sub Command2_Click()
HideFlag = False
ListView1.ColumnHeaders(2).Width = OldWidth
End Sub
Private Sub Form_Load()
Command1.Caption = "隐藏第2列"
Command2.Caption = "显示第2列"
ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "第1列"
ListView1.ColumnHeaders.Add , , "第2列"
ListView1.ColumnHeaders.Add , , "第3列"
With ListView1.ListItems.Add(, , "d")
.ListSubItems.Add , , "d1"
.ListSubItems.Add , , "d2"
End With
With ListView1.ListItems.Add(, , "e")
.ListSubItems.Add , , "e1"
.ListSubItems.Add , , "e2"
End With
With ListView1.ListItems.Add(, , "f")
.ListSubItems.Add , , "f1"
.ListSubItems.Add , , "f2"
End With
Call RegisterListView(ListView1)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call UnregisterListView(ListView1)
End Sub
Public Function ListView1_HeaderEvent(ByVal Action As lvHeaderActions, ByVal Column As Long) As Boolean
If Column = 2 And HideFlag = True Then
ListView1_HeaderEvent = True
End If
End Function
'模块代码:
Option Explicit
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Const GWL_WNDPROC As Long = -4
Public Const GWL_STYLE As Long = -16
Public Const WM_NOTIFY As Long = &H4E&
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal wNewWord As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Any, lParam As Any) As Long
Public Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hWnd As Long, ByVal lpString As String) As Long
Public Declare Function SetProp Lib "user32" Alias "SetPropA" (ByVal hWnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
Private Const HDN_FIRST As Long = -300&
Private Const HDN_ITEMCLICK As Long = (HDN_FIRST - 2)
Private Const HDN_DIVIDERDBLCLICK As Long = (HDN_FIRST - 5)
Private Const HDN_BEGINTRACK As Long = (HDN_FIRST - 6)
Private Const HDN_ENDTRACK As Long = (HDN_FIRST - 7)
Private Const HDN_TRACK As Long = (HDN_FIRST - 8)
Private Const HDN_GETDISPINFO As Long = (HDN_FIRST - 9)
Private Const HDN_BEGINDRAG As Long = (HDN_FIRST - 10)
Private Const HDN_ENDDRAG As Long = (HDN_FIRST - 11)
Private Const HDN_ITEMCHANGING As Long = (HDN_FIRST - 0)
Private Const HDN_ITEMCHANGED As Long = (HDN_FIRST - 1)
Private Const HDN_ITEMDBLCLICK As Long = (HDN_FIRST - 3)
Private Const HDN_NM_RCLICK As Long = -5
Public Function HandleListViewHeaderMsgs(ByVal ListViewhWnd As Long, ByVal msg As Long, ByVal wp As Long, ByVal lp As Long) As Long
Const EVENT_SUFFIX As String = "_HeaderEvent"
Dim ListViewControl As ListView
Dim NmHdrMsg As NMHDR
Dim PointStruct As POINTAPI
Dim HitTestInfo As HD_HITTESTINFO
Dim HeaderhWnd As Long
Dim HeaderAction As lvHeaderActions
Dim CancelMsg As Boolean
Select Case NmHdrMsg.code
Case HDN_ITEMCLICK: HeaderAction = lvHeaderActionClick
Case HDN_NM_RCLICK: HeaderAction = lvHeaderActionRightClick
Case HDN_DIVIDERDBLCLICK: HeaderAction = lvHeaderActionDividerDoubleClick
Case HDN_BEGINTRACK: HeaderAction = lvHeaderActionResizeBegin
Case HDN_ENDTRACK: HeaderAction = lvHeaderActionResizeEnd
Case HDN_ITEMCHANGING: HeaderAction = lvHeaderActionChanging
Case HDN_ITEMCHANGED: HeaderAction = lvHeaderActionChanged
Case HDN_BEGINDRAG: HeaderAction = lvHeaderActionDragBegin
Case HDN_ENDDRAG: HeaderAction = lvHeaderActionDragEnd
End Select
If HeaderAction <> 0 Then
On Error Resume Next
Set ListViewControl = RegisteredListViewControls(CStr(ListViewhWnd))
Public Sub ListViewHeaderEventDebugPrint(ByVal ListViewControl As ListView, ByVal Action As lvHeaderActions, ByVal Column As Long)
Dim msg As String
Select Case Action
Case lvHeaderActionClick: msg = "clicked"
Case lvHeaderActionRightClick: msg = "right-clicked"
Case lvHeaderActionDividerDoubleClick: msg = "divider dbl-clicked"
Case lvHeaderActionResizeBegin: msg = "resize begin"
Case lvHeaderActionResizeEnd: msg = "resize end"
Case lvHeaderActionChanging: msg = "changing"
Case lvHeaderActionChanged: msg = "changed"
Case lvHeaderActionDragBegin: msg = "drag begin"
Case lvHeaderActionDragEnd: msg = "drag end"
End Select