Announcement

Collapse
No announcement yet.

How to paint the background of a listview with gradient color

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

  • How to paint the background of a listview with gradient color

    I tried to paint a Listview with the gradient color background using Jim's DrawGradientLV() function
    but was not successful.

    see
    https://forum.powerbasic.com/forum/u...772#post785772


    Code:
    '_________________________________________________________________
    '
    '   SUB  DrawGradientLV
    '_________________________________________________________________
    
    SUB DrawGradientLV(BYVAL hDC AS DWORD, ItemRect AS RECT)
       LOCAL rectFill   AS RECT
       LOCAL fStep      AS SINGLE
       LOCAL hBrushGrad AS DWORD
       LOCAL lOnBand    AS LONG
       LOCAL Temp&
       LOCAL iItem      AS LONG
    
       'Put gradient on each item
       FOR iItem = ItemRect.nTop TO ItemRect.nBottom + 1
           INCR lOnBand
    
           SetRect _
               rectFill  _
             , ItemRect.nLeft      _
             , iItem               _
             , ItemRect.nRight + 1 _
             , iItem + 1
    
           Temp& = RGB((94 - lOnBand), (245 - lOnBand), (251 - lOnBand))  'LIGHT BLUE
           hBrushGrad = CreateSolidBrush(Temp&)
           Fillrect hDC, rectFill, hBrushGrad   'draw line across rectangle
           DeleteObject hBrushGrad
       NEXT
    
    END SUB

    My attempted program code is listed below
    those codes in green is my attempt to use Jim's color gradient routine

    All help appreciated to get this code corrected. Thank you

    Code:
    ' LV color2 GradBG.bas
    '  Highlight the listview cell whenever a cell is clicked
    '  unlike the others programs that high light the entire row when click
    
     ' Place in a color Gradient background
    
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
    #INCLUDE "COMMCTRL.INC"
    
    
    
    %IDC_LISTVIEW = 181
    
      GLOBAL  hDlg,hListView AS DWORD
      GLOBAL  CurrentRow, CurrentCol,MaxRow, MaxCol AS LONG
      GLOBAL  OrigLVProc AS DWORD
    
    
    
    FUNCTION PBMAIN () AS LONG
      LOCAL cRow AS LONG
    
      DIALOG NEW 0, "ListView color2",,, 266, 130, %WS_OVERLAPPEDWINDOW  _
                , 0 TO hDlg
    
    
    
      ' display the listview
        CONTROL ADD LISTVIEW, hDlg, %IDC_ListView,"", 5, 5, 255, 99, _
          %LVS_REPORT OR %WS_TABSTOP OR %LVS_SHOWSELALWAYS OR %LVS_SINGLESEL, %WS_EX_CLIENTEDGE
       CONTROL HANDLE hDlg, %IDC_ListView TO hListView
       LISTVIEW SET STYLEXX hDlg, %IDC_ListView,%LVS_EX_GRIDLINES OR %LVS_EX_FULLROWSELECT
    
    
    ' Listview data
      MaxCol = 2
      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 1, "Column 1", 130, 0
      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 2, "Column 2", 110, 2
      MaxRow = 20
      FOR cRow = 1 TO MaxRow
          LISTVIEW INSERT ITEM hDlg, %IDC_LISTVIEW, cRow, 0, "Col 1, " & "Line" & STR$(cRow)
          LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW, cRow, 1, "Col 1, " & "Line" & STR$(cRow)
          LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW, cRow, 2, "Col 2, " & "Line" & STR$(cRow)
      NEXT
    
    
    
    
      DIALOG SHOW MODAL hDlg, CALL DlgProc
    
    END FUNCTION
    
    
    
    ' Main Dialog callback
    CALLBACK FUNCTION DlgProc() AS LONG
    
       LOCAL pLVDI AS LV_DISPINFO PTR
       LOCAL lplvcd AS NMLVCUSTOMDRAW PTR
       LOCAL LpLvNm AS NM_LISTVIEW PTR
    
       LOCAL dis    AS DRAWITEMSTRUCT PTR
       LOCAL iTemRect AS RECT
    
    
    
    
      SELECT CASE CB.MSG
             CASE %WM_INITDIALOG
                 CurrentRow = 1 : CurrentCol = 1
                 DIALOG SET TEXT CB.HNDL  , "ListView cell  row " _
                  + STR$(CurrentRow) + " col " + STR$(CurrentCol)
             '    subclass LV for keyboard keys
                 OrigLVProc = SetWindowLong(hListView, %GWL_WndProc, CODEPTR(NewLVProc))
    
    
    
        CASE %WM_DRAWITEM
                 dis = CB.LPARAM
                'dis is Draw Item Structure
                'lParam = Pointer to a DRAWITEMSTRUCT structure containing information
                'about the item to be drawn and the type of drawing required.
                iTemRect = @dis.rcItem
                 DrawGradientLV(@dis.hDc, iTemRect)
    
    
    
      CASE %WM_DESTROY
             SetWindowLong hListView, %GWL_WNDPROC, OrigLVProc
    
    
      CASE %WM_COMMAND
          SELECT CASE CB.CTL
          CASE %IDCANCEL
              IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                  DIALOG END CB.HNDL, 0
              END IF
          END SELECT
    
      CASE %WM_NOTIFY
             SELECT CASE CB.NMID
                CASE %IDC_ListView
                   SELECT CASE CB.NMCODE
                      CASE %LVN_ITEMCHANGING
                         FUNCTION = %True
    
                      CASE %NM_CLICK
                         LpLvNm = CB.LPARAM
                         CurrentRow = @LpLvNm.iiTem + 1
                         CurrentCol = @LpLvNm.iSubItem + 1
                         CONTROL REDRAW hDlg, %IDC_ListView
                          DIALOG SET TEXT CB.HNDL  , "ListView cell  row " _
                            + STR$(CurrentRow) + " col " + STR$(CurrentCol)
    
                      CASE %NM_CUSTOMDRAW
                           lpLvCd = CBLPARAM
                           SELECT CASE @lplvcd.nmcd.dwDrawStage
                              CASE %CDDS_PREPAINT, %CDDS_ITEMPREPAINT
                                 FUNCTION = %CDRF_NOTIFYSUBITEMDRAW
                              CASE %CDDS_ITEMPREPAINT OR %CDDS_SUBITEM
                                 IF  @lplvcd.nmcd.dwItemSpec = CurrentRow-1 THEN
                                    IF @lpLvCd.iSubItem <> CurrentCol-1 THEN
                                       @lpLvCd.clrTextBk = %WHITE
                                    ELSE
                                       @lpLvCd.clrTextBk = %RGB_YELLOW
                                    END IF
                                 END IF
                               FUNCTION = %CDRF_NEWFONT
                           END SELECT
                   END SELECT
             END SELECT
    
    
    
    
    
      END SELECT
    
    END FUNCTION
    
    
    
    '_________________________________________________________________
    '
    '   SUB  DrawGradientLV
    '_________________________________________________________________
    
    SUB DrawGradientLV(BYVAL hDC AS DWORD, ItemRect AS RECT)
       LOCAL rectFill   AS RECT
       LOCAL fStep      AS SINGLE
       LOCAL hBrushGrad AS DWORD
       LOCAL lOnBand    AS LONG
       LOCAL Temp&
       LOCAL iItem      AS LONG
    
       'Put gradient on each item
       FOR iItem = ItemRect.nTop TO ItemRect.nBottom + 1
           INCR lOnBand
    
           SetRect _
               rectFill  _
             , ItemRect.nLeft      _
             , iItem               _
             , ItemRect.nRight + 1 _
             , iItem + 1
    
           Temp& = RGB((94 - lOnBand), (245 - lOnBand), (251 - lOnBand))  'LIGHT BLUE
           hBrushGrad = CreateSolidBrush(Temp&)
           Fillrect hDC, rectFill, hBrushGrad   'draw line across rectangle
           DeleteObject hBrushGrad
       NEXT
    
    END SUB
    
    
    
    
    
    
    ' Subclass ListView procedure
    ' for responding to keyboard keys
    FUNCTION NewLVProc(BYVAL hWnd AS LONG, BYVAL Msg AS LONG, _
        BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG
    
    
    
       SELECT CASE Msg
          CASE %WM_KEYDOWN
             SELECT CASE wParam
    
                CASE %VK_Up
                   CurrentRow = MAX(1,CurrentRow-1)
                   CONTROL REDRAW hDlg, %IDC_ListView
    
                CASE %VK_Down
                   CurrentRow = MIN(MaxRow,CurrentRow+1)
                   CONTROL REDRAW hDlg, %IDC_ListView
    
                CASE %VK_Left
                   CurrentCol = MAX(1,CurrentCol-1)
                   CONTROL REDRAW hDlg, %IDC_ListView
    
                CASE %VK_Right
                   CurrentCol = MIN(MaxCol,CurrentCol+1)
                   CONTROL REDRAW hDlg, %IDC_ListView
    
               CASE %VK_Home
                   CurrentCol = 1
                   IF GetKeyState(%VK_Control) THEN CurrentRow = 1
                   CONTROL REDRAW hDlg, %IDC_ListView
    
                CASE %VK_End
                   CurrentCol = MaxCol
                   IF GetKeyState(%VK_Control) THEN CurrentRow = MaxRow
                   CONTROL REDRAW hDlg, %IDC_ListView
            END SELECT
       END SELECT
       FUNCTION = CallWindowProc(OrigLVProc, hWnd, Msg, wParam, lParam)
    END FUNCTION


  • #2
    On Jim's advice, I have added style for the ownerdrawn Listview as listed below

    Code:
     ' display the listview
        CONTROL ADD LISTVIEW, hDlg, %IDC_ListView,"", 5, 5, 255, 99, _
          %WS_CHILD OR %WS_VISIBLE OR _
          %LVS_REPORT OR %WS_TABSTOP OR %LVS_SHOWSELALWAYS OR %LVS_SINGLESEL OR %LVS_OWNERDRAWFIXED, %WS_EX_CLIENTEDGE

    but I now gotten a blank listview --- how to resolve this ?

    Click image for larger version

Name:	LV blank.png
Views:	91
Size:	15.0 KB
ID:	785790

    Comment


    • #3
      Tim,
      You will need to finish the %WM_DRAWITEM section to draw the text in the control. Keywords: DrawTextA and SetTextColor.

      Also %NM_CUSTOMDRAW is no longer needed when using OwnerDraw method and %WM_DRAWITEM. All the listview drawing is done in the %WM_DRAWITEM section.

      Define LVData
      Code:
      GLOBAL LVData() AS STRING
      
      
      
      DIM LVData(0 TO 3)  AS STRING
      
      LVData(0) "Tim" & $TAB & "Lakinir 0" & $TAB
      LVData(1) "Tim" & $TAB & "Lakinir 1" & $TAB
      LVData(2) "Tim" & $TAB & "Lakinir 2" & $TAB
      LVData(3) "Tim" & $TAB & "Lakinir 3" & $TAB
      Although not tested, I'm thinking this extraction should work...

      Code:
      '________________________________________________________________________________
      '
      '   SUB DrawItemCommand
      '________________________________________________________________________________
      
      SUB DrawItemCommand(BYVAL wParamX AS LONG, BYVAL lParamX AS LONG)
        LOCAL dis         AS DRAWITEMSTRUCT PTR
        LOCAL ColumnRect  AS RECT
        LOCAL PointXY     AS POINT
        LOCAL ItemPosX    AS LONG
        LOCAL COLx        AS LONG
        LOCAL hFont_A     AS LONG
        LOCAL iTemRect    AS RECT
      
        giTheReturn = 0
      
        dis = lParamX
        'dis is Draw Item Structure
        'lParamX = Pointer to a DRAWITEMSTRUCT structure containing information
        'about the item to be drawn and the type of drawing required.
      
        SELECT CASE @dis.CtlId
            CASE %ID_ListView
                GOSUB ProcessListView
        END SELECT
        giTheReturn = %TRUE
        GOTO AroundTheEnd
      
        ProcessListView:
        iTemRect = @dis.rcItem
      
        DrawGradientLV(@dis.hDc, iTemRect)
      
        SELECT CASE @dis.itemAction
            CASE %ODA_DRAWENTIRE, %ODA_SELECT
                'ODA_DRAWENTIRE: The entire control needs to be drawn.
                'ODA_SELECT:     The selection status has changed
      
                'Get item height into rect
                ColumnRect.nLeft = %LVIR_BOUNDS 'LVM_GETITEMRECT will ask for the bounding rectangle of the entire item, including the icon and label.
                SendMessageA(hListView, %LVM_GETITEMRECT, @dis.itemID, BYVAL VARPTR(ColumnRect))          'Get the rect
      
                'Clear background and set text plus background color
                IF (@dis.itemState AND %ODS_SELECTED) THEN 'Row is selected
                    FillRect(@dis.hDC, @dis.rcItem, GetSysColorBrush(28)) '%COLOR_HIGHLIGHT))         22   'Fill selected row background color
                    SetBkColor(@dis.hDC, GetSysColor(28)) '%COLOR_HIGHLIGHT))                         22   'Set selected row background text color
                    SetTextColor(@dis.hDC, GetSysColor(26)) '%COLOR_HIGHLIGHTTEXT))                        'Set selected row foreground text color
      
                ELSE 'Row is not selected
      
                    SetTextColor @dis.hDc, RGB(0,0,0) 'gdwMyColorRefText
      
                END IF
      
                'Calculate column position and width, grab and draw text
                SendMessageA(hListView, %LVM_GETITEMPOSITION, 0, BYVAL VARPTR(PointXY))              'Get row position
                ItemPosX = PointXY.x - 2
      
                FOR COLx = 0 TO 1 '6
                    IF (@dis.itemState AND %ODS_SELECTED) = %FALSE THEN                              'Row is not selected
      
                        SetTextColor @dis.hDc, RGB(0,0,0) 'gdwMyColorRefText
      
                    END IF
      
                    ListviewColumn.mask = %LVCF_FMT OR %LVCF_WIDTH OR %LVCF_ORDER                    'Ask for column format, with, and order
                    SendMessageA(hListView, %LVM_GETCOLUMN, COLx, BYVAL VARPTR(ListviewColumn))      'Get column info
      
                    ColumnRect.nLeft  = ItemPosX                                                     'Set column left position
                    ItemPosX          = ItemPosX + ListviewColumn.cx                                 'Compute column width
                    ColumnRect.nRight = ItemPosX                                                     'Set column right position
                    InflateRect(ColumnRect, -3, 0)                                                   'Reduce width
      
                    zText = $CRLF & PARSE$(LVData(@dis.itemID), $TAB, COLx + 1) 'Get text from text atrray
      
                    IF COLx = 0 THEN
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_RIGHT) 'OR _          'Text is right aligned
                                   '%DT_VCENTER OR %DT_END_ELLIPSIS OR %DT_SINGLELINE)
      
                    ELSE
                        IF (ListviewColumn.fmt AND %LVCFMT_CENTER) THEN  'Is text centered, at left, or at right
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_CENTER) 'OR _         'Text is centered
                                   '%DT_END_ELLIPSIS) '%DT_VCENTER OR %DT_SINGLELINE)
                        ELSEIF (ListviewColumn.fmt AND %LVCFMT_RIGHT) THEN
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_RIGHT) 'OR _          'Text is right aligned
                                   '%DT_END_ELLIPSIS) ''%DT_VCENTER OR %DT_SINGLELINE)
                        ELSE
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_LEFT) 'OR _           'Text is left aligned
                                   '%DT_END_ELLIPSIS) ''%DT_VCENTER OR %DT_SINGLELINE)
                        END IF
                        InflateRect(ColumnRect, 3, 0)
                    END IF  'Restore width to original size
                NEXT
      
                giTheReturn = %TRUE
      
        END SELECT
        RETURN
      
        AroundTheEnd:
      END SUB
      Note:
      "Tim" should be right aligned with no vertical center and "Lakinir" should be left aligned with vertical center.

      That was an extraction from EjectDrive which is multi-line for each item. 0 to 1 column.

      This is an extraction from DriveChange which is single line for each item. 0 to 6 column.


      Code:
      '________________________________________________________________________________
      '
      '   SUB DrawItemCommand
      '________________________________________________________________________________
      
      SUB DrawItemCommand(BYVAL wParamX AS LONG, BYVAL lParamX AS LONG)
        LOCAL dis         AS DRAWITEMSTRUCT PTR
        LOCAL ColumnRect  AS RECT
        LOCAL PointXY     AS POINT
        LOCAL ItemPosX    AS LONG
        LOCAL COLx        AS LONG
        LOCAL hFont_A     AS LONG
        LOCAL iTemRect    AS RECT
      
        giTheReturn = 0
      
        dis = lParamX
        'dis is Draw Item Structure
        'lParamX = Pointer to a DRAWITEMSTRUCT structure containing information
        'about the item to be drawn and the type of drawing required.
      
        SELECT CASE @dis.CtlId
            CASE %ID_ListView
                GOSUB ProcessListView
        END SELECT
        giTheReturn = %TRUE
        GOTO AroundTheEnd
      
        ProcessListView:
        iTemRect = @dis.rcItem
      
        DrawGradientLV(@dis.hDc, iTemRect)
      
        SELECT CASE @dis.itemAction
            CASE %ODA_DRAWENTIRE, %ODA_SELECT
                'ODA_DRAWENTIRE: The entire control needs to be drawn.
                'ODA_SELECT:     The selection status has changed
      
      
                'Get item height into rect
                ColumnRect.nLeft = %LVIR_BOUNDS 'LVM_GETITEMRECT will ask for the bounding rectangle of the entire item, including the icon and label.
                SendMessageA(hListView, %LVM_GETITEMRECT, @dis.itemID, BYVAL VARPTR(ColumnRect))          'Get the rect
      
                'Clear background and set text plus background color
                IF (@dis.itemState AND %ODS_SELECTED) THEN 'Row is selected
      
                    FillRect(@dis.hDC, @dis.rcItem, GetSysColorBrush(28)) '%COLOR_HIGHLIGHT))      'Fill selected row background color
                    SetBkColor(@dis.hDC, GetSysColor(28))                 '%COLOR_HIGHLIGHT))      'Set selected row background text color
                    SetTextColor(@dis.hDC, GetSysColor(26))               '%COLOR_HIGHLIGHTTEXT))  'Set selected row foreground text color
      
                ELSE 'Row is not selected
      
                    SetTextColor @dis.hDc, RGB(0,0,0) 'gdwMyColorRefText
      
                END IF
      
                'Calculate column position and width, grab and draw text
                SendMessageA(hListView, %LVM_GETITEMPOSITION, 0, BYVAL VARPTR(PointXY))              'Get row position
                ItemPosX = PointXY.x - 2
      
                FOR COLx = 0 TO 6
                    IF (@dis.itemState AND %ODS_SELECTED) = %FALSE THEN                              'Row is not selected
      
                        SetTextColor @dis.hDc, RGB(0,0,0) 'gdwMyColorRefText
      
                    END IF
      
                    ListviewColumn.mask = %LVCF_FMT OR %LVCF_WIDTH OR %LVCF_ORDER                    'Ask for column format, with, and order
                    SendMessageA(hListView, %LVM_GETCOLUMN, COLx, BYVAL VARPTR(ListviewColumn))      'Get column info
      
                    ColumnRect.nLeft  = ItemPosX                                                     'Set column left position
                    ItemPosX          = ItemPosX + ListviewColumn.cx                                 'Compute column width
                    ColumnRect.nRight = ItemPosX                                                     'Set column right position
                    InflateRect(ColumnRect, -3, 0)                                                   'Reduce width
      
                    IF COLx = 1 THEN
                        zText = MID$(PARSE$(LVData(@dis.itemID), $TAB, COLx + 1), 1, 40)             'limited to 40 characters
                    ELSE
                        zText = PARSE$(LVData(@dis.itemID), $TAB, COLx + 1)                          'Get text from text atrray
                    END IF
      
                    IF COLx = 0 THEN
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_RIGHT OR _          'Text is right aligned
                                   %DT_VCENTER OR %DT_END_ELLIPSIS OR %DT_SINGLELINE)
      
                    ELSE
                        IF (ListviewColumn.fmt AND %LVCFMT_CENTER) THEN                              'Is text centered, at left, or at right
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_CENTER OR _         'Text is centered
                                   %DT_VCENTER OR %DT_END_ELLIPSIS OR %DT_SINGLELINE)
                        ELSEIF (ListviewColumn.fmt AND %LVCFMT_RIGHT) THEN
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_RIGHT OR _          'Text is right aligned
                                   %DT_VCENTER OR %DT_END_ELLIPSIS OR %DT_SINGLELINE)
                        ELSE
                          DrawTextA(@dis.hDC, zText, LEN(zText), ColumnRect, %DT_LEFT OR _           'Text is left aligned
                                   %DT_VCENTER OR %DT_END_ELLIPSIS OR %DT_SINGLELINE)
                        END IF
                        InflateRect(ColumnRect, 3, 0)
                    END IF                                                    'Restore width to original size
                NEXT
      
                giTheReturn = %TRUE
      
        END SELECT
        RETURN
      
        AroundTheEnd:
      END SUB
      Last edited by Jim Fritts; 12 Oct 2019, 01:30 PM.

      Comment


      • #4
        Thanks Jim, it is very complicated .. I'll need some time to look at how you did your DrawItemCommand()

        Comment


        • #5
          No worries

          You can also look here. My code is a modified version of that.
          Last edited by Jim Fritts; 12 Oct 2019, 05:07 PM.

          Comment


          • #6
            This is a test...
            Post#5 4:03 pm

            Post#6 4:14 AM (No edit on Post #5)

            Comment


            • #7
              Thank you Jim

              Comment


              • #8
                Now finally, I was able to display the gradient background for the listview after cobbling together other's codes

                Click image for larger version

