there is a big problem with the column width version
if you resize a unlocked column to the specified size (0), it will become locked
i use a ParamArray to specify the column numbers so i can use it on all my forms
Code:SubClassHwnd ListView1.hWnd, 7, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23optionaly you can move all the hidden column to the leftCode:Option Explicit Option Base 0 ' GetWindowsLong Constants Private Const GWL_WNDPROC = (-4) ' Windows Message Constants Private Const WM_NOTIFY = &H4E Private Const WM_DESTROY = &H2 ' Column Header Notification Meassage Constants Private Const HDN_FIRST = -300& Private Const HDN_DIVIDERDBLCLICKA = (HDN_FIRST - 5) Private Const HDN_BEGINTRACK = (HDN_FIRST - 6) ' Column Header Item Info Message Constants Private Const HDI_WIDTH = &H1 ' Notify Message Header Type Private Type NMHDR hWndFrom As Long idFrom As Long code As Long End Type ' Notify Message Header for Listview Private Type NMHEADER hdr As NMHDR iItem As Long iButton As Long lPtrHDItem As Long ' HDITEM FAR* pItem End Type ' Header Item Type Private Type HDITEM mask As Long cxy As Long pszText As Long hbm As Long cchTextMax As Long fmt As Long lParam As Long iImage As Long iOrder As Long End Type Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private 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 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Private mlPrevWndProc As Long, ColumnsBits As Long Private Function WindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim tNMH As NMHDR Dim tNMHEADER As NMHEADER Dim tITEM As HDITEM Select Case Msg Case WM_NOTIFY ' Copy the Notify Message Header to a Header Structure CopyMemory tNMH, ByVal lParam, Len(tNMH) Select Case tNMH.code Case HDN_DIVIDERDBLCLICKA ' If the user is trying to DOUBLECLICK a Column Header ' Extract Information about the Header being Sized CopyMemory tNMHEADER, ByVal lParam, Len(tNMHEADER) ' Don't allow Columns to be Sized. If (ColumnsBits And (2 ^ tNMHEADER.iItem)) = (2 ^ tNMHEADER.iItem) Then WindowProc = 1 Exit Function End If Case HDN_BEGINTRACK ' If the user is trying to Size a Column Header ' Extract Information about the Header being Sized CopyMemory tNMHEADER, ByVal lParam, Len(tNMHEADER) ' Don't allow Columns to be Sized. If (ColumnsBits And (2 ^ tNMHEADER.iItem)) = (2 ^ tNMHEADER.iItem) Then WindowProc = 1 Exit Function End If End Select Case WM_DESTROY ' Remove Subclassing when Listview is Destroyed (Form unloaded) WindowProc = CallWindowProc(mlPrevWndProc, hWnd, Msg, wParam, lParam) Call SetWindowLong(hWnd, GWL_WNDPROC, mlPrevWndProc) Exit Function End Select ' Call Default Window Handler WindowProc = CallWindowProc(mlPrevWndProc, hWnd, Msg, wParam, lParam) End Function Public Sub SubClassHwnd(ByVal hWnd As Long, ParamArray ColumnsNums()) Dim t As Integer ColumnsBits = 0 For t = 0 To UBound(ColumnsNums) ColumnsBits = ColumnsBits + (2 ^ (ColumnsNums(t) - 1)) Next t mlPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc) End Sub
Code:ListView1.ColumnHeaders(7).Position = 1




Reply With Quote