Results 1 to 5 of 5

Thread: change background color in cell of listview

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    change background color in cell of listview

    Possible to change the background color, no text color(i know the way), in a single cell of listview, for example in row6 cell 9?

    note:
    the listview is in a userform of excel project.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: change background color in cell of listview

    As far as i know not possible in Office/VBA, since the only way to change the backcolor of a listview in VB is with a picture-box, and you don't have picturebox in VBA

    EDIT: Of course, you could create a PictureBox with the API-Calls and what not, but it's your decision if it's worth the hassle
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: change background color in cell of listview

    Quote Originally Posted by Zvoni View Post
    As far as i know not possible in Office/VBA, since the only way to change the backcolor of a listview in VB is with a picture-box, and you don't have picturebox in VBA

    EDIT: Of course, you could create a PictureBox with the API-Calls and what not, but it's your decision if it's worth the hassle
    tks for explain.

    really my prob is to insert a icon .gif in a listitems cell of listview but without imagelist component.
    And the ico is in a c:\mydir\ico.gif...
    possible?

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: change background color in cell of listview

    Every reference i found had the imagelist-component, so no idea if it's possible

    http://www.vbforums.com/showthread.p...items-Resolved
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: change background color in cell of listview

    Quote Originally Posted by Zvoni View Post
    Every reference i found had the imagelist-component, so no idea if it's possible

    http://www.vbforums.com/showthread.p...items-Resolved
    Code:
    Option Explicit
     
    Public Const NM_CUSTOMDRAW = (-12&)
    Public Const WM_NOTIFY As Long = &H4E&
     
    Public Const CDDS_PREPAINT As Long = &H1&
    Public Const CDRF_NOTIFYITEMDRAW As Long = &H20&
    Public Const CDDS_ITEM As Long = &H10000
    Public Const CDDS_ITEMPREPAINT As Long = CDDS_ITEM Or CDDS_PREPAINT
    Public Const CDRF_NEWFONT As Long = &H2&
     
    Private Const CDDS_SUBITEM& = &H20000
     
    Public 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
      
    ' sub struct of the NMCUSTOMDRAW struct
    Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
      
    ' generic customdraw struct
    Public 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
      
    ' listview specific customdraw struct
    Public Type NMLVCUSTOMDRAW
        nmcd As NMCUSTOMDRAW
        clrText As Long
        clrTextBk As Long
        ' if IE >= 4.0 this member of the struct can be used
        iSubItem As Integer
    End Type
     
    Public g_addProcOld As Long
    Public g_MaxItems As Long
        
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes&)
    Public Declare Function CallWindowProc& Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc&, ByVal hwnd&, ByVal Msg&, ByVal wParam&, ByVal lParam&)
      
    Public Const GWL_WNDPROC As Long = (-4&)
    Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
     
    Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Select Case iMsg
            Case WM_NOTIFY
                Dim udtNMHDR As NMHDR
                CopyMemory udtNMHDR, ByVal lParam, 12&
                With udtNMHDR
                    If .code = NM_CUSTOMDRAW Then
                        Dim udtNMLVCUSTOMDRAW As NMLVCUSTOMDRAW
                        CopyMemory udtNMLVCUSTOMDRAW, ByVal lParam, Len(udtNMLVCUSTOMDRAW)
                        With udtNMLVCUSTOMDRAW.nmcd
                            Select Case .dwDrawStage
                                Case CDDS_PREPAINT
                                    WindowProc = CDRF_NOTIFYITEMDRAW
                                    Exit Function
                                Case CDDS_ITEMPREPAINT
                                    WindowProc = CDRF_NOTIFYITEMDRAW
                                    Exit Function
                                Case CDDS_ITEMPREPAINT Or CDDS_SUBITEM
                                    Dim lCol As Long
                                    Dim lRow As Long
                                    
                                    lCol = udtNMLVCUSTOMDRAW.iSubItem  'zero based
                                    lRow = .dwItemSpec                 'zero based
                                    If (lCol = 1) And (lRow = 5) Then
                                        udtNMLVCUSTOMDRAW.clrTextBk = RGB(255, 0, 0)
                                        udtNMLVCUSTOMDRAW.clrText = RGB(255, 255, 0)
                                    Else
                                        udtNMLVCUSTOMDRAW.clrTextBk = RGB(255, 255, 255)
                                        udtNMLVCUSTOMDRAW.clrText = RGB(0, 0, 0)
                                    End If
                                    
                                    CopyMemory ByVal lParam, udtNMLVCUSTOMDRAW, Len(udtNMLVCUSTOMDRAW)
                                    WindowProc = CDRF_NEWFONT
                                    Exit Function
                            End Select
                        End With
                    End If
                End With
        End Select
        WindowProc = CallWindowProc(g_addProcOld, hwnd, iMsg, wParam, lParam)
    End Function
    peraph this solve my prob....
    but i dont know the way to change, for example, the background cell in listview in row 2 col 7

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