Change color of listviewheader for a specific column

Started by Rainer Wiedemann, September 20, 2008, 02:40:01 PM

Previous topic - Next topic

Rainer Wiedemann

Hi,
is it possible to change the textcolor of the header of one colomn. I want to show whitch colomn sorts the list in the moment. I succeded in coloring special rows, but not the header.
Thanks for help.
Rainer

TechSupport

Hi Rainer,

I have an example where I subclass the SysHeader child control in the ListView. The sample prevents column resizing but I should be able to modify it to handle the custom drawing. I'll post it soon.

TechSupport

Don't need to subclass the header in this case. You need to handle the NM_CUSTOMDRAW notification message that is sent from the Header to the ListView. Because the ListView is already subclassed, this makes it pretty easy to deal with the message. The sample below displays the column headers in different colors.




'------------------------------------------------------------------------------------------------------------------------
Function FORM1_WM_CREATE ( _
                         hWndForm As Dword, _  ' handle of Form
                         ByVal UserData As Long _  'optional user defined Long value
                         ) As Long

   FF_ListView_InsertColumn HWND_FORM1_LISTVIEW1, 0, "Column1", 0, 100
   FF_ListView_InsertColumn HWND_FORM1_LISTVIEW1, 1, "Column2", 0, 100
   FF_ListView_InsertColumn HWND_FORM1_LISTVIEW1, 2, "Column3", %LVCFMT_RIGHT, 100
     
     
   For row& = 0 To 10
      For col& = 0 To 2
          FF_ListView_InsertItem HWND_FORM1_LISTVIEW1, row&, col&, Str$(row&) & Str$(col&), 0, 0
      Next
   Next
   
End Function




'------------------------------------------------------------------------------------------------------------------------
Function FORM1_LISTVIEW1_CUSTOM ( _
                                ControlIndex  As Long,  _  ' index in Control Array
                                hWndForm      As Dword, _  ' handle of Form
                                hWndControl   As Dword, _  ' handle of Control
                                wMsg          As Long,  _  ' type of message
                                wParam        As Dword, _  ' first message parameter
                                lParam        As Long   _  ' second message parameter
                                ) As Long

   Local lpLvNm     As NM_LISTVIEW Ptr
   Local lpLvCd     As NMLVCUSTOMDRAW Ptr
   Local hdi        As HD_ITEM
   Local hSysHeader As Dword

   ' Handle the custom painting
   Select Case wMsg
      Case %WM_NOTIFY
         hSysHeader = SendMessage( HWND_FORM1_LISTVIEW1, %LVM_GETHEADER, 0, 0 )
         lpLvNm     = lParam
         Select Case @lpLvNm.hdr.hWndFrom
            Case hSysHeader
               Select Case @lpLvNm.hdr.Code
                   Case %NM_CUSTOMDRAW
                     lpLvCd = lParam
                     'check the drawing stage
                     Select Case @lpLvCd.nmcd.dwDrawStage

                         'prior to painting
                         Case %CDDS_PREPAINT
                            'tell Windows we want individual
                            'notification of each item being drawn
                            Function = %CDRF_NOTIFYITEMDRAW :Exit Function

                         'notification of each item being drawn
                         Case %CDDS_ITEMPREPAINT

                             SetBkMode @lpLvCd.nmcd.hdc, %TRANSPARENT
                             SetTextColor @lpLvCd.nmcd.hdc, RGB(255,0,0)   ' red

                            '--Can also select which column and color...
                             SELECT CASE ( @lpLvCd.nmcd.dwItemSpec )
                                 CASE 0 : SetTextColor @lpLvCd.nmcd.hdc, RGB(0,0,255)
                                 CASE 1 : SetTextColor @lpLvCd.nmcd.hdc, RGB(255,0,0)
                                 CASE 2 : SetTextColor @lpLvCd.nmcd.hdc, RGB(188,143,143)
                             END SELECT

                             'tell Windows the item has already been drawn
                             Function = %CDRF_NEWFONT: Exit Function
                             
                        End Select

               End Select

         End Select
   
   End Select
   
End Function




Rainer Wiedemann

Thank you very much Paul, for the quick answer. I tried this allready but I failed in getting the right handle of the header! Now the routine is used, but unfortunately the color doesn't change! Don't know why?

TechSupport

Have you tried creating a sample project and paste the above code in it? The sample works perfectly for me. The text color in the headers shows a different color for each of the three columns.

Are you sure that you are putting the code in the custom message handler of the ListView rather than the Form? Not sure why it wouldn't work for you.


Rainer Wiedemann

Hi Paul,
yes, I put it in the ListView Custom Handler and yes I made a simple Project with only form1 and listview1 and the only code that I added was a copy of your code. It doesn't work. I tried the exe on several pc with several OS - nothing. Dpn't know what I'm doing wrong...
I generated a single file of the FF-project and append it (Test.bas) - perhaps some other options are wrong?
(I use WinXP SP2, FF 2.91, PB 8.04 or 9.0).
Regards Rainer

TechSupport

Hi Rainer,

I downloaded your test.bas code and immediately compiled it and ran it (no other changes whatsoever). I have attached a screenshot of the resulting window. It shows the different color text in the header. I don't know why you are not seeing the same result.

Rainer Wiedemann

Okay Paul, thanks for the screenshot. You have Vista, now I tried my exe on a vista-pc and: it works!!! Is it possible, that with XP odr Win2k this doesn't work or I have to do it other?

David Kenny

Rainer,

I have XP SP2 and except for the vista theme, mine looks like Paul's.  Colors are present in the listview column headers.

David

Rainer Wiedemann

Hi David,
thanks for testing - but it's not working for me on XP-PCs. The same exe on Vista or W2k works.
Mystic... or windows!
Rainer