http://codeguru.earthweb.com/listview/background_image.shtml

解决方案 »

  1.   

    用SetBkColor()  SetTextBkColor() SetBkImage() SetTextColor()就可以了
      

  2.   

    There are two ways. one is use ownerdraw.
    other is use NM_COSTUMDRAW. the second way 
    is very simple. the netants use it.
    after ie4,the listview has support the background(bmp).
    if you want use second way,you need do as follow.
    1) in your app class OnInstance...,you need call AfxOleInit();
    2) in your ListView init..,you need call function SetBkImage();there three type,
       but i feel that using the arg is file name.
    3) then the listview has bkground but it is opaque,if you want it is transparent
       you need use NM_CUSTOMDRAW notify message.
       in your .h  define callback function.
       afx_msg void OnCustomDrawYourList( NMHDR* pNMHDR, LRESULT* pResult );
       in your .cpp 
       add message entry
       ON_NOTIFY(NM_CUSTOMDRAW, YOUR_LIST_ID, OnCustomDrawYourList)
       implement the callbak function.
       OnCustomDrawYourList(NMHDR* pNMHDR, LRESULT* pResult );
       {
        NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR); 
        *pResult = 0; if(pLVCD->nmcd.dwDrawStage==CDDS_PREPAINT){
    *pResult=CDRF_NOTIFYITEMDRAW;
    }
    else if(pLVCD->nmcd.dwDrawStage==(CDDS_ITEMPREPAINT | CDDS_SUBITEM)){ CDC* pDC=CDC::FromHandle(pLVCD->nmcd.hdc);
            int      nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
    CRect rcItem,rcText;
    LVITEM   rItem;
    CString sText=m_list.GetItemText(nItem,pLVCD->iSubItem);        UINT     uFormat=ILD_TRANSPARENT;                      
            BOOL bListHasFocus = ( m_list.GetSafeHwnd() == ::GetFocus() );       
            ZeroMemory ( &rItem, sizeof(LVITEM) );
            rItem.mask  = LVIF_IMAGE | LVIF_STATE;
            rItem.iItem = nItem;
            rItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
            m_list.GetItem ( &rItem ); //draw icon
    if(pLVCD->iSubItem==0){
            m_list.GetItemRect (nItem, &rcItem, LVIR_ICON );
            if((rItem.state & LVIS_SELECTED ) && bListHasFocus )
    uFormat |= ILD_FOCUS;
    m_imagelist.Draw(pDC,rItem.iImage,rcItem.TopLeft(),uFormat);
    } //get whole item rect.
    m_list.GetSubItemRect(nItem,pLVCD->iSubItem,LVIR_LABEL,rcItem);
    rcText=rcItem;
        rcText.top++;

    if(pLVCD->iSubItem==0){
            rcText.left += 20;
    rcItem.left +=20;
    }        if ( rItem.state & LVIS_SELECTED ){
                if ( bListHasFocus ){
                    pDC->SetTextColor (RGB(255,255,255));
    if(pLVCD->iSubItem !=0)
    pDC->FillSolidRect ( rcItem, RGB(0,0,255));
    }else{
    if(pLVCD->iSubItem !=0)
    pDC->FillSolidRect ( rcItem, RGB(192,192,192));
    }        }else{
    if(pLVCD->nmcd.uItemState & CDIS_HOT)
    pDC->SetTextColor(RGB(0,0,255));
    else
                pDC->SetTextColor ( GetSysColor ( COLOR_BTNTEXT ));
            }
            pDC->SetBkMode ( TRANSPARENT );
            pDC->DrawText ( sText, rcText, DT_VCENTER | DT_SINGLELINE );             // Draw a focus rect around the item if necessary.
            if ( bListHasFocus && ( rItem.state & LVIS_FOCUSED ) && pLVCD->iSubItem !=0 ){
                pDC->DrawFocusRect ( rcItem );
            } *pResult = CDRF_SKIPDEFAULT;
    }
    else if(pLVCD->nmcd.dwDrawStage ==CDDS_ITEMPREPAINT){
    *pResult=CDRF_NOTIFYSUBITEMDRAW;
    }
    }4 when the listview item is changed,you need invalidate the listview.To treeview,i think it is implemented with the same way. in fact, i
    have finish part work.