vc treectrl

p164
// Ex040203Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Ex040203.h"
#include "Ex040203Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

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

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
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)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx040203Dlg dialog

CEx040203Dlg::CEx040203Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEx040203Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEx040203Dlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

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

BEGIN_MESSAGE_MAP(CEx040203Dlg, CDialog)
	//{{AFX_MSG_MAP(CEx040203Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx040203Dlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	




	HTREEITEM hItem;//记录节点位置
	hItem=m_treeCtrl.InsertItem("文学爱好");//插在根节点
	m_treeCtrl.InsertItem("古诗",hItem);//插在hItem节点
	m_treeCtrl.InsertItem("散文",hItem);//插在hItem节点

	hItem=m_treeCtrl.InsertItem("体育爱好");//插在根节点
	m_treeCtrl.InsertItem("乒乓球",hItem);//插在hItem节点
	m_treeCtrl.InsertItem("足球",hItem);//插在hItem节点

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEx040203Dlg::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;

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

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEx040203Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEx040203Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
}

void CEx040203Dlg::OnOK() 
{
	// TODO: Add extra validation here
	
CString strMess="你的爱好有\n------------";
HTREEITEM hItem=m_treeCtrl.GetFirstVisibleItem();//取得tree的第一个节点
while (NULL!=hItem)//遍历所有节点
{
if (m_treeCtrl.GetCheck(hItem))//获取节点选中状态
{

	strMess+="\n";
	strMess+=m_treeCtrl.GetItemText(hItem);//取得本节点的文字
}
	hItem=m_treeCtrl.GetNextVisibleItem(hItem);//取得下一个节点
}

AfxMessageBox(strMess);
}

派生类class CCheckTreeCtrl : public CTreeCtrl,为此类添加了WM_LBUTTONDOWN和WM_KEYDOWN消息,使得子节点可以响应父节点的选中于取消选中
// CheckTreeCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "Ex040203.h"
#include "CheckTreeCtrl.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCheckTreeCtrl

CCheckTreeCtrl::CCheckTreeCtrl()
{
}

CCheckTreeCtrl::~CCheckTreeCtrl()
{
}


BEGIN_MESSAGE_MAP(CCheckTreeCtrl, CTreeCtrl)
	//{{AFX_MSG_MAP(CCheckTreeCtrl)
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCheckTreeCtrl message handlers

void CCheckTreeCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default

	CTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
	if (nChar==' ')//使空格键选中或取消
	{
		HTREEITEM hItem=GetSelectedItem();//取得当前选择的项目
		TravelChild(hItem);//取得当前选择的项目
	}

}

void CCheckTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CTreeCtrl::OnLButtonDown(nFlags, point);


	HTREEITEM hItem =HitTest(point,NULL);//取得当前鼠标选择的项目
	TravelChild(hItem);
}


void CCheckTreeCtrl::TravelChild(HTREEITEM hItem)
{
	if (NULL==hItem) return;

	bool bCheck=GetCheck(hItem);//取得当前项的当前选择状态
	HTREEITEM hChildItem=GetChildItem(hItem);//取得当前项的子项
	while (NULL!=hChildItem)
	{
	SetCheck(hChildItem,bCheck);//选中或不选
	TravelChild(hChildItem);//递归当前项
	hChildItem=GetNextSiblingItem(hChildItem);//取得下一项
	}
}

1.添加一个treectrl控件,为其关联CtreeCtrl类型的变量(对象)m_treeCtrl
2.使用m_treeCtrl.InsertItem添加节点
3.
使用m_treeCtrl.GetFirstVisibleItem();取得第一个节点
使用m_treeCtrl.GetItemText(hItem);取得某个节点的text
使用m_treeCtrl.GetNextVisibleItem(hItem);取得下一个可视节点
使用m_treeCtrl.GetCheck(hItem)取得节点的状态等
4.添加派生类class CCheckTreeCtrl : public CTreeCtrl,为此类添加了WM_LBUTTONDOWN和WM_KEYDOWN消息,使得子节点可以响应父节点的选中于取消选中
5.将对象m_treeCtrl的类型改为CCheckTreeCtrl