Name:	LV grad.png
Views:	62
Size:	24.4 KB
ID:	785813



                Here's the code, if any member find an error here such as flickering or slowness or memory conflicts
                please correct this error for all of us

                Code:
                '  Ownerdrawn ListView.bas
                
                
                    ' Ownerdrawn ListView with a gradient color background
                    ' Thank you to Borje Hagsten, Pierre Bellise and Jim Fritts
                
                
                
                '====================================================================
                ' Declares
                '--------------------------------------------------------------------
                #COMPILE EXE
                #DIM ALL
                #INCLUDE "WIN32API.INC"
                #INCLUDE "COMMCTRL.INC"
                #INCLUDE "INITCTRL.INC"
                %IDC_LISTVIEW1 = 181
                
                
                 GLOBAL  CurrentRow, CurrentCol AS LONG
                 GLOBAL hDlg AS DWORD
                 GLOBAL LVData() AS STRING
                
                
                '====================================================================
                ' Program entrance
                FUNCTION PBMAIN () AS LONG
                
                  DIALOG NEW 0, "ListView color test",,, 360, 220, %WS_CAPTION OR _
                            %WS_SYSMENU OR %WS_MINIMIZEBOX, 0 TO hDlg
                
                 ' Add in the ListView
                   CreateLV
                
                   CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Quit", 310, 205, 40, 12
                
                   DIALOG SHOW MODAL hDlg, CALL DlgProc
                
                END FUNCTION
                
                
                
                
                '===================================
                ' Add in the Listview
                SUB CreateLV
                    LOCAL  dwStyle AS DWORD
                    LOCAL cRow AS LONG
                    LOCAL  ListviewItemHeight, hIml ,hListviewU  AS DWORD
                
                     ' need to have %LVS_OWNERDRAWFIXED style for the Listview
                    dwStyle = %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %LVS_REPORT OR _
                            %LVS_SINGLESEL OR %LVS_OWNERDRAWFIXED OR %LVS_SHOWSELALWAYS
                
                      CONTROL ADD LISTVIEW, hDlg, %IDC_LISTVIEW1, "", 5, 5, 285, 200, _
                                           dwStyle, %WS_EX_CLIENTEDGE
                
                
                   '     Create list view with big cell height -- inorder to
                   '     better show gradient color in the cell
                       ' Specify item height in pixels
                         ListviewItemHeight = 30
                        ' Create dummy imagelist:
                         hIml = ImageList_Create(1, ListviewItemHeight, %ILC_COLORDDB, 0, 0)
                     '   Set dummy imagelist in listview:
                         CONTROL HANDLE hDlg, %IDC_ListView1 TO hListViewU
                         ListView_SetImageList hListviewU, BYVAL hIml, %LVSIL_SMALL
                
                 ' column headers
                  LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW1, 1, "              Column 1", 110, 0
                  LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW1, 2, "Column 2", 110, 2
                
                
                
                
                 ' setup the Listview data arrays
                  REDIM LVData( 1 TO 20, 1 TO 2 )
                  FOR cRow = 1 TO 20
                      LVData( cRow, 1)=    "Row" & STR$(cRow) & "   Column 1  "
                      LVData( cRow, 2)=    "Row" & STR$(cRow) & "   Column 2  "
                
                      LISTVIEW INSERT ITEM hDlg, %IDC_LISTVIEW1, cRow, 0,LVData( cRow, 1)
                      LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW1, cRow, 1, LVData( cRow, 1)
                      LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW1, cRow, 2, LVData( cRow, 2)
                  NEXT
                
                
                END SUB
                
                
                
                '====================================================================
                ' Main Dialog procedure
                '--------------------------------------------------------------------
                CALLBACK FUNCTION DlgProc() AS LONG
                
                
                 SELECT CASE CB.MSG
                        CASE %WM_INITDIALOG
                             CurrentRow = 1
                             CurrentCol =  1
                
                      CASE %WM_COMMAND
                          SELECT CASE CB.CTL
                            CASE %IDCANCEL
                              IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                                 DIALOG END CB.HNDL, 0
                              END IF
                            END SELECT
                
                
                  CASE %WM_DRAWITEM
                      '  for Ownerdrawn highlight of focus row
                      IF CB.WPARAM = %IDC_LISTVIEW1 THEN
                          DrawLVitems  GetDlgItem(CB.HNDL, CB.WPARAM), CB.LPARAM , CurrentCol
                      END IF
                
                
                  CASE %WM_NOTIFY
                      IF LO(WORD, CB.WPARAM) = %IDC_LISTVIEW1 THEN
                
                          LOCAL nmLvPtr AS NM_LISTVIEW PTR
                          nmLvPtr = CB.LPARAM
                
                
                
                          SELECT CASE @nmLvPtr.hdr.code
                          CASE %LVN_COLUMNCLICK
                                'Click on column header
                              IF @nmLvPtr.iSubItem <> -1 THEN
                                  DIALOG SET TEXT CB.HNDL, _
                                    "Clicked Column header " +  STR$(@nmLvPtr.iSubItem+1)
                              END IF
                
                
                
                
                           CASE %NM_CLICK , %NM_DBLCLK
                               ' when clicked and double clicked on a cell
                                LOCAL LpLvNm AS NM_LISTVIEW PTR
                                LpLvNm = CB.LPARAM
                                CurrentRow = @LpLvNm.iiTem + 1
                                CurrentCol = @LpLvNm.iSubItem + 1
                                DIALOG SET TEXT CB.HNDL," Clicked row " + STR$( CurrentRow) + _
                                                     ", col " + STR$(CurrentCol) + _
                                                     "           Data array value :  " + _
                                                     LVData(CurrentRow ,CurrentCol)
                
                              ' Refresh the listview
                                InvalidateRect(GetDlgItem(CBHNDL, %IDC_LISTVIEW1), BYVAL 0, 0)
                
                          END SELECT
                      END IF
                
                  END SELECT
                
                END FUNCTION
                
                
                
                '====================================================================
                ' Ownerdrawn procedure
                '--------------------------------------------------------------------
                SUB DrawLVitems(hListView AS DWORD, lParam AS LONG,BYVAL CCol AS LONG  )
                  LOCAL hBrush AS DWORD, cw1, cw2 AS LONG
                  LOCAL lpdis AS DRAWITEMSTRUCT PTR
                  LOCAL pt AS POINTAPI
                  LOCAL  rc AS RECT
                  STATIC zTxt AS ASCIIZ * 100
                  LOCAL  rctF AS RECT
                  LOCAL  ColFc1 , ColFc2 AS LONG
                
                  lpdis = lParam
                  SELECT CASE @lpdis.itemAction
                
                  CASE %ODA_DRAWENTIRE, %ODA_SELECT
                      'CLEAR BACKGROUND
                      IF (@lpdis.itemState AND %ODS_SELECTED) THEN
                          ' row is selected -- color the entire row
                          hBrush = CreateSolidBrush(%RGB_LIGHTYELLOW)
                          FillRect @lpdis.hDC, @lpdis.rcItem, hBrush
                          DeleteObject hBrush
                
                          SetBkColor @lpdis.hDC, %RGB_LIGHTYELLOW
                          SetTextColor @lpdis.hDC,  %RGB_DARKBLUE
                      ELSE
                          '  Not selected row -- shows the gradient colors
                          DrawGradientLV(@lpdis.hDc, @lpdis.rcItem)
                          SetTextColor @lpdis.hDC,  %RGB_DARKBLUE
                      END IF
                
                      'GET COORDINATES
                      ListView_GetItemPosition(hListView, 0, pt)  'get ev. scrollpos
                      cw1 = ListView_GetColumnWidth(hListView, 0) 'width, column 1
                      cw2 = ListView_GetColumnWidth(hListView, 1) 'width, column 2
                      rc  = @lpdis.rcItem                         'item rect
                
                
                
                      'DRAW FOCUSRECT (if control has focus)
                      ColFc1 = 0
                      ColFc2 = 0
                      IF GetFocus() = hListView AND (@lpdis.itemState AND %ODS_SELECTED) THEN
                        IF CCol = 1 THEN
                           rctf.nLeft = pt.x
                           rctf.nRight = rctf.nLeft  + cw1
                           rctf.nTop  = @lpdis.rcItem.ntop
                           rctf.nBottom  = @lpdis.rcItem.nBottom
                         ' set the focus text color indicator to red
                           ColFc1 = 1
                        ELSEIF   CCol = 2 THEN
                           rctf.nLeft  = pt.x   + cw1
                           rctf.nRight = rctf.nLeft  + cw2
                           rctf.nTop   = @lpdis.rcItem.nTop
                           rctf.nBottom  = @lpdis.rcItem.nBottom
                         ' set the focus text color indicator to red
                           ColFc2 = 1
                        END IF
                        RoundRect(  @lpdis.hDC, rctf.nLeft, rctf.ntop, rctf.nRight, rctf.nbottom, 2, 2)
                      END IF
                
                      ' Draw the texts -- all centerized
                      'COLUMN 1
                      rc.nLeft = pt.x : rc.nRight = rc.nLeft + cw1
                      ListView_GetItemText hListView, @lpdis.itemID, 0, zTxt, SIZEOF(zTxt)
                      IF ColFc1 = 1 THEN
                         SetTextColor @lpdis.hDC,  %RGB_RED
                      ELSE
                         SetTextColor @lpdis.hDC,  %RGB_DARKBLUE
                      END IF
                      DrawText @lpdis.hDC, zTxt, LEN(zTxt), rc, %DT_CENTER OR %DT_SINGLELINE OR %DT_VCENTER
                
                
                      'COLUMN 2
                      rc.nLeft  = rc.nRight : rc.nRight = rc.nLeft + cw2
                      ListView_GetItemText hListView, @lpdis.itemID, 1, zTxt, SIZEOF(zTxt)
                      IF ColFc2 = 1 THEN
                         SetTextColor @lpdis.hDC,  %RGB_RED
                      ELSE
                         SetTextColor @lpdis.hDC,  %RGB_DARKBLUE
                      END IF
                      DrawText @lpdis.hDC, zTxt, LEN(zTxt), rc, %DT_CENTER OR %DT_SINGLELINE OR %DT_VCENTER
                
                
                  END SELECT
                END SUB
                
                
                
                
                 '_________________________________________________________________
                '
                '   SUB  DrawGradientLV  by Jim Fritts
                '_________________________________________________________________
                
                SUB DrawGradientLV(BYVAL hDC AS DWORD, ItemRect AS RECT)
                   LOCAL rectFill   AS RECT
                   LOCAL fStep      AS SINGLE
                   LOCAL hBrushGrad AS DWORD
                   LOCAL lOnBand    AS LONG
                   LOCAL Temp&
                   LOCAL iItem      AS LONG
                
                   'Put gradient on each item
                   FOR iItem = ItemRect.nTop TO ItemRect.nBottom + 1
                        lOnBand = lOnBand + 3
                       SetRect _
                           rectFill  _
                         , ItemRect.nLeft      _
                         , iItem               _
                         , ItemRect.nRight + 1 _
                         , iItem + 1
                
                        Temp& = RGB((215 - lOnBand), (255 - lOnBand), (255 - lOnBand))  'LIGHT BLUE
                       hBrushGrad = CreateSolidBrush(Temp&)
                       Fillrect hDC, rectFill, hBrushGrad   'draw line across rectangle
                       DeleteObject hBrushGrad
                   NEXT
                
                END SUB

                Comment


                • #9
                  Tim,
                  Looks like that will work.

                  Comment


                  • #10
                    Good job

                    Comment

                    Working...
                    X