Clistctrl mfc C++,派生类


我创建了 CListCtrl 的派生类,但我有一个问题..当我单击一行时,该行不会变成蓝色..我不明白为什么..我的派生类:

<pre>
#include "stdafx.h"
#include "Domino6.h"
#include "MyListCtrl.h"
#include "MyHeader.h"


// MyListCtrl

IMPLEMENT_DYNAMIC(MyListCtrl, CListCtrl)

MyListCtrl::MyListCtrl()
{
	//m_bList = false;
}

MyListCtrl::~MyListCtrl()
{
}


BEGIN_MESSAGE_MAP(MyListCtrl, CListCtrl)
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
END_MESSAGE_MAP()


void MyListCtrl::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	CHeaderCtrl* pHeader = NULL;
	pHeader = GetHeaderCtrl();
	int nWidth;


	if (m_bList == false)
		nWidth = GetColumnWidth(1);

	else
		nWidth = GetColumnWidth(0);
	
	if (pHeader != NULL)
	{
		//int nWidth = GetColumnWidth(1);
		
		//m_HeaderCtrl.m_nCol = m_nCol;
		VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd)); // m_HeaderCtrl is the new wrapper object
	}
	CListCtrl::PreSubclassWindow();
}
// MyListCtrl message handlers


void MyListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVCUSTOMDRAW *pCD = (NMLVCUSTOMDRAW*)pNMHDR;
	// By default set the return value to do the default behavior.
	*pResult = CDRF_DODEFAULT;

	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: Add your control notification handler code here
	*pResult = CDRF_DODEFAULT;

	int n_width = 0;

	if(m_bList == false)

	n_width = GetColumnWidth(1);

	else

	n_width = GetColumnWidth(0);


	int NumberOfColumns = m_HeaderCtrl.GetItemCount();

	m_HeaderCtrl.m_nwidth_  = GetColumnWidth(0);
	m_HeaderCtrl.m_nwidth1_ = GetColumnWidth(1);
	m_HeaderCtrl.m_nwidth1 = GetColumnWidth(2);
	m_HeaderCtrl.m_nwidth2 = GetColumnWidth(3);
	m_HeaderCtrl.m_nwidth3 = GetColumnWidth(4);
	m_HeaderCtrl.m_nwidth4 = GetColumnWidth(5);
	m_HeaderCtrl.m_nwidth5 = GetColumnWidth(6);

	//obtain row and column of item
	m_nRow = pCD->nmcd.dwItemSpec;
	m_nCol = pCD->iSubItem;

	//Remove standard highlighting of selected (sub)item.
	pCD->nmcd.uItemState = CDIS_DEFAULT;

	switch (pCD->nmcd.dwDrawStage)
	{
	case  CDDS_PREPAINT:  // First stage (for the whole control)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	break;

	case CDDS_ITEMPREPAINT:
	{
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
	}
	break;

	case  CDDS_ITEMPREPAINT | CDDS_SUBITEM: // Stage three 
	{
		if (m_bList == true)
		{
			{
				CDC* pDC = CDC::FromHandle(pNMCD->hdc);

				CBrush brushBlue(RGB(255, 192, 203));// inner color blue.
				CBrush* pOldBrush = pDC->SelectObject(&brushBlue);

				// create and select a thick, black pen
				CPen penBlack;
				penBlack.CreatePen(PS_SOLID, 3, RGB(0, 0, 0));// red color with width 3
				CPen* pOldPen = pDC->SelectObject(&penBlack);

				for (int ij = 0; ij < 50; ij++)
				{
					CRect rect1(0, 24 + (17 * (ij)), n_width, 42 + (17 * (ij)));  //CRect(a,b,c,d) come b metto il valore d del rettangolo precedente, invece d = b+18; 
					pDC->Rectangle(rect1);

					if (ij < m_arrStr.GetSize())
					pDC->DrawText(m_arrStr[ij], CRect(rect1), DT_BOTTOM);
				}

				pDC->SelectObject(pOldBrush);
				pDC->SelectObject(pOldPen);
			}
			*pResult = CDRF_NOTIFYPOSTPAINT;
		}
		break;
	case CDDS_ITEMPOSTPAINT | CDDS_SUBITEM: // Stage four (called for each subitem of the focused item)
	{

	}
	break;

	default:// it wasn't a notification that was interesting to us.
	{
		*pResult = CDRF_DODEFAULT;
	}
	break;
	}
  }
}

我尝试过的:

我发现如果我使用其他未派生的列表,我就不会遇到这个问题..你能帮助我吗?

解决方案1

如果您自己绘制单个项目(根据我的示例代码),您还需要返回 CDRF_NEWFONT 加工时 CDDS_ITEMPREPAINT | CDDS_SUBITEM。 Microsoft 示例中也显示了这一点: 使用自定义绘制- Win32 应用程序微软学习[^]。

解决方案2

这篇优秀的文章描述了在 Listcontrols 中绘图的许多有趣的细节 使用自定义绘制在列表控件中完成的漂亮事情
所以你不仅可以找到一些解决方案,还可以找到一些进一步的灵感。

コメント

タイトルとURLをコピーしました