Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Custom drawn vertical tree control

, 14 Oct 2005
A CTreeCtrl derived class which is both: a normal CTreeCtrl or a fully custom drawn vertical tree control.
// VerticalTreeDlg.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "VerticalTree.h"
#include "VerticalTreeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg-Dialogfeld f�r Anwendungsbefehl "Info"

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialogfelddaten
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// Vom Klassenassistenten generierte �berladungen virtueller Funktionen
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV-Unterst�tzung
	//}}AFX_VIRTUAL

// Implementierung
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// Keine Nachrichten-Handler
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVerticalTreeDlg Dialogfeld

CVerticalTreeDlg::CVerticalTreeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVerticalTreeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVerticalTreeDlg)
		// HINWEIS: Der Klassenassistent f�gt hier Member-Initialisierung ein
	//}}AFX_DATA_INIT
	// Beachten Sie, dass LoadIcon unter Win32 keinen nachfolgenden DestroyIcon-Aufruf ben�tigt
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVerticalTreeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVerticalTreeDlg)
	DDX_Control(pDX, IDC_TREE_MYTREE, m_TestTree);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVerticalTreeDlg, CDialog)
	//{{AFX_MSG_MAP(CVerticalTreeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUT_CUSTOM, OnButCustom)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVerticalTreeDlg Nachrichten-Handler

BOOL CVerticalTreeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Hinzuf�gen des Men�befehls "Info..." zum Systemmen�.

	// IDM_ABOUTBOX muss sich im Bereich der Systembefehle befinden.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{	
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Symbol f�r dieses Dialogfeld festlegen. Wird automatisch erledigt
	//  wenn das Hauptfenster der Anwendung kein Dialogfeld ist
	SetIcon(m_hIcon, TRUE);			// Gro�es Symbol verwenden
	SetIcon(m_hIcon, FALSE);		// Kleines Symbol verwenden
	
	// ZU ERLEDIGEN: Hier zus�tzliche Initialisierung einf�gen
	TV_INSERTSTRUCT TreeItem;

	TreeItem.hParent = NULL; 
	TreeItem.hInsertAfter = TVI_LAST ;
	TreeItem.itemex.mask = TVIF_TEXT | TVIF_CHILDREN; 
	TreeItem.itemex.pszText = "Root Node 1";
	TreeItem.itemex.cChildren = 1;
	TreeItem.hParent = m_TestTree.InsertItem(&TreeItem);
	FillTree(TreeItem, 4);

	TreeItem.hParent = NULL; 
	TreeItem.hInsertAfter = TVI_LAST;
	TreeItem.itemex.mask = TVIF_TEXT | TVIF_CHILDREN; 
	TreeItem.itemex.pszText = "Root Node 2";
	TreeItem.itemex.cChildren = 1;
	TreeItem.hParent = m_TestTree.InsertItem(&TreeItem);
	FillTree(TreeItem, 4);


	m_TestTree.Expand( TreeItem.hParent, TVE_EXPAND );
	return TRUE;  // Geben Sie TRUE zur�ck, au�er ein Steuerelement soll den Fokus erhalten
}

void CVerticalTreeDlg::FillTree(TV_INSERTSTRUCT &TreeItem, int depth)
{
	char name[256];
	TV_INSERTSTRUCT curTreeItem;
	TV_INSERTSTRUCT parent;

	curTreeItem.hParent = TreeItem.hParent; 
	curTreeItem.itemex.mask = TVIF_TEXT; 
	curTreeItem.itemex.cChildren = 0;
	curTreeItem.hInsertAfter = TVI_FIRST;

	for (int i = 0; i < 4; i++)
	{
		if (rand() < RAND_MAX / 4)
			sprintf(name, "Child some longer name ...%d.%d", 4-depth, i);
		else
			sprintf(name, "Child %d.%d", 4-depth, i);

		curTreeItem.itemex.pszText = name;
		parent.hParent = m_TestTree.InsertItem( &curTreeItem );
		if (depth > 0)
			FillTree(parent, depth-1);
	}
}


void CVerticalTreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// Wollen Sie Ihrem Dialogfeld eine Schaltfl�che "Minimieren" hinzuf�gen, ben�tigen Sie 
//  den nachstehenden Code, um das Symbol zu zeichnen. F�r MFC-Anwendungen, die das 
//  Dokument/Ansicht-Modell verwenden, wird dies automatisch f�r Sie erledigt.

void CVerticalTreeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // Ger�tekontext f�r Zeichnen

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

		// Symbol in Client-Rechteck zentrieren
		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;

		// Symbol zeichnen
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// Die Systemaufrufe fragen den Cursorform ab, die angezeigt werden soll, w�hrend der Benutzer
//  das zum Symbol verkleinerte Fenster mit der Maus zieht.
HCURSOR CVerticalTreeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVerticalTreeDlg::OnButCustom() 
{
	m_TestTree.SetCustomDrawn(!m_TestTree.GetCustomDrawn());
	m_TestTree.Invalidate();
}

void CVerticalTreeDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);

	if (!IsWindowVisible())
		return;	

	m_TestTree.MoveWindow(0, 30, cx, cy-40);
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Doga Arinir
Chief Technology Officer W3L
Germany Germany
-Since 1th August 2007: Chief Technology Officer of W3L
-2002/08/01-2007/07/31: PhD student
-1997/10/15-2002/07/31: Studied Electrical Engineering and Computer Science

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 14 Oct 2005
Article Copyright 2005 by Doga Arinir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid