Clistctrl mfc C++, classe dérivée

la programmation


J’ai créé une classe dérivée de CListCtrl, mais j’ai un problème..quand je clique sur une ligne, la ligne ne devient pas bleue..et je ne comprends pas pourquoi..ma classe dérivée :

<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;
	}
  }
}

Ce que j’ai essayé :

J’ai vu que si j’utilise une autre liste qui n’est pas dérivée, je n’ai pas ce problème. pouvez-vous m’aider ?

Solution 1

Si vous peignez vous-même un seul élément, vous devez également (selon mon exemple de code) retourner CDRF_NEWFONT lors du traitement CDDS_ITEMPREPAINT | CDDS_SUBITEM. Ceci est également illustré dans l’exemple Microsoft à l’adresse Utilisation de Custom Draw – Applications Win32 | Microsoft Apprendre[^].

Solution 2

De nombreux détails intéressants sur le dessin dans Listcontrols sont décrits dans l’article exceptionnel Des choses intéressantes à faire dans les contrôles de liste à l’aide de Custom Draw.
Ainsi, vous trouverez peut-être non seulement une solution, mais aussi une source d’inspiration supplémentaire.

コメント

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