C ++, MFC redraw listcontrl, single line color set, each row shows a different color

1. Add the following code () before the message map END_MESSAGE_MAP

ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, OnCustomdrawList)

IDC_LIST1 change the ID list of the control according to the actual situation.

2. Add the following code in the project .h file

afx_msg void OnCustomdrawList(NMHDR*,LRESULT*);

The function name can be defined according to their own preferences, to be consistent with the message map function name.

3. project .cpp file, add the following function

void CRealPlayDlg :: OnCustomdrawList (pNMHDR the NMHDR *, * LRESULT pResult) 
{ 

    NMLVCUSTOMDRAW * = pLVCD reinterpret_cast <NMLVCUSTOMDRAW *> (pNMHDR);   
     * pResult = 0 ;    
    IF (CDDS_PREPAINT pLVCD- ==> nmcd.dwDrawStage)    
    {   
         * pResult = CDRF_NOTIFYITEMDRAW ;    
    }    
    the else  IF (CDDS_ITEMPREPAINT pLVCD- ==> nmcd.dwDrawStage)    
    {       
                // list of color per line, a total of three colors 
        IF (pLVCD-> nmcd.dwItemSpec% . 3 == . 1 )    
            pLVCD -> clrTextBk the RGB = ( 255, 0, 0);    
        else if(pLVCD->nmcd.dwItemSpec % 3 == 2)     
            pLVCD->clrTextBk = RGB(101, 147, 74);  
        if (pLVCD->nmcd.dwItemSpec % 3 == 0)   
            pLVCD->clrTextBk = RGB(244, 208, 0);    
        *pResult = CDRF_DODEFAULT;   
    }   
}

4. Run to

Read and write data to the list table, after a time to write. If you are interested can discuss the exchange with a message.

Guess you like

Origin www.cnblogs.com/Guo-xin/p/11723460.html