MFC CreateTreeControl

 
private:
	CTreeCtrl *TreeSoft;
};

-----------------------------------------------------

// Tree1Dlg.cpp : implementation file
//

CTree1Dlg::CTree1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTree1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTree1Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	TreeSoft = new CTreeCtrl;
}

CTree1Dlg::~CTree1Dlg()
{
	delete TreeSoft;
}



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

	// 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
	
	// TODO: Add extra initialization here
	TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
		CRect(10, 10, 240, 280), this, 0x1221);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

After adding or creating a tree control, you may want to fill it with the necessary items. Each node of the control is an HTREEITEM object. To create a new node, call the CTreeCtrl::InsertItem() method. It comes in various versions. One of them is:

HTREEITEM InsertItem(LPCTSTR lpszItem,
                                    HTREEITEM hParent = TVI_ROOT,
                                    HTREEITEM hInsertAfter = TVI_LAST );

The easiest way to add an item consists of calling the InsertItem() method with a null-terminated string as argument because this is the only required argument of this version. Here is an example:

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

	// 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
	
	// TODO: Add extra initialization here
	TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
		CRect(10, 10, 240, 280), this, 0x1221);

	TreeSoft->InsertItem("Office Production");

	return TRUE;  // return TRUE  unless you set the focus to a control
}

In this case, the item would appear as the root. You can add as many nodes like that and each would appear as a root:

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

	// 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
	
	// TODO: Add extra initialization here
	TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
		CRect(10, 10, 240, 280), this, 0x1221);

	TreeSoft->InsertItem("Office Production");
	TreeSoft->InsertItem("Company Management");
	TreeSoft->InsertItem("Software Development");
	TreeSoft->InsertItem("Human Interaction");

	return TRUE;  // return TRUE  unless you set the focus to a control
}

When calling this version of the InsertItem() method, if you do not pass the second argument, the node is created as root. This is because the root item has an HTREEITEM value of TVI_ROOT, which is passed as default. You can also pass the second argument as NULL, which would produce the same effect.

The InsertItem() method returns an HTREEITEM value. You can use this value as a parent to a leaf item. This is done by passing it as the second argument when creating a leaf. Here is an example:

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

	// 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
	
	// TODO: Add extra initialization here
	TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP |
		             TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT |
			 TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS |  
			 TVS_TRACKSELECT,
		             CRect(10, 10, 200, 240), this, 0x1221);
	
	HTREEITEM hTree, hCompany;

	hTree = TreeSoft->InsertItem("Software Production", TVI_ROOT);
	
	hCompany = TreeSoft->InsertItem("Microsoft", hTree);
	TreeSoft->InsertItem("Office", hCompany);
	TreeSoft->InsertItem("Visual Studio", hCompany);
	TreeSoft->InsertItem("Servers", hCompany);
	
	hCompany = TreeSoft->InsertItem("Jasc", hTree);
	TreeSoft->InsertItem("Paint Shop Pro", hCompany);
	TreeSoft->InsertItem("Animation Shop", hCompany);
	
	hCompany = TreeSoft->InsertItem("Lotus", hTree);
	TreeSoft->InsertItem("Notes", hCompany);
	TreeSoft->InsertItem("Smart Office", hCompany);

	hCompany = TreeSoft->InsertItem("Macromedia", hTree);
	TreeSoft->InsertItem("Flash", hCompany);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

When using the InsertItem() method as we have done so far, the items are added in the order of their appearance. Besides creating new nodes, the InsertItem() method also allows you to control the order in which to insert the new item. The new leaf can be added as the first or the last child of a node. To do this, pass a third argument to this version of the InsertItem() method and give it the TVI_FIRST to be the first child or TVI_LAST to be the last child of the current parent node. Here is an example:

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

	// 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
	
	// TODO: Add extra initialization here
	TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP |
		             TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT |
			 TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS |  
			 TVS_TRACKSELECT,
		             CRect(10, 10, 200, 240), this, 0x1221);
	
	HTREEITEM hTree, hCompany;

	hTree = TreeSoft->InsertItem("Software Production", TVI_ROOT);
	
	hCompany = TreeSoft->InsertItem("Microsoft", hTree);
	TreeSoft->InsertItem("Office", hCompany);
	TreeSoft->InsertItem("Graphics Manipulation", hCompany, TVI_LAST);
	TreeSoft->InsertItem("Project Management", hCompany);
	TreeSoft->InsertItem("Software Develoment", hCompany);
	TreeSoft->InsertItem("Operating Systems", hCompany, TVI_FIRST);
	TreeSoft->InsertItem("Software Documentation", hCompany);

	hCompany = TreeSoft->InsertItem("Jasc", hTree);
	TreeSoft->InsertItem("Paint Shop Pro", hCompany);
	TreeSoft->InsertItem("Animation Shop", hCompany);
	
	hCompany = TreeSoft->InsertItem("Lotus", hTree);
	TreeSoft->InsertItem("Notes", hCompany);
	TreeSoft->InsertItem("Smart Office", hCompany);
	
	hCompany = TreeSoft->InsertItem("Macromedia", hTree);
	TreeSoft->InsertItem("Flash", hCompany);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

Tree Control Messages

 

Most messages of the tree controls are notification messages that are sent to its parent window. For example, the NM_CLICK message is sent to the dialog box or the form, that acts as the parent, that the tree control has been clicked. If the click was done with the right mouse button, the NM_RCLICK message is sent instead. In the same way, if the user double-clicks the control an NM_DBLCLK message is sent. If the user double-clicked with the right mouse button, the NM_CDBLCLK message is sent.

As mentioned already, the user has the ability to expand a node that has at least one child. When the user initiates an action that would expand a node, the tree control sends a TVN_ITEMEXPANDING. After the item has expanded, the control sends the TVN_ITEMEXPANDED message.

Tree Control With Bitmaps or Icons

 

Bitmaps can be used to enhanced the display of items on a tree control. Each tree item can be configured to display or not to display a small picture on its left. To do this, you can declare a CImageList variable and add pictures to it. Once the image list is ready, you can call the CTreeCtrl::SetImageList() method. Its syntax is:

CImageList* SetImageList(CImageList * pImageList, int nImageListType);

The first argument, pImageList, is a pointer to a CImageList variable. The nImageListType argument specifies the type of image list that will be used. The possible values are:

Value TypeDescription
LVSIL_NORMAL The image list is made of large icons
LVSIL_SMALL The image list is made of small icons
LVSIL_STATE The image list is made of state images

 
To specify the pictures used for a tree item, call one of the following versions of the CTreeCtrl:: InsertItem() methods:

HTREEITEM InsertItem(UINT nMask,
                                    LPCTSTR lpszItem,
                                    int nImage,
                                     int nSelectedImage,
                                     UINT nState,
                                     UINT nStateMask,
                                     LPARAM lParam,
                                     HTREEITEM hParent,
                                     HTREEITEM hInsertAfter );
HTREEITEM InsertItem(LPCTSTR lpszItem,
                                    int nImage,
                                    int nSelectedImage, 
                                    HTREEITEM hParent = TVI_ROOT,
                                    HTREEITEM hInsertAfter = TVI_LAST);

The nMask argument specifies the type of value used to set on the list item. As seen already, the lpszItem is the text that will be displayed for the current item.

The value of nImage is the index of the image used for the item being inserted from the image list. The nSelectedImage value is the index of the image that will display when the inserted item is selected or has focus.

Related Application

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值