Click here to Skip to main content
15,885,365 members
Articles / Desktop Programming / MFC

CTreeCtrlEx - Setting Color and Font Attribute for Individual Items in Tree Control

Rate me:
Please Sign up or sign in to vote.
4.68/5 (24 votes)
26 May 20021 min read 245K   11.1K   63  
Allows to display color items and change the font style
/////////////////////////////////////////////////////////////
//	
//	Author:		Sami (M.ALSAMSAM), ittiger@ittiger.net
//
//	Filename:	ColorTreeDlg.cpp
//
//	http	 :	www.ittiger.net
//
//////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ColorTree.h"
#include "ColorTreeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
CColorTreeDlg::CColorTreeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CColorTreeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CColorTreeDlg)
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CColorTreeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CColorTreeDlg)
	DDX_Control(pDX, IDC_TREE1, m_cTree);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CColorTreeDlg, CDialog)
	//{{AFX_MSG_MAP(CColorTreeDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
BOOL CColorTreeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);
	
	HTREEITEM			hOrder[1024], hContent[1024];
	CString				sStr;

	for(int i = 0; i < 255; i++) {
		sStr.Format("Parent%d", i);
		hOrder[i] = m_cTree.InsertItem(sStr);
		for (int j = 0; j < 5; j++) {
			sStr.Format("Child%d", j);
			hContent[j] = m_cTree.InsertItem(sStr, hOrder[i]);
			m_cTree.SetItemColor(hContent[j], RGB(i, 255, 0));
		}
		m_cTree.SetItemColor(hOrder[i], RGB(255, i, 0));
	}

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
void CColorTreeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

/////////////////////////////////////////////////////////////////////////////
HCURSOR CColorTreeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Software Developer (Senior)
Germany Germany
Lion is a software engineer start working with C/C++ in 1995, after that he move to the internet developing with PERL, PHP, ASP, JSP, MySql. now he since 12 years is working on Object Oriented, Visual C++ (he could say that he speak VC++ better than his nativ language), SDK, MFC, COM, COM+, ATL, ADO, ODBC, Internet technologies, DataBase (MS-SQLServer, ORACLE), named pipe, threading... you name it.
And now he is very interesting in .NET technology, as well in Android development and maybe later in Windows Phone 7.
Lion is originally from Bulgaria, he is working right now in Germany since 2001, developing a hospital/Labor software.

Comments and Discussions