Results 1 to 8 of 8

Thread: Listview Row Color

  1. #1

    Thread Starter
    Member chint's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    37

    Listview Row Color

    is it possible to have different list view color, preferably the whole row if not then just one item of it.
    im using the v5 of the controls.


    regards,
    abhi

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Listview Row Color

    Possible? Yes.

    You can subclass the listview and set fore/back colors. One example found here. More examples exist & recommend experimenting.

    You can use Krool's common-controls-replacement (CCR) project. I believe his version of the listview (themable), has a backcolor property.

    If an option for you, some people recommend using the MSFlexGrid controls instead, which has built-in 'cell' coloring options.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Listview Row Color

    I am one of those 'some people' who would recommend the use of the Flexgrid controls over a listview. I found (and because I am a slow learner) that the flexgrids are much more easy to understand, and SEEM to have better options for displaying information on the screen. I am sure some would disagree as they are very familiar with the use of the listview control, but, that is MY recommendation...use flexgrids.

    How's COVID in India?...sux here in the US. People refuse to comply with simple requests.
    Sam I am (as well as Confused at times).

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Listview Row Color

    Keep in mind the hazards of hot dog stand polychromatic user interfaces.

    Many of them fall apart miserably when a user turns on low-vision themes, color filters, etc. and can put your software in violation of disability legislation in many countries.

    We now also have things like Dark Mode to avoid running afoul of:

    https://www.pcmag.com/how-to/how-to-...-in-windows-10

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: Listview Row Color

    Here's a simplified version of an example on this I did a while back. It's set up to highlight a single row, but you'll see where you can easily change it to specify any condition you want to determine the rows, and the subitem if you don't want the entire row changed.
    Attached Files Attached Files
    Last edited by fafalone; Nov 23rd, 2020 at 04:52 PM.

  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: Listview Row Color

    Having trouble posting things, don't want to mess up the attachment of the project in the last post, so here's the code and SS.



    Name:  lvbk.jpg
Views: 654
Size:  22.7 KB

    All you have to do is subclass then apply a simple NM_CUSTOMDRAW routine:
    Code:
    Public Function F1LVWndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal uIdSubclass As Long) As Long
    Select Case uMsg
       Case WM_NOTIFY
            Dim tNMH As NMHDR
            CopyMemory tNMH, ByVal lParam, Len(tNMH)
            Select Case tNMH.Code
    
                    
                Case NM_CUSTOMDRAW
                    Dim nmcdr As NMLVCUSTOMDRAW
                    CopyMemory nmcdr, ByVal lParam, LenB(nmcdr)
                    Select Case nmcdr.nmcd.dwDrawStage
                        Case CDDS_PREPAINT
                            F1LVWndProc = CDRF_NOTIFYITEMDRAW
                            Exit Function
                        Case CDDS_ITEMPREPAINT
                            F1LVWndProc = CDRF_NOTIFYSUBITEMDRAW Or CDRF_NEWFONT
                            Exit Function
                        Case (CDDS_ITEMPREPAINT Or CDDS_SUBITEM)
                                Debug.Print "item=" & nmcdr.nmcd.dwItemSpec & ",target=" & gHighlight
                                If (nmcdr.nmcd.dwItemSpec = gHighlight) Then ' And (nmcdr.iSubitem = gHighlightSub) Then 'If you only want to set a subitem
                                    If gSetBk Then nmcdr.clrTextBk = vbYellow
                                    If gSetTxt Then nmcdr.clrText = vbRed
                                Else
                                    If gSetBk Then nmcdr.clrTextBk = vbWhite
                                    If gSetTxt Then nmcdr.clrText = vbBlack
                                End If
                                CopyMemory ByVal lParam, nmcdr, LenB(nmcdr)
                                F1LVWndProc = CDRF_NEWFONT
                                Exit Function
                    End Select
            End Select
                
       Case WM_DESTROY
         Call UnSubclass(hWnd, PtrLVWndProc)
    
    End Select
    
    F1LVWndProc = DefSubclassProc(hWnd, uMsg, wParam, lParam)
    
    End Function

  7. #7
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: Listview Row Color

    Quote Originally Posted by fafalone View Post
    Having trouble posting things, don't want to mess up the attachment of the project in the last post, so here's the code and SS.



    Name:  lvbk.jpg
