我调用了CListCtrl中的DrawItem()可以有颜色出现了,但是每行的字都没有了
我不知道错在了那里,那位高手给我只点一下吧!
以下是代码
void CMyDataBaseView::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct ) 
{
CListCtrl& theCtrl = GetListCtrl();
CDC             *pDC = CDC::FromHandle(lpDrawItemStruct -> hDC);
CRect  rcItem(lpDrawItemStruct -> rcItem); 
int    nItem = lpDrawItemStruct ->itemID;
int    nSavedDC = pDC->SaveDC();
CRect                        rcBounds,rcLabel;
LV_ITEM lvi;
lvi.mask = LVIF_IMAGE | LVIF_STATE;
lvi.iItem = nItem;
lvi.iSubItem = 0;
lvi.stateMask = 0xFFFF;
theCtrl.GetItem(&lvi);
theCtrl.GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
theCtrl.GetItemRect(nItem, rcLabel, LVIR_LABEL);
    CString sLabel = theCtrl.GetItemText( nItem, 0 );
  //  theCtrl.SetItemText( nItem, 0 ,sLabel);
CRect rcClient, rcRow = rcItem;
GetClientRect(&rcClient);
rcLabel.right = rcClient.right;
pDC->SetTextColor(RGB(0,0,0));
pDC->FillRect(rcLabel, &CBrush(nItem%2 ? RGB(115,100,115):
  RGB(115,125,122)));//::GetSysColor(COLOR_WINDOW)
pDC->RestoreDC( nSavedDC );}

解决方案 »

  1.   

    pDC->FillRect(rcLabel, &CBrush(nItem%2 ? RGB(115,100,115) : RGB(115,125,122)));
    这不是填充了背景吗,下面就要绘制内容了:
    pDC->TextOut(...);
      

  2.   

    http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/custdraw/custdraw.asp.....
    case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
            SelectObject(lplvcd->nmcd.hdc,
                         GetFontForSubItem(lplvcd->nmcd.dwItemSpec,
                                           lplvcd->nmcd.lItemlParam,
                                           lplvcd->iSubItem));
            lplvcd->clrText = GetColorForSubItem(lplvcd->nmcd.dwItemSpec,
                                                 lplvcd->nmcd.lItemlParam,
                                                 lplvcd->iSubItem));
            lplvcd->clrTextBk = GetBkColorForSubItem(lplvcd->nmcd.dwItemSpec,
                                                     lplvcd->nmcd.lItemlParam,
                                                     lplvcd->iSubItem));/* This notification is received only if you are in report mode and
    returned CDRF_NOTIFYSUBITEMDRAW in the previous step. At
    this point, you can change the background colors for the
    subitem and return CDRF_NEWFONT.*/        ...
            return CDRF_NEWFONT;    ....
      

  3.   

    上面那位MVP兄使用的是CustomDraw,与OwnerDraw是不同的,需要注意。
      

  4.   

    ͨ¹ý´¦ÀíNM_CUSTOMDRAW£¬¿ÉÒÔʵÏÖÄãµÄ¹¦ÄÜ£¡µ«ÊÇNM_CUSTOMDRAWÔÚClass WizardÖÐÓпÉÄÜ¿´²»µ½£¬²»ÓùÜËû£¬Ö±½Ó°´ÕÕСÃæµÄ·½·¨Ìí¼Ó´¦Àí¹ý³Ì¼´¿É£¡1. ÔÚÏûÏ¢Ó³Éä±íÖÐ
       BEGIN_MESSAGE_MAP(CIHISSERVERView, CListView)
    //{{AFX_MSG_MAP(CIHISSERVERView)
    ...
    ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
             ...
    //}}AFX_MSG_MAP
    // Standard printing commands
    END_MESSAGE_MAP()2.ÔÚÍ·ÎļþÖÐ
      afx_msg void OnCustomDraw(NMHDR*, LRESULT*);3.ÔÚcppÎļþÖÐ
    void CMyView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
    {
    // TODO: ÔÚ´ËÌí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂë
    // Contains information specific to an NM_CUSTOMDRAW 
    // notification message sent by a list-view control.
    // mean:draw each item.set txt color,bkcolor....
    NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)(pNMHDR); // Take the default processing unless we set this to something else below.
    *pResult = CDRF_NEWFONT; // First thing - check the draw stage. If it's the control's prepaint
    // stage, then tell Windows we want messages for every item.
    if ( pLVCD->nmcd.dwDrawStage==CDDS_PREPAINT)
    {
    *pResult = CDRF_NOTIFYITEMDRAW;
    }
    // This is the notification message for an item.  We'll request
    // notifications before each subitem's prepaint stage.
    else if ( pLVCD->nmcd.dwDrawStage==CDDS_ITEMPREPAINT ) 
    {
    COLORREF   m_crTextBk , m_clrText;
    int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);

    CListCtrl &m_list = GetListCtrl();
    CString str1 = m_list.GetItemText(nItem ,15);

    bool bDBImplFail = false ;
    if (pLVCD->nmcd.dwItemSpec%2==0)
    {
    m_crTextBk = RGB(253,241,249) ;
    m_clrText  = RGB(0,0,0) ;
    }
    else
    {
    m_crTextBk =  RGB(51,153,255);
    m_clrText  =  RGB(0,0,0);
    }

    pLVCD->clrTextBk = m_crTextBk;
    pLVCD->clrText = m_clrText;
    *pResult = CDRF_DODEFAULT;
    }
    }