怎么用CMFClistCtrl去改变输入的文本颜色?

wmjhl 2014-12-23 04:40:00
只说用CMFCListCtrl类可以改变每个单元格的颜色,网上找不到例子,有没有人知道的?
...全文
238 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
vocanicy 2014-12-24
  • 打赏
  • 举报
回复
引用 9 楼 wmjhl 的回复:
To:wxhxj0268 兄: 那个CMFCListCtrl::OnGetCellTextColor(nRow, nColum)函数,从字面意思理解是获得单元格文本颜色,怎么会是设置文本颜色呢?
OnGetCellBkColor这个是背景颜色 两个颜色都可以修改
笨笨仔 2014-12-24
  • 打赏
  • 举报
回复
引用 9 楼 wmjhl 的回复:
To:wxhxj0268 兄: 那个CMFCListCtrl::OnGetCellTextColor(nRow, nColum)函数,从字面意思理解是获得单元格文本颜色,怎么会是设置文本颜色呢?
当初我也有此疑问,但结果就是这样。这个Get是对系统而言,而本程序就是利用这个虚函数,修改了系统需要获得的结果,从而达到修改颜色的目的。
wmjhl 2014-12-23
  • 打赏
  • 举报
回复
To:wxhxj0268 兄: 那个CMFCListCtrl::OnGetCellTextColor(nRow, nColum)函数,从字面意思理解是获得单元格文本颜色,怎么会是设置文本颜色呢?
笨笨仔 2014-12-23
  • 打赏
  • 举报
回复
以下片断供参考
.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;
}

实现指定行变色效果
wmjhl 2014-12-23
  • 打赏
  • 举报
回复
引用 5 楼 VisualEleven 的回复:
你可以重写CListCtrl类,添加处理虚函数DrawItem可以实现你想要的功能。
现在不只是想改变一行或一列的文本颜色,是想要改变具体一个单元格的文本颜色,有什么好的方法?
schlafenhamster 2014-12-23
  • 打赏
  • 举报
回复

//////////////////////////////////////////////////////////////
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;
}
Eleven 2014-12-23
  • 打赏
  • 举报
回复
你可以重写CListCtrl类,添加处理虚函数DrawItem可以实现你想要的功能。
jacksonfan 2014-12-23
  • 打赏
  • 举报
回复
引用 3 楼 wmjhl 的回复:
还有就是我重载了CMFCListCtrl这个类后,所有的成员函数还都是CListCtrl类的成员函数,并没出现属于CMFCListCtrl的成员函数
那要自己写的 CListCtrl::SetTextBkColor Sets the background color of text in a list view control.
wmjhl 2014-12-23
  • 打赏
  • 举报
回复
还有就是我重载了CMFCListCtrl这个类后,所有的成员函数还都是CListCtrl类的成员函数,并没出现属于CMFCListCtrl的成员函数
wmjhl 2014-12-23
  • 打赏
  • 举报
回复
引用 1 楼 jacksonfan 的回复:
CListCtrl::SetBkColor Sets the background color of the list view control. CListCtrl::SetTextColor Sets the text color of a list view control. CListCtrl类成员类找找
你这个只能改全部的,我想改具体行和列的字体颜色
jacksonfan 2014-12-23
  • 打赏
  • 举报
回复
CListCtrl::SetBkColor Sets the background color of the list view control. CListCtrl::SetTextColor Sets the text color of a list view control. CListCtrl类成员类找找

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