Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

QuickQuery Half-Life Edition

Browsing QuickQuery HL Edition/LVHeaderSortIcons.cls (4.19 KB)

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "cLVHeaderSortIcons"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private m_ListView As ListView
Attribute m_ListView.VB_VarHelpID = -1
Private m_himl As Long
Public ExcludeColumn As Integer

Public Enum SortOrderConstants
    soAscending = 0
    soDescending = 1
End Enum

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const LVM_FIRST = &H1000
Private Const LVM_GETHEADER = (LVM_FIRST + 31)

Private Const HDM_FIRST = &H1200
Private Const HDM_SETITEM = (HDM_FIRST + 4)
Private Const HDM_SETIMAGELIST = (HDM_FIRST + 8)

Private Type HDITEM
    mask As Long
    cxy As Long
    pszText As String
    hbm As Long
    cchTextMax As Long
    fmt As Long
    lParam As Long
    iImage As Long
    iOrder As Long
End Type

Private Const HDI_FORMAT = &H4
Private Const HDI_IMAGE = &H20

Private Const HDF_LEFT = 0
Private Const HDF_RIGHT = 1
Private Const HDF_CENTER = 2
Private Const HDF_IMAGE = &H800
Private Const HDF_BITMAP_ON_RIGHT = &H1000
Private Const HDF_STRING = &H4000

Private Const ILC_MASK = &H1
Private Const ILC_COLOR8 = &H8

Private Declare Function ImageList_Create Lib "comctl32" (ByVal cx As Long, ByVal cy As Long, ByVal Flags As Long, ByVal cInitial As Long, ByVal cGrow As Long) As Long
Private Declare Function ImageList_Destroy Lib "comctl32" (ByVal himl As Long) As Boolean
Private Declare Function ImageList_ReplaceIcon Lib "comctl32" (ByVal himl As Long, ByVal i As Long, ByVal hIcon As Long) As Long
Private Sub Class_Initialize()
    m_himl = ImageList_Create(16, 16, ILC_MASK Or ILC_COLOR8, 2, 0)
    If m_himl Then
        ImageList_AddIcon m_himl, LoadResPicture(100, 1)
        ImageList_AddIcon m_himl, LoadResPicture(101, 1)
    End If
End Sub

Private Sub Class_Terminate()
    If m_himl Then ImageList_Destroy m_himl
End Sub

Public Property Get ListView() As ListView
    Set ListView = m_ListView
End Property

Public Property Set ListView(LV As Object)
    Set m_ListView = LV
End Property

Public Function SetHeaderIcons(iActiveColumn As Long, iSortOrder As SortOrderConstants) As Boolean
Static hwndHdr As Long
Dim i As Long
Dim fShow As Boolean
Dim fAlignRight As Boolean
Dim HDI As HDITEM
  
    If (m_himl = 0) Or (m_ListView Is Nothing) Then Exit Function
    If (m_ListView.View <> lvwReport) Then Exit Function
    
    If hwndHdr = 0 Then
        hwndHdr = ListView_GetHeader(m_ListView.hWnd)
        Header_SetImageList hwndHdr, m_himl
    End If
    
    If hwndHdr = 0 Then Exit Function
    
    With m_ListView.ColumnHeaders
        For i = 0 To .Count - 1
            HDI.mask = HDI_FORMAT Or HDI_IMAGE
            fAlignRight = .Item(i + 1).Alignment = lvwColumnRight
            HDI.fmt = HDF_STRING Or (fAlignRight And HDF_RIGHT)
            If (i = iActiveColumn) And (ExcludeColumn <> i + 1) Then
                HDI.fmt = HDI.fmt Or HDF_IMAGE Or ((fAlignRight = False) And HDF_BITMAP_ON_RIGHT)
            Else
                HDI.fmt = HDI.fmt Or Val(.Item(i + 1).Tag)
            End If
            HDI.iImage = Abs(CBool(iSortOrder))
            Header_SetItem hwndHdr, i, HDI
        Next i
    End With
    
    SetHeaderIcons = True
End Function

Private Function ListView_GetHeader(hWnd As Long) As Long
    ListView_GetHeader = SendMessage(hWnd, LVM_GETHEADER, 0, 0)
End Function

Private Function Header_SetItem(hwndHD As Long, i As Long, phdi As HDITEM) As Boolean
    Header_SetItem = SendMessage(hwndHD, HDM_SETITEM, i, phdi)
End Function
 
Private Function Header_SetImageList(hWnd As Long, himl As Long) As Long
    Header_SetImageList = SendMessage(hWnd, HDM_SETIMAGELIST, 0, ByVal himl)
End Function

Private Function ImageList_AddIcon(himl As Long, hIcon As Long) As Long
    ImageList_AddIcon = ImageList_ReplaceIcon(himl, -1, hIcon)
End Function

Download QuickQuery HL Edition/LVHeaderSortIcons.cls

Back to file list


Back to project page