Clistctrl mfc C++, lớp dẫn xuất

lập trình


Tôi đã tạo một lớp dẫn xuất của CListCtrl, nhưng tôi gặp một vấn đề..khi tôi nhấp vào một hàng, hàng đó không chuyển sang màu xanh lam..và tôi không hiểu tại sao..lớp dẫn xuất của tôi:

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

Những gì tôi đã thử:

Tôi thấy rằng nếu tôi sử dụng một danh sách khác không có nguồn gốc thì tôi không gặp phải vấn đề này..bạn có thể giúp tôi không?

Giải pháp 1

Nếu bạn đang tự vẽ một món đồ thì (theo mã mẫu của tôi), bạn cũng cần phải quay lại CDRF_NEWFONT khi xử lý CDDS_ITEMPREPAINT | CDDS_SUBITEM. Điều này cũng được thể hiện trong ví dụ của Microsoft tại Sử dụng Custom Draw – Ứng dụng Win32 | Microsoft Tìm hiểu[^].

Giải pháp 2

Rất nhiều chi tiết thú vị với cách vẽ trong Listcontrols được mô tả trong bài viết nổi bật Những điều gọn gàng cần làm trong điều khiển danh sách bằng cách sử dụng bản vẽ tùy chỉnh.
Vì vậy, bạn có thể không chỉ tìm thấy một số giải pháp mà còn có thêm nguồn cảm hứng nào đó.

コメント

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