Announcement

Collapse
No announcement yet.

Changing listview colors

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing listview colors

    Code:
    CONTROL HANDLE hDlg, %IDC_LISTBOX1 TO hCtl&
    ListView_SetTextBkColor hCtl&, %RGB_LIGHTSLATEGRAY
     ListView_SetTextColor hCtl&,%RGB_BLACK
    ListView_SetBkColor hCtl&, %RGB_LIGHTSLATEGRAY
    this code will change from the default color one time
    if you want to change it a second time it will not
    i even tried this the second time
    Code:
    control send hdlg, %IDC_LISTBOX1, %LVM_SETTEXTCOLOR, 0, %Blue
    i tried this code "control" to set color then tried to change color the second time will not work
    happy new year
    thanks

  • #2
    You need to redraw the control after color change, CONTROL REDRAW...

    Comment


    • #3
      control send hdlg, %IDC_LISTBOX1, %LVM_SETTEXTCOLOR, 0, %Blue
      CONTROL SET COLOR does it too. Not sure if this includes a "free" refresh?

      Comment


      • #4
        Originally posted by Chris Holbrook View Post

        CONTROL SET COLOR does it too. Not sure if this includes a "free" refresh?
        Quite possibly Bob wrapped the API Set Color and Redraw into the one DDT command. It would make sense

        (ISTM rather pointless to use API commands with a DDT dialog and DDT controls where there is a suitable DDT command to do what you want )

        Comment


        • #5
          i was trying have rows with difference colors redraw would change all rows right
          thanks

          Comment


          • #6
            was trying have rows with difference colors redraw would change all rows right
            No, not right.

            That's the bad news.

            But there is good news:

            There are at least a half dozen examples in the source code forum of listviews with different colored rows and even different colored columns within rows. (CONTROL SET COLOR will not accomplish this).

            Just search on "listview" in that forum and you will find plenty of starter examples.





            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Yes, then you need to do custom draw. Like Michael says - many examples around. Here's one to get you started.
              Click image for larger version  Name:	ListViewLineColors1.jpg Views:	0 Size:	22.3 KB ID:	803282
              '
              Code:
              #COMPILE EXE
              #DIM ALL
              #INCLUDE "WIN32API.INC"
              #INCLUDE "COMMCTRL.INC"
              %IDC_LISTVIEW1 = 221
              
              '====================================================================
              FUNCTION PBMAIN() AS LONG
                LOCAL hDlg, c AS LONG
                DIALOG NEW 0, "ListView Line Colors",,, 205, 90, %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
              
                '------------------------------------------------------------------
                CONTROL ADD LISTVIEW, hDlg, %IDC_LISTVIEW1,"", 5, 5, 195, 80, _
                                      %WS_TABSTOP OR %LVS_REPORT OR %LVS_SHOWSELALWAYS OR _
                                      %LVS_SINGLESEL, %WS_EX_CLIENTEDGE
                LISTVIEW SET STYLEXX hDlg, %IDC_LISTVIEW1, %LVS_EX_GRIDLINES OR %LVS_EX_FULLROWSELECT
              
                '------------------------------------------------------------------
                LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW1, 1, "Number",    40, 0
                LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW1, 2, "Whatever",  60, 1
                LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW1, 3, "Important", 80, 2
              
                FOR c = 1 TO 15
                   LISTVIEW INSERT ITEM hDlg, %IDC_LISTVIEW1, c, 0, FORMAT$(c)
                   LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW1, c, 2, "Col2    " + "Row " + FORMAT$(c)
                   LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW1, c, 3, "Row " + FORMAT$(c)
                NEXT
                '------------------------------------------------------------------
              
                 DIALOG SHOW MODAL hDlg CALL DlgProc
              END FUNCTION
              
              '====================================================================
              CALLBACK FUNCTION DlgProc() AS LONG
                LOCAL lplvcd AS NMLVCUSTOMDRAW PTR, nmLvPtr AS NM_LISTVIEW PTR
              
                SELECT CASE CB.MSG
                CASE %WM_NOTIFY
                    SELECT CASE CB.NMID
                    CASE %IDC_LISTVIEW1
                        nmLvPtr = CB.LPARAM
              
                        SELECT CASE CB.NMCODE
                        CASE %LVN_COLUMNCLICK  'Click on column header
                            IF @nmLvPtr.iSubItem <> -1 THEN
                                DIALOG SET TEXT CB.HNDL, "Clicked Col" + STR$(@nmLvPtr.iSubItem+1)
                            END IF
              
                        CASE %LVN_ITEMCHANGED  ' Selection changed - also by keyboard
                            IF (@nmLvPtr.uNewState AND %LVIS_SELECTED) = %LVIS_SELECTED THEN
                                DIALOG SET TEXT CB.HNDL, "Selected Row" + STR$(@nmLvPtr.iItem+1) + _
                                                         ", Col" + STR$(@nmLvPtr.iSubItem+1)
                            END IF
              
                        CASE %NM_DBLCLK        'Double-click in list
                            IF @nmLvPtr.iItem > -1 THEN
                                DIALOG SET TEXT CB.HNDL, "Double-clicked Row" + STR$(@nmLvPtr.iItem+1) + _
                                                         ", Col" + STR$(@nmLvPtr.iSubItem+1)
                            END IF
              
                        CASE %NM_RCLICK        'Right-click in list
                            IF @nmLvPtr.iItem > -1 THEN
                                DIALOG SET TEXT CB.HNDL, "Right-clicked Row" + STR$(@nmLvPtr.iItem+1) + _
                                                         ", Col" + STR$(@nmLvPtr.iSubItem+1)
                            END IF
              
                        CASE %NM_CUSTOMDRAW  ' custom draw
                            lplvcd = CB.LPARAM
                            SELECT CASE @lplvcd.nmcd.dwDrawStage
                            CASE %CDDS_PREPAINT, %CDDS_ITEMPREPAINT
                                FUNCTION = %CDRF_NOTIFYITEMDRAW
              
                            CASE %CDDS_ITEMPREPAINT OR %CDDS_SUBITEM
                                IF @lplvcd.nmcd.dwItemSpec MOD 2 THEN      ' dwItemSpec = line number  iSubItem
                                    @lplvcd.clrTextBk = RGB(196, 255, 255) ' Baclground color
                                    @lplvcd.clrText   = %BLUE              ' text color
                                ELSE
                                    @lplvcd.clrTextBk = GetSysColor(%COLOR_INFOBK)
                                    @lplvcd.clrText   = %RED
                                END IF
                                FUNCTION = %CDRF_NEWFONT
                            END SELECT
                        END SELECT
              
                    END SELECT
                END SELECT
              END FUNCTION
              '

              Comment


              • #8
                This thread was very helpful to me. Is there a way to also color the header of the listview control without sub-classing?

                Comment


                • #9
                  I have not have much with header
                  what I have done IS subclassed the header and sized then to zero.
                  I then drew buttonS on top of where they were.

                  Comment


                  • #10
                    Never done it without subclassing (I think), but that part is easy to do, using some global variables, etc. Following is an old test code - original work was done by Jules Marchildon - I just cleaned it up a bit for my own purpose. Can be combined with the ListView colors example above.
                    Click image for larger version

