PDA

Click to See Complete Forum and Search --> : icon in the header of a listview when sorting


David Laplante
Nov 9th, 1999, 12:24 PM
Hi,

I managed to put an icon in the header of my listview (the little arrows that show if the sort is ascending or not) but it seem that in order to do that the list view MUST have icons in the list as well... I would like for my list NOT to have icons (just text) is it possible?

Just in case... here's my code

----------Module code------------
Option Explicit

Public Declare Function SendMessageLong Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Declare Function SendMessageAny Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) 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 dwNewLong As Long) As Long

Public Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal Y As Long, _
ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long) As Long


Public Const HDS_BUTTONS = &H2
Const SWP_DRAWFRAME = &H20
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
Const SWP_NOZORDER = &H4
Public Const SWP_FLAGS = SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME

Public Const GWL_STYLE = (-16)
Public Const LVM_FIRST = &H1000
Public Const LVM_GETHEADER = (LVM_FIRST + 31)

Public Const HDS_HOTTRACK = &H4

Public Const HDI_BITMAP = &H10
Public Const HDI_IMAGE = &H20
Public Const HDI_ORDER = &H80
Public Const HDI_FORMAT = &H4
Public Const HDI_TEXT = &H2
Public Const HDI_WIDTH = &H1
Public Const HDI_HEIGHT = HDI_WIDTH

Public Const HDF_LEFT = 0
Public Const HDF_RIGHT = 1
Public Const HDF_IMAGE = &H800
Public Const HDF_BITMAP_ON_RIGHT = &H1000
Public Const HDF_BITMAP = &H2000
Public Const HDF_STRING = &H4000

Public Const HDM_FIRST = &H1200
Public Const HDM_SETITEM = (HDM_FIRST + 4)

Public Type HD_ITEM
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

Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type


------- form load code-----
Option Explicit
Dim DB As Database
Dim rstSalutation, rstClient As Recordset
Private bState As Boolean
Private imgPosition As Long

Private Sub Form_Load()


Dim i As Integer
Dim itmX As ListItem
Dim imgX As ListImage

imgPosition = HDF_BITMAP_ON_RIGHT
'Create and populate the listview
With ListView1
.View = lvwReport
.Icons = ImageList1
.SmallIcons = ImageList1

.ColumnHeaders.Add , , "Appelation " & Space$(4)
.ColumnHeaders.Item(1).Width = 1520
.ColumnHeaders.Add , , "Prénom " & Space$(4)
.ColumnHeaders.Item(2).Width = 1520
.ColumnHeaders.Add , , "Nom " & Space$(4)
.ColumnHeaders.Item(3).Width = 1520

End With
Set DB = OpenDatabase("C:\WINDOWS\Desktop\Courtier97.mdb")
Set rstClient = DB.OpenRecordset("SELECT * FROM Client")
Set DataClient.Recordset = rstClient
Set rstSalutation = DB.OpenRecordset("SELECT Client_Salutation, Count(Client_No) AS CountOfClient_No " & _
"FROM Client " & _
"WHERE Client_No = " & DataClient.Recordset!Client_No & _
" GROUP BY Client_Salutation " & _
"ORDER BY Client_Salutation")
Set DataSalutation.Recordset = rstSalutation

Call proPopulerListe
'MakeFlat
End Sub

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ComctlLib.ColumnHeader)
Dim i As Long
Static sOrder

sOrder = Not sOrder

'Use default sorting to sort the items in the list
ListView1.SortKey = ColumnHeader.Index - 1
ListView1.SortOrder = Abs(sOrder)
ListView1.Sorted = True

'clear the image from the header not currently
'sorted, and update the header clicked
For i = 0 To 2

'if this is the index of the header clicked
If i = ListView1.SortKey Then

ShowHeaderIcon ListView1.SortKey, _
ListView1.SortOrder, _
imgPosition, _
HDF_IMAGE

Else: ShowHeaderIcon i, 0, 0, 0
End If

Next


'ListView1.SortOrder = IIf(ListView1.SortOrder = lvwAscending, lvwDescending, lvwAscending)
'ListView1.SortKey = ColumnHeader.SubItemIndex
End Sub

Private Sub proPopulerListe()
Dim Client As ListItem
ListView1.ListItems.Clear
If rstClient.RecordCount > 0 Then
rstClient.MoveFirst
Do While Not rstClient.EOF
Set Client = ListView1.ListItems.Add(, , IIf(IsNull(rstClient!Client_Salutation), "", rstClient!Client_Salutation))
Client.SubItems(1) = IIf(Not IsNull(rstClient!Client_Prenom), rstClient!Client_Prenom, "")
Client.SubItems(2) = IIf(Not IsNull(rstClient!Client_Nom), rstClient!Client_Nom, "")
Client.Tag = rstClient!Client_No
rstClient.MoveNext
Loop
End If
End Sub
Public Sub ShowHeaderIcon(colNo As Long, imgIconNo As Long, _
justify As Long, showImage As Long)

Dim r As Long
Dim hHeader As Long
Dim HD As HD_ITEM

'get a handle to the listview header component
hHeader = SendMessageLong(ListView1.hwnd, LVM_GETHEADER, 0, 0)

'set up the required structure members
With HD
.mask = HDI_IMAGE Or HDI_FORMAT
.fmt = HDF_LEFT Or HDF_STRING Or justify Or showImage
.pszText = ListView1.ColumnHeaders(colNo + 1).Text
If showImage Then .iImage = imgIconNo
End With

'modify the header
r = SendMessageAny(hHeader, HDM_SETITEM, colNo, HD)

End Sub

thanks in advance :)