VC:重载CListCtrl::DrawItem

Step1: Owner Draw Fixed :true

Step2: 重载DrawItem函数

 

void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
    CDC* pDC = CDC::FromHandle(lpDIS->hDC);

  
    LVITEM lvi = {0};
    lvi.mask = LVIF_STATE;//|LVIF_IMAGE;
    lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ;
    lvi.iItem = lpDIS->itemID;
    BOOL bGet = GetItem(&lvi);

    BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||((lvi.state & LVIS_SELECTED) &&
        ((GetFocus() == this)|| (GetStyle() & LVS_SHOWSELALWAYS))));

    // 画文本背景
    CRect rcBack = lpDIS->rcItem;
    pDC->SetBkMode(TRANSPARENT);
    if( bHighlight )
    {
        pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
        pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); 
        pDC->FillRect(rcBack, &CBrush(::GetSysColor(COLOR_HIGHLIGHT))); 
        
    }
    else
    {
        if (lpDIS->itemID%2 ==0)
        {
            pDC->SetTextColor(0x0C0F80);
            //pDC->SetTextColor(RGB(250,0,0));
            pDC->FillRect(rcBack, &CBrush(0xEEF1E3));
        }
        else
        {
            pDC->SetTextColor(RGB(0,128,255));
            pDC->FillRect(rcBack, &CBrush(0xE3e3e3));
        }
    }

    //设置字体颜色
    CString str;

    //得到焦点大小
    CRect rcFocus = lpDIS->rcItem;
    rcFocus.DeflateRect(1,1,1,1); //将CRect的边向其中心移动来缩小它

    if (lpDIS->itemAction & ODA_DRAWENTIRE)
    {
        //写文本
        CString szText;
        int nCollumn = GetHeaderCtrl()->GetItemCount();//列数
        for (int i = 0; i < GetHeaderCtrl()->GetItemCount(); i++)
        { //循环得到文本
            CRect rcItem;
            if ( !GetSubItemRect(lpDIS->itemID, i, LVIR_LABEL, rcItem ))
                continue;
            szText = GetItemText( lpDIS->itemID, i );

            rcItem.left += 5; rcItem.right -= 1;
            pDC->DrawText(szText, lstrlen(szText), &rcItem, DT_LEFT |DT_VCENTER|DT_NOPREFIX|DT_SINGLELINE);
        }
    }

}

你可能感兴趣的:(c,image)