CListCtrl::SetBkColor
Sets the background color of the list view control.CListCtrl::SetTextColor
Sets the text color of a list view control.CListCtrl类成员类找找

解决方案 »

  1.   

    还有就是我重载了CMFCListCtrl这个类后,所有的成员函数还都是CListCtrl类的成员函数,并没出现属于CMFCListCtrl的成员函数
      

  2.   

    那要自己写的
    CListCtrl::SetTextBkColor
    Sets the background color of text in a list view control.
      

  3.   

    你可以重写CListCtrl类,添加处理虚函数DrawItem可以实现你想要的功能。
      

  4.   


    //////////////////////////////////////////////////////////////
    LRESULT ListViewCustomDraw(HWND hwnd, LPARAM lParam)
    {
    LPNMHDR pnmh = (LPNMHDR) lParam;
            
        if (pnmh->code != NM_CUSTOMDRAW) return 0;

    LPNMLVCUSTOMDRAW lpNMCustomDraw = (LPNMLVCUSTOMDRAW) lParam; int nResult = CDRF_DODEFAULT; 

    if (CDDS_PREPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
    {
    nResult = CDRF_NOTIFYITEMDRAW;
    }
    else if (CDDS_ITEMPREPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
    {
    nResult = CDRF_NOTIFYSUBITEMDRAW | CDRF_NOTIFYPOSTPAINT;
    }
    else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == lpNMCustomDraw->nmcd.dwDrawStage)
    {
    nResult = CDRF_SKIPDEFAULT;

    const DWORD dwStyle = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS;

    HDC hdc = lpNMCustomDraw->nmcd.hdc; 
    SetBkMode(hdc,TRANSPARENT);
    int nItem = (int)lpNMCustomDraw->nmcd.dwItemSpec; 
    int nSubItem = lpNMCustomDraw->iSubItem; 

    BOOL bItemSelected = ListView_GetItemState(hwnd, nItem, LVIS_SELECTED);

    RECT subItemRect;
    ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_BOUNDS, &subItemRect);
    //
    HBRUSH brsh=0; 
    if (bItemSelected)
    {  //OutputDebugString("bItemSelected\n");
    brsh=CreateSolidBrush(RGB(255, 128, 128));
    FillRect(hdc, &subItemRect,brsh);
    }
    else
    {// not Selected
    brsh=CreateSolidBrush(RGB(51+nItem*30, 153, 255-nItem*30));
    FillRect(hdc, &subItemRect,brsh);
    }
    if(brsh) DeleteObject(brsh);
    //
    if(nSubItem==0)
    {//OutputDebugString("bmp\n");
    RECT iconRect;
    ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_ICON, &iconRect);
    OffsetRect(&iconRect, -1, 0);
    HBITMAP oldbmp=(HBITMAP)SelectObject(g_hMemDC,g_hbmNormal);
    BitBlt(hdc,iconRect.left, iconRect.top, 16, 16,g_hMemDC,0,0,SRCCOPY);
    SelectObject(hdc,oldbmp);
    }
    //
    TCHAR szText[260];
    ListView_GetItemText(hwnd, nItem, nSubItem, szText, 260);
            OffsetRect(&subItemRect, 18, 0);
    DrawText(hdc, szText, strlen(szText), &subItemRect, dwStyle);
    return nResult;
    }
    else if (CDDS_ITEMPOSTPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
    {// draw (horizantal line)
    HPEN hpen=CreatePen(PS_SOLID,1,RGB(0,255,0));
    HPEN holdpen=(HPEN)SelectObject(lpNMCustomDraw->nmcd.hdc,hpen);
    RECT crc;
    GetClientRect(hwnd,&crc);
    RECT irc;
    ListView_GetItemRect(hwnd, lpNMCustomDraw->nmcd.dwItemSpec,&irc,LVIR_BOUNDS);
    HDC hdc = lpNMCustomDraw->nmcd.hdc; 
    MoveToEx(hdc,0,irc.top,0);
    LineTo(hdc,crc.right-crc.left,irc.top);
    SelectObject(hdc,holdpen);
    //
    return CDRF_DODEFAULT;
    }
    return nResult;
    }
      

  5.   

    以下片断供参考
    .H// CMyListCtrlclass CMyListCtrl : public CMFCListCtrl
    {
    DECLARE_DYNAMIC(CMyListCtrl)public:
    CMyListCtrl();
    virtual ~CMyListCtrl();protected:
    DECLARE_MESSAGE_MAP() /*通过虚函数可修改表的行、列的颜色和字体*/
    virtual COLORREF OnGetCellBkColor(int nRow, int nColum); // 修改背景色
    virtual COLORREF OnGetCellTextColor(int nRow, int nColum); // 修改文本色
    virtual HFONT OnGetCellFont(int nRow, int nColum, DWORD dwData = 0); // 修改字体.CPP// 修改背景色
    COLORREF CMyListCtrl::OnGetCellBkColor(int nRow, int nColum)
    {
    CMainFrame* pFm=(CMainFrame*)AfxGetMainWnd();
    CAnalysisRs232View* pView=(CAnalysisRs232View*)pFm->GetActiveView(); if(!pView->GetListIsFocus())
    {
    // 当表失去焦点时模拟选中
    if(nRow==pView->GetItem())
    {
    COLORREF crBackground = ::GetSysColor(COLOR_HIGHLIGHT); // 系统背景色
    return crBackground;
    }
    }
    // 非B通道不处理
    if(GetItemText(nRow,0)!=L"1")
    {
    return CMFCListCtrl::OnGetCellBkColor(nRow, nColum);
    }
    // 返回背景颜色
    return RGB(245, 230, 240); // 使用自定义背景色
    }
    // 修改文本色
    COLORREF CMyListCtrl::OnGetCellTextColor(int nRow, int nColum)
    {
    CMainFrame* pFm=(CMainFrame*)AfxGetMainWnd();
    CAnalysisRs232View* pView=(CAnalysisRs232View*)pFm->GetActiveView();
    // 返回偶数行或奇数行的字体颜色
    if(!pView->GetListIsFocus())
    return nRow==pView->GetItem() ? ::GetSysColor(COLOR_HIGHLIGHTTEXT) : RGB(0, 0, 0); return CMFCListCtrl::OnGetCellTextColor(nRow, nColum);
    }
    // 修改字体
    HFONT CMyListCtrl::OnGetCellFont(int nRow, int nColum, DWORD /*dwData* = 0*/)
    {
    return NULL;
    }实现指定行变色效果
      

  6.   

    To:wxhxj0268 兄:
                     那个CMFCListCtrl::OnGetCellTextColor(nRow, nColum)函数,从字面意思理解是获得单元格文本颜色,怎么会是设置文本颜色呢?
      

  7.   

    当初我也有此疑问,但结果就是这样。这个Get是对系统而言,而本程序就是利用这个虚函数,修改了系统需要获得的结果,从而达到修改颜色的目的。
      

  8.   


    OnGetCellBkColor这个是背景颜色
    两个颜色都可以修改