Name:	LVHeaderColors.jpg
Views:	82
Size:	40.2 KB
ID:	806545
                    '
                    Code:
                    #COMPILE EXE
                    #DIM ALL
                    #IF %PB_REVISION >= &H1000  ' if compiler PBWIN10 or later
                      %UNICODE = 1              ' then we do unicode text
                    #ENDIF
                    #INCLUDE "WIN32API.INC"
                    #INCLUDE "COMMCTRL.INC"
                    
                    %IDC_LISTVIEW = 221
                    
                    GLOBAL hDlg, ghHeader, ghListview, OldSubClassProc AS DWORD
                    
                    '====================================================================
                    FUNCTION PBMAIN
                       LOCAL c AS LONG
                    
                       DIALOG NEW 0, "DDT - SysHeader32 colors",,, 264, 102, _
                                  %WS_MINIMIZEBOX OR %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
                    
                      '------------------------------------------------------------------
                      CONTROL ADD LISTVIEW, hDlg, %IDC_LISTVIEW,"", 5, 5, 254, 92, _
                                  %WS_CHILD OR %WS_TABSTOP OR %LVS_REPORT OR _
                                  %LVS_SHOWSELALWAYS OR %LVS_SINGLESEL, %WS_EX_CLIENTEDGE
                      LISTVIEW SET STYLEXX hDlg, %IDC_LISTVIEW, %LVS_EX_GRIDLINES OR %LVS_EX_FULLROWSELECT
                    
                      CONTROL HANDLE hDlg, %IDC_LISTVIEW TO ghListview         ' Get listview handle
                      ghHeader = SendMessage(ghListview, %LVM_GETHEADER, 0, 0) ' Get listview header's handle
                    
                     'Subclass ListView
                      OldSubClassProc = SetWindowLong(GetDlgItem(hDlg, %IDC_LISTVIEW), _
                                                      %GWL_WNDPROC, CODEPTR(ListViewProc))
                    
                      '------------------------------------------------------------------
                      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 1, "Column 1", 80,0
                      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 2, "Column 2", 80,2
                      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 3, "Column 3", 80,2
                    
                      FOR c = 1 TO 25
                          LISTVIEW INSERT ITEM hDlg, %IDC_LISTVIEW, c, 0, "Column 1,  Line " & FORMAT$(c, "00")
                          LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW, c, 2, "Column 2,  Line " & FORMAT$(c, "00")
                          LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW, c, 3, "Column 3,  Line " & FORMAT$(c, "00")
                      NEXT
                      '------------------------------------------------------------------
                      DIALOG SHOW MODAL hDlg CALL DlgProc
                    
                    END FUNCTION
                    
                    '====================================================================
                    CALLBACK FUNCTION DlgProc
                      SELECT CASE CBMSG
                      CASE %WM_COMMAND
                         SELECT CASE CBCTL
                         CASE %IDCANCEL ' Esc key..
                             IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                                 DIALOG END CB.HNDL, 0
                             END IF
                         END SELECT
                    
                      CASE %WM_DESTROY
                         IF OldSubClassProc THEN
                            SetWindowLong GetDlgItem(CB.HNDL, %IDC_LISTVIEW), _
                                          %GWL_WNDPROC, OldSubClassProc
                         END IF
                    
                      END SELECT
                    END FUNCTION
                    
                    '====================================================================
                    ' SubClassed ListView, handling custom draw notifications for Header
                    '--------------------------------------------------------------------
                    FUNCTION ListViewProc(BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, _
                                          BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
                      LOCAL hBrush AS DWORD
                      LOCAL pNmcd  AS NMCUSTOMDRAW PTR
                      LOCAL hdi    AS HD_ITEM
                      #IF %DEF(%UNICODE)
                         LOCAL zText AS WSTRINGZ * 255 ' should be enough..
                      #ELSE
                         LOCAL zText AS ASCIIZ * 255
                      #ENDIF
                    
                      SELECT CASE wMsg
                      CASE %WM_NOTIFY
                          pNmcd = lParam
                          SELECT CASE @pNmcd.hdr.code
                          CASE %NM_CUSTOMDRAW
                              SELECT CASE @pNmcd.dwDrawStage
                              CASE %CDDS_PREPAINT
                                  FUNCTION = %CDRF_NOTIFYITEMDRAW
                                  EXIT FUNCTION
                    
                              CASE %CDDS_ITEMPREPAINT
                                   ' DRAW HEADER BUTTON STATE (up/dn)
                                  SetBkMode @pNmcd.hdc, %TRANSPARENT
                                  IF (@pNmcd.uItemState AND %CDIS_SELECTED) THEN
                                      DrawFrameControl @pNmcd.hdc, @pNmcd.rc, %DFC_BUTTON ,%DFCS_BUTTONPUSH OR %DFCS_PUSHED
                                      SetTextColor @pNmcd.hdc, RGB(0,0,128)
                                  ELSE
                                      DrawFrameControl @pNmcd.hdc,@pNmcd.rc, %DFC_BUTTON , %DFCS_BUTTONPUSH
                                      SetTextColor @pNmcd.hdc, RGB(0,0,255)
                                  END IF
                    
                                  ' PAINT HEADER BACKGROUNDS
                                  hBrush = CreateSolidBrush(RGB(255, 223, 186))
                                  InflateRect @pNmcd.rc, -1,-1
                                  FillRect @pNmcd.hdc, @pNmcd.rc, hBrush
                                  DeleteObject hBrush
                    
                                  'GET THE HEADER ITEM TEXT...
                                  hdi.mask       = %HDI_TEXT
                                  hdi.psztext    = VARPTR(ztext)
                                  hdi.cchtextmax = SIZEOF(ztext)
                                  Header_GetItem ghHeader, @pNmcd.dwItemSpec, hdi
                    
                                  ' DRAW THE TEXT (offset text slightly if header pressed)
                                  IF (@pNmcd.uItemState AND %CDIS_SELECTED) THEN OffsetRect @pNmcd.rc, 0,-1
                                  DrawText @pNmcd.hdc, BYVAL VARPTR(zText), LEN(zText), @pNmcd.rc, _
                                           %DT_CENTER OR %DT_VCENTER OR %DT_SINGLELINE OR %DT_END_ELLIPSIS
                    
                                  FUNCTION = %CDRF_SKIPDEFAULT
                                  EXIT FUNCTION
                    
                              END SELECT
                          END SELECT
                      END SELECT
                    
                      FUNCTION = CallWindowProc(OldSubClassProc, hWnd, wMsg, wParam, lParam)
                    END FUNCTION
                    '

                    Comment


                    • #11
                      Thank you Sir Borje, very nice

                      Comment


                      • #12
                        Originally posted by Borje Hagsten View Post
                        Never done it without subclassing (I think), but that part is easy to do, using some global variables, etc. Following is an old test code - original work was done by Jules Marchildon - I just cleaned it up a bit for my own purpose. Can be combined with the ListView colors example above.
                        Perfect! Thanks.

                        Comment

                        Working...
                        X