CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: CListCtrl

  1. #1
    Join Date
    Oct 1999
    Location
    Ireland
    Posts
    12

    CListCtrl

    I am trying to change the text background colour in different rows in my app using CListCtrl, but if I change the first and then reset the rest of the rows to a default colour the first changes to match the default colour. I am using the code:


    if (index == 0) {

    ptr->SetTextBkColor(0x000000);
    }
    else
    {
    ptr->SetTextBkColor(0xffff00);
    }




    Does anybody know why the first index does not stay as the orignal colour?




  2. #2
    Join Date
    Jul 1999
    Posts
    19

    Re: CListCtrl

    Hi,

    The answer is simple (and sad, from your point of view). The method you use enable you to set the background color of *all* items (and not for a single row only).
    If you want to have a different background color in different rows, you will have to work (a bit) harder. You should use the draw notification mechanism in your list control, and then set the bakcground color for the text in each item as you please.


    BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
    //{{AFX_MSG_MAP(CListCtrlEx)
    //}}AFX_MSG_MAP
    ON_NOTIFY_REFLECT( NM_CUSTOMDRAW, OnCustomDraw )
    END_MESSAGE_MAP()





    void CListCtrlEx::OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult )
    {
    // Change backword color as specified
    LPNMLVCUSTOMDRAW pNMLVCUSTOMDRAW = ( LPNMLVCUSTOMDRAW )pNMHDR;
    switch ( pNMLVCUSTOMDRAW->nmcd.dwDrawStage )
    {
    case CDDS_PREPAINT:
    *pResult = CDRF_NOTIFYITEMDRAW;
    break;
    case CDDS_ITEMPREPAINT:
    if ( pNMLVCUSTOMDRAW->nmcd.dwItemSpec == 0 )
    pNMLVCUSTOMDRAW->clrTextBk = 0x000000;
    else
    pNMLVCUSTOMDRAW->clrTextBk = 0xffff00;
    break;
    }
    }









    J



  3. #3
    Guest

    Re: CListCtrl

    That works if you have a class derived from CListCtrl, but what if you have a CListView class and use the embedded listctrl? How does this work then? And the listctrl is not an onwnerdrawn listctrl...
    If you have a solution, please let me know.
    Thankx.



  4. #4
    Join Date
    Aug 1999
    Location
    wa, usa
    Posts
    88

    Re: CListCtrl

    put this in your dialog message map

    ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_DATA, OnD)




    add this function to your class declaration:

    void OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult );




    and then use the same function:


    void CListTestDlg::OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult )
    {
    // Change backword color as specified
    LPNMLVCUSTOMDRAW pNMLVCUSTOMDRAW = ( LPNMLVCUSTOMDRAW )pNMHDR;
    switch ( pNMLVCUSTOMDRAW->nmcd.dwDrawStage )
    {
    case CDDS_PREPAINT:
    *pResult = CDRF_NOTIFYITEMDRAW;
    break; case CDDS_ITEMPREPAINT:
    if ( pNMLVCUSTOMDRAW->nmcd.dwItemSpec == 0 )
    pNMLVCUSTOMDRAW->clrTextBk = 0x000000;
    else
    pNMLVCUSTOMDRAW->clrTextBk = 0xffff00;
    break;
    }
    }





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

Featured