⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorlistctrl.cpp

📁 人工智能实验---求解传教士与野人问题,并画出状态图做动态演示.
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/*                                ColorListCtrl.h                            */
/*                         Author: SFr frydaysoft@gmx.de                     */
/*       这里使用了SFr frydaysoft@gmx.de编写的CColorListCtrl类,在此感谢他;   */
/*****************************************************************************/

#include "stdafx.h"
#include "ColorListCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorListCtrl
// ColorListCtrl.cpp : 实现文件

IMPLEMENT_DYNAMIC(CColorListCtrl, CListCtrl)
CColorListCtrl::CColorListCtrl()
{
}

CColorListCtrl::~CColorListCtrl()
{
	POSITION pos = m_lstItemColor.GetHeadPosition();
	while (pos)
	{
		delete []m_lstItemColor.GetNext(pos);
	}
}

BEGIN_MESSAGE_MAP(CColorListCtrl, CListCtrl)

ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)

END_MESSAGE_MAP()

void CColorListCtrl::PreSubclassWindow()
{
	// TODO: 在此添加专用代码和/或调用基类
	ModifyStyle(0, /*LVS_OWNERDRAWFIXED | */LVS_REPORT, 0);
	
	CListCtrl::PreSubclassWindow();
}

// CColorListCtrl 消息处理程序

COLORREF CColorListCtrl::SetItemColor(int nRow, int nCol, COLORREF clrItem)
{
	int nCols = GetHeaderCtrl()->GetItemCount();
	int nRows = GetItemCount();
	if (nCol >= nCols || nRow >= nRows)
		return COLORREF(-1);
	
	for (int i = m_lstItemColor.GetCount(); i < nRow + 1; i++)
	{
		COLORREF *clrCol = new COLORREF[nCols];
		ZeroMemory(clrCol, sizeof(clrCol));
		m_lstItemColor.AddTail(clrCol);
	}
	
	for (i = 0; i < nRows; i++)
	{
		if (i == nRow)
		{
			COLORREF *clrCol = m_lstItemColor.GetAt(m_lstItemColor.FindIndex(nRow));
			COLORREF clrRet = clrCol[nCol];
			clrCol[nCol] = clrItem;
			return clrRet;
		}
	}
	
	return COLORREF(-1);
}


COLORREF CColorListCtrl::SetItemColor(LVITEM* plvItem, COLORREF clrItem)
{
	if (!plvItem)
	{
		return COLORREF(0);
	}
	int nRow = plvItem->iItem;
	int nCol = plvItem->iSubItem;
	return SetItemColor(nCol, nRow, clrItem);
}

void CColorListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	
	int nRows = m_lstItemColor.GetCount();
	int nRow = int(pNMCD->nmcd.dwItemSpec);
	COLORREF *clrCol = NULL;
	if (nRows && nRow < nRows)
	{
		clrCol = m_lstItemColor.GetAt(m_lstItemColor.FindIndex(nRow));
	}
	
	switch(pNMCD->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT :
		*pResult = CDRF_NOTIFYITEMDRAW;
		return;
		
	case CDDS_ITEMPREPAINT:
		if (clrCol)
			pNMCD->clrText = clrCol[0];
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
		return;
		
	case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
		if (clrCol)
			pNMCD->clrText = clrCol[pNMCD->iSubItem];
		*pResult = CDRF_NEWFONT;
		return;
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -