如何用CMFClistCtrl去改变输入的文本颜色
怎么用CMFClistCtrl去改变输入的文本颜色?
只说用CMFCListCtrl类可以改变每个单元格的颜色,网上找不到例子,有没有人知道的?
------解决思路----------------------
你可以重写CListCtrl类,添加处理虚函数DrawItem可以实现你想要的功能。
------解决思路----------------------
------解决思路----------------------
以下片断供参考
.H
.CPP
实现指定行变色效果

------解决思路----------------------
OnGetCellBkColor这个是背景颜色
两个颜色都可以修改
只说用CMFCListCtrl类可以改变每个单元格的颜色,网上找不到例子,有没有人知道的?
------解决思路----------------------
你可以重写CListCtrl类,添加处理虚函数DrawItem可以实现你想要的功能。
------解决思路----------------------
//////////////////////////////////////////////////////////////
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;
}
------解决思路----------------------
以下片断供参考
.H
// CMyListCtrl
class 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;
}
实现指定行变色效果
------解决思路----------------------
OnGetCellBkColor这个是背景颜色
两个颜色都可以修改