Views: 654
Size:  22.7 KB

    All you have to do is subclass then apply a simple NM_CUSTOMDRAW routine:
    Code:
    Public Function F1LVWndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal uIdSubclass As Long) As Long
    Select Case uMsg
       Case WM_NOTIFY
            Dim tNMH As NMHDR
            CopyMemory tNMH, ByVal lParam, Len(tNMH)
            Select Case tNMH.Code
    
                    
                Case NM_CUSTOMDRAW
                    Dim nmcdr As NMLVCUSTOMDRAW
                    CopyMemory nmcdr, ByVal lParam, LenB(nmcdr)
                    Select Case nmcdr.nmcd.dwDrawStage
                        Case CDDS_PREPAINT
                            F1LVWndProc = CDRF_NOTIFYITEMDRAW
                            Exit Function
                        Case CDDS_ITEMPREPAINT
                            F1LVWndProc = CDRF_NOTIFYSUBITEMDRAW Or CDRF_NEWFONT
                            Exit Function
                        Case (CDDS_ITEMPREPAINT Or CDDS_SUBITEM)
                                Debug.Print "item=" & nmcdr.nmcd.dwItemSpec & ",target=" & gHighlight
                                If (nmcdr.nmcd.dwItemSpec = gHighlight) Then ' And (nmcdr.iSubitem = gHighlightSub) Then 'If you only want to set a subitem
                                    If gSetBk Then nmcdr.clrTextBk = vbYellow
                                    If gSetTxt Then nmcdr.clrText = vbRed
                                Else
                                    If gSetBk Then nmcdr.clrTextBk = vbWhite
                                    If gSetTxt Then nmcdr.clrText = vbBlack
                                End If
                                CopyMemory ByVal lParam, nmcdr, LenB(nmcdr)
                                F1LVWndProc = CDRF_NEWFONT
                                Exit Function
                    End Select
            End Select
                
       Case WM_DESTROY
         Call UnSubclass(hWnd, PtrLVWndProc)
    
    End Select
    
    F1LVWndProc = DefSubclassProc(hWnd, uMsg, wParam, lParam)
    
    End Function
    magic lParam:
    CopyMemory tNMH, ByVal lParam, Len(tNMH) '1st CopyMemory
    CopyMemory nmcdr, ByVal lParam, LenB(nmcdr) '2nd CopyMemory

    Is the lParam the same value in both CopyMemory? If so, is what data structure of lParam?

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: Listview Row Color

    The code snippet is just to highlight the critical part of the code, it's not the complete source (which is in the zip in the post above that).

    Here, the lParam contains the NMLVCUSTOMDRAW structure. The first part of that is the NMHDR structure, which is copied to determine the code, some codes inform us there's additional data that can be read from the address the lParam is pointing to.

    Code:
    Private Type NMHDR
      hWndFrom As Long   ' Window handle of control sending message
      IDFrom As Long        ' Identifier of control sending message
      Code  As Long          ' Specifies the notification code
    End Type
    Private Type NMCUSTOMDRAW
      hdr As NMHDR
      dwDrawStage As Long
      hDC As Long
      rc As RECT
      dwItemSpec As Long
      uItemState As Long
      lItemlParam As Long
    End Type
    Private Type NMLVCUSTOMDRAW
      nmcd As NMCUSTOMDRAW
      clrText As Long
      clrTextBk As Long
      iSubitem As Long
    End Type
    So when you copy the first 12 bytes (each Long is 4 bytes), you get just the NMHDR structure. Then the code tells you it's a NM_CUSTOMDRAW message, and since we're working with a ListView, we use the ListView extension, and can copy the full NMLVCUSTOMDRAW structure.
    Last edited by fafalone; Nov 23rd, 2020 at 07:43 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width