CTreeCtrl Class Members

Construction
Attributes
Operations

Construction

CTreeCtrl Constructs a CTreeCtrl object.
Create Creates a tree view control and attaches it to a CTreeCtrl object.

Attributes

GetCount Retrieves the number of tree items associated with a tree view control.
GetIndent Retrieves the offset (in pixels) of a tree view item from its parent.
SetIndent Sets the offset (in pixels) of a tree view item from its parent.
GetImageList Retrieves the handle of the image list associated with a tree view control.
SetImageList Sets the handle of the image list associated with a tree view control.
GetNextItem Retrieves the next tree view item that matches a specified relationship.
ItemHasChildren Returns nonzero if the specified item has child items.
GetChildItem Retrieves the child of a specified tree view item.
GetNextSiblingItem Retrieves the next sibling of the specified tree view item.
GetPrevSiblingItem Retrieves the previous sibling of the specified tree view item.
GetParentItem Retrieves the parent of the specified tree view item.
GetFirstVisibleItem Retrieves the first visible item of the specified tree view item.
GetNextVisibleItem Retrieves the next visible item of the specified tree view item.
GetPrevVisibleItem Retrieves the previous visible item of the specified tree view item.
GetSelectedItem Retrieves the currently selected tree view item.
GetDropHilightItem Retrieves the target of a drag-and-drop operation.
GetRootItem Retrieves the root of the specified tree view item.
GetItem Retrieves the attributes of a specified tree view item.
SetItem Sets the attributes of a specified tree view item.
GetItemState Returns the state of an item.
SetItemState Sets the state of an item.
GetItemImage Retrieves the images associated with an item.
SetItemImage Associates images with an item.
GetItemText Returns the text of an item.
SetItemText Sets the text of an item.
GetItemData Returns the 32-bit application-specific value associated with an item.
SetItemData Sets the 32-bit application-specific value associated with an item.
GetItemRect Retrieves the bounding rectangle of a tree view item.
GetEditControl Retrieves the handle of the edit control used to edit the specified tree view item.
GetVisibleCount Retrieves the number of visible tree items associated with a tree view control.
GetToolTips Retrieves the handle to the child ToolTip control used by a tree view control.
SetToolTips Sets a tree view control's child ToolTip control.
GetBkColor Retrieves the current background color of the control.
SetBkColor Sets the background color of the control.
GetItemHeight Retrieves the current height of the tree view items.
SetItemHeight Sets the height of the tree view items.
GetTextColor Retrieves the current text color of the control.
SetTextColor Sets the text color of the control.
SetInsertMark Sets the insertion mark in a tree view control.
GetCheck Retrieves the check state of a tree control item.
SetCheck Sets the check state of a tree control item.
GetInsertMarkColor Retrieves the color used to draw the insertion mark for the tree view.
SetInsertMarkColor Sets the color used to draw the insertion mark for the tree view.

Operations

InsertItem Inserts a new item in a tree view control.
DeleteItem Deletes a new item in a tree view control.
DeleteAllItems Deletes all items in a tree view control.
Expand Expands, or collapses, the child items of the specified tree view item.
Select Selects, scrolls into view, or redraws a specified tree view item.
SelectItem Selects a specified tree view item.
SelectDropTarget Redraws the tree item as the target of a drag-and-drop operation.
SelectSetFirstVisible Selects a specified tree view item as the first visible item.
EditLabel Edits a specified tree view item in-place.
HitTest Returns the current position of the cursor related to the CTreeCtrl object.
CreateDragImage Creates a dragging bitmap for the specified tree view item.
SortChildren Sorts the children of a given parent item.
EnsureVisible Ensures that a tree view item is visible in its tree view control.
SortChildrenCB Sorts the children of a given parent item using an application-defined sort function.

http://download.csdn.net/detail/luck_good/3787056

你可能感兴趣的:(vc treectrl)