www.gusucode.com > 学习VC++的一些小实例源码程序 > 学习VC++的一些小实例(界面、游戏、简单图形处理、算法、数据库操作)源码程序/code/sample/ch06/Treewing/TreewingView.cpp

    //Download by http://www.NewXing.com
// TreewingView.cpp : implementation of the CTreewingView class
//

#include "stdafx.h"
#include "Treewing.h"

#include "TreewingDoc.h"
#include "TreewingView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTreewingView

IMPLEMENT_DYNCREATE(CTreewingView, CTreeView)

BEGIN_MESSAGE_MAP(CTreewingView, CTreeView)
	//{{AFX_MSG_MAP(CTreewingView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreewingView construction/destruction

CTreewingView::CTreewingView()
{
	cim = 0;

}

CTreewingView::~CTreewingView()
{
	if (cim)
	{
		delete cim;
		cim = 0;
	}

}

BOOL CTreewingView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTreewingView drawing

void CTreewingView::OnDraw(CDC* pDC)
{
	CTreewingDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CTreewingView::OnInitialUpdate()
{
	CTreeView::OnInitialUpdate();
	GetTreeCtrl().ModifyStyle(NULL,
		TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT);
	
	InsertBitmaps();
}

/////////////////////////////////////////////////////////////////////////////
// CTreewingView diagnostics

#ifdef _DEBUG
void CTreewingView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CTreewingView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

CTreewingDoc* CTreewingView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTreewingDoc)));
	return (CTreewingDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTreewingView message handlers

void CTreewingView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	GetTreeCtrl().DeleteAllItems();
	InsertAnimals();
}
void CTreewingView::InsertBitmaps()
{
	// Create the CImageList. It's destroyed in the destructor.
	cim = new CImageList();
	cim->Create(BITMAP_WIDTH, BITMAP_HEIGHT, TRUE, NUM_BITMAPS, 0);

	CBitmap bitmap;

	// Load the bitmaps and add them to the image list.
	for (int i = IDB_AMPHIBIAN; i <= IDB_REPTILE_SELECTED; i++)
	{
		bitmap.LoadBitmap(i);
		cim->Add(&bitmap, (COLORREF)0xFFFFFF);
		bitmap.DeleteObject();
	}

	// Associate the image list with the tree control.
	GetTreeCtrl().SetImageList(cim, TVSIL_NORMAL);
}

void CTreewingView::InsertAnimals()
{
	static char * classes[] = {  "amphibians", "birds", "fishes",
								"mammals", "reptiles", NULL };
	static char * types[][4] = { "frogs", "toads", "salamanders", NULL,
								"eagles", "owls", "falcons", NULL,
								"trout", "perch", "bass", NULL,
								"bears", "whales", "rodents", NULL,
								"snakes", "turtles", "lizards", NULL };

	HTREEITEM hSubTree;
	int ImageListIndex = 0;

	CTreeCtrl & ctc = GetTreeCtrl();

	for (int r = 0; r < 5; r++)
	{
		hSubTree = ctc.InsertItem(classes[r], TVI_ROOT, TVI_SORT);
		ctc.SetItemImage(hSubTree, ImageListIndex, ImageListIndex + 1);
		// The string from the classes array is placed at the root...
		// and the newly-inserted node serves as parent to the types.
		InsertNodes(hSubTree, types[r], ImageListIndex, ImageListIndex + 1);
		ImageListIndex += 2;
	}

	static char * beartypes[] = { "grizzly", "polar", "black", NULL };
	HTREEITEM hSubTree2;

	// Now that the tree is built, the bears node will have children
	// added to it. This requires 2 lookup operations. 
	hSubTree = FindNode(ctc.GetRootItem(), classes[3]);
	if (NULL != hSubTree)
	{
		hSubTree2 = FindNode(hSubTree, types[3][0]);
		if (NULL != hSubTree2)
			InsertNodes(hSubTree2, beartypes, MAMMAL, MAMMAL + 1);
	}
}

// The function iterates over the children of ParentNode looking for a match.
// If ParentNode is NULL, the search starts in the root, otherwise it starts
// in the designated node. Returns NULL if not found, the HTREEITEM if found.
HTREEITEM CTreewingView::FindNode(const HTREEITEM ParentNode,
										const CString &str) const
{
	CTreeCtrl & ctc = GetTreeCtrl();
	HTREEITEM node;
	CString s;

	if (NULL == ParentNode)
		node = ctc.GetRootItem();
	else
		if (ctc.GetRootItem() == ParentNode)
			node = ParentNode;
		else
			node = ctc.GetChildItem(ParentNode);
	
	while (node != NULL)
	{
		s = ctc.GetItemText(node);
		// Halt the search when we find what we're looking for.
		if (0 == s.CompareNoCase(str))
			return node;
		node = ctc.GetNextItem(node, TVGN_NEXT);
	}
	// If we get to here, string was never found, and node == NULL.
	return node;
}

// This function inserts the array of strings pointed to by the 2nd
// argument into the tree at the node represented by the first
// argument. All nodes have the same 2 image IDs.
void CTreewingView::InsertNodes(const HTREEITEM hSubTree, char ** AnimalClass,
							const int ImageID, const int SelectedImageID)
{
	HTREEITEM node;
	CTreeCtrl & ctc = GetTreeCtrl();
	
	int i = 0;
	while (AnimalClass[i])
	{
		// Insert a node as a child of hSubTree and set its images.
		node = ctc.InsertItem(AnimalClass[i++], hSubTree, TVI_SORT);
		ctc.SetItemImage(node, ImageID, SelectedImageID);
	}
}