Verán, yo antes tenía una clase derivada de CTreeCtrl que tiene como objetivo “coloerar” los nodos de un árbol. Por ejemplo, si el contenido del nodo es “<castellano>” se mostrará en amarillo, si el contenido es “<english>” se mostrará en verde, etc… Y esta clase funcionaba correctamente.
Pero ahora la he adaptado un poco porque el contenido de los nodos ha variado, pero hay que seguir coloreándolos. Pero ahora se que en un bucle infinito pintando el primer nodo del árbol, y no sé por qué, ya que he seguido paso por paso la clase que tenía.
Les pongo un resumen de la nueva clase CColorTreeFamConcepto.
// ColorTreeFamConcepto.cpp: implementation of the CColorTreeFamConcepto class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "familiaConceptos.h"
#include "ColorTreeFamConcepto.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CColorTreeFamConcepto::CColorTreeFamConcepto(){}
CColorTreeFamConcepto::~CColorTreeFamConcepto(){}
BEGIN_MESSAGE_MAP(CColorTreeFamConcepto, CTreeCtrl)
//{{AFX_MSG_MAP(CColorTreeFamConcepto)
ON_WM_DRAWITEM()
ON_WM_PAINT()
ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndlabeledit)
ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT, OnBeginlabeledit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorTreeFamConcepto message handlers
void CColorTreeFamConcepto::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC );
RECT clientrect;
GetClientRect(&clientrect);
int nIndent = GetIndent();
CRect rect = lpDrawItemStruct->rcItem;
rect.left = rect.left + nIndent;
rect.right = clientrect.right;
DrawItemText(pDC,&rect,GetItemText((HTREEITEM)lpDrawItemStruct->hwndItem),(HTREEITEM) lpDrawItemStruct->hwndItem);
CColorTreeFamConcepto::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
void CColorTreeFamConcepto::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
HTREEITEM hItem;
CWnd::DefWindowProc(WM_PAINT, (WPARAM)dc.m_hDC,0);
m_nScrollPix = GetScrollPos(SB_HORZ);
dc.SetBkMode(TRANSPARENT);
for(hItem = GetFirstVisibleItem(); hItem; hItem = GetNextVisibleItem(hItem)){
DRAWITEMSTRUCT dis;
dis.hDC = dc.m_hDC;
CRect Rect;
dis.hwndItem = (HWND)hItem;
GetItemRect(hItem, Rect, FALSE);
dis.rcItem = Rect;
OnDrawItem(0, &dis);
}
// Do not callCColorTreeFamConcepto::OnPaint() for painting messages
}
BOOL CColorTreeFamConcepto::isFamily(HTREEITEM hItem) {….}
BOOL CColorTreeFamConcepto::isCfamily(HTREEITEM hItem){….}
….
//son una serie de funciones para ver de qué tipo es el nodo,ya que dependiendo del tipo de nodo se pintará de un color u otro.
void CColorTreeFamConcepto::pintar_nodo(CDC *pDC, CRect *prect, CString str, HTREEITEM hItem, CString tiponodo, int count){
CFont* pFont = CFont::FromHandle((HFONT)::GetStockObject( DEFAULT_GUI_FONT ));
CFont* pOldFont = pDC->SelectObject(pFont);
UINT nState = GetItemState(hItem,TVIS_DROPHILITED | TVIS_SELECTED);
UINT nStyle = GetStyle();
CSize size;
CString tmp1,temp2;
CRect rect = *prect;
int indice,indice_aux1,longitud;
longitud = str.GetLength();
prect->left += 2 - m_nScrollPix;
prect->left += GetIndent()*count;
pDC->FillSolidRect(prect,RGB(255,255,255));
prect->bottom++;
prect->top++;
if(strcmp((const char*)tiponodo,"cfamily") == 0){
MessageBox("inicio Cerrar familia");
tmp1 = str.Mid(0,2);
pDC->SetTextColor(RGB(0,0,0));
pDC->DrawText(tmp1,prect,DT_LEFT);
size = pDC->GetTextExtent(tmp1);
tmp1 = str.Mid(2,6);
pDC->SetTextColor(RGB(10,143,211));
prect->left += size.cx;
pDC->DrawText(tmp1,prect,DT_LEFT);
size = pDC->GetTextExtent(tmp1);
tmp1 = str.Mid(8,1);
pDC->SetTextColor(RGB(0,0,0));
prect->left += size.cx;
pDC->DrawText(tmp1,prect,DT_LEFT);
size = pDC->GetTextExtent(tmp1);
MessageBox("fin cerrar familia");
}
else if(…){…..}
else{….}
}
void CColorTreeFamConcepto::DrawItemText(CDC *pDC, CRect *prect, CString str, HTREEITEM hItem)
{
CFont* pFont = CFont::FromHandle((HFONT)::GetStockObject( DEFAULT_GUI_FONT ));
CFont* pOldFont = pDC->SelectObject(pFont);
UINT nState = GetItemState(hItem,TVIS_DROPHILITED | TVIS_SELECTED);
int count = 0;
if(!(nState & (TVIS_DROPHILITED | TVIS_SELECTED))){
int count = 0;
HTREEITEM tmp = hItem;
while(GetParentItem(tmp)){
tmp = GetParentItem(tmp);
count++;
}
if(isFamily(hItem)) {pintar_nodo(pDC,prect,str,hItem,"family",count);}
else if(isCfamily(hItem)) {pintar_nodo(pDC,prect,str,hItem,"cfamily",count);}
else if(…){…}
else{…}
}
pDC->SelectObject(pOldFont);
}
void CColorTreeFamConcepto::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult) { ...}
void CColorTreeFamConcepto::OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult) {....}
Este es el contenido más o menos de la case CColorTreeFamConcepto.cpp.
En la clase CEditorFamilia hay una variable CColorTreeFamConcepto llamada m_familia, y también un método paint con el siguiente contenido:
void CEditorFamilia::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
}
else{
CDialog::OnPaint();
}
}
No sé donde puede estar el error para que se quede en un bucle infinito pintando el primer nodo del árbol. He seguido paso por paso todo lo que hice para crear la anterior clase que pintaba los nodos y antes no tenía este problema.
Saludos y gracias de antemano.



