当前位置:网站首页>[MFC development (16)] tree control

[MFC development (16)] tree control

2022-07-01 08:41:00 Demo. demo

1. Property configuration of tree control

(1)Check Boxes : The default is false, If you choose to true Each node will be preceded by a box

 

(2)Edit Labels: The default is false, If you choose to true The name of each node can be edited and modified

(3)Has Buttons:  The default is false, If you choose to true The expanded node will be preceded by a plus or minus sign

 (4)Has Lines: The default is false, If you choose to true The following lines will appear

 

 (5) Lines At Root: The default is false, Choose as true If so, it will be wired from the root node

2. Common function methods

 (1) Add variables to the tree control

 

 (2) Insert node

// Add a node 
	// Each node in the tree control has a handle (HTREEITEM),
	// When inserting nodes at the same time, the parent node handle must be provided ,
	//( One of them Root There is only one node , You can neither add nor delete )
	// utilize HTREEITEM InsertItem(LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST);
	// You can insert a node ,pszItem For the displayed characters ,
	//hParent Handle representing the parent node , The currently added nodes will be arranged in hInsertAfter After the node represented by , The return value is the handle of the currently created node .
	hroot = m_Tree.InsertItem(_T(" father "));
	HTREEITEM hroot_sub1 = m_Tree.InsertItem(_T(" son 1"), hroot);
	HTREEITEM hroot_sub2 = m_Tree.InsertItem(_T(" son 2"), hroot);
	HTREEITEM hroot_sub3 = m_Tree.InsertItem(_T(" son 3"), hroot);
	HTREEITEM hroot_sub1_sub1 = m_Tree.InsertItem(_T(" Grandson 1-1"), hroot_sub1);
	HTREEITEM hroot_sub1_sub2 = m_Tree.InsertItem(_T(" Grandson 1-2"), hroot_sub1);
	HTREEITEM hroot_sub3_sub1 = m_Tree.InsertItem(_T(" Grandson 3-1"), hroot_sub3);
	HTREEITEM hroot_sub3_sub2 = m_Tree.InsertItem(_T(" Grandson 3-2"), hroot_sub3);

(3) Get the text content of the node

// Get the text content of the node 
	CString hroot_text = m_Tree.GetItemText(hroot);
	MessageBox(hroot_text);

 (4) Set the text content of the node

	// Set the text content of the node 
	m_Tree.SetItemText(hroot_sub1_sub1, _T(" My name is grandson "));

 (5) Get the currently selected node

// Get the selected node 
	HTREEITEM hselected = m_Tree.GetSelectedItem();
	// Get the text content on the selected node 
	CString hselected_text = m_Tree.GetItemText(hselected);
	MessageBox(hselected_text);

(6) Modify the currently selected node

// focusing 
	m_Tree.SetFocus();
	// Modify the selected node 
	m_Tree.SelectItem(hroot);

(7) Delete node

// Delete a node , Delete according to the handle 
	// If the deleted node has child nodes , Will also be deleted 
	m_Tree.DeleteItem(hroot_sub3);
	
	// Delete all nodes 
	m_Tree.DeleteAllItems();

3. Traverse the tree control

By default, the above cases are all for single selection , that Tree The tree control does not support multiple selection ? The answer is : Not supported by default
However, you can add CheckBox Check box to resolve !

Create a new member function to write a recursive function to traverse the tree control

// Tree recursive function 
void CMFC_Test1Dlg::EnumTreeItem(HTREEITEM hitem){
	if (!hitem)  return;

	// If the current node is checked 
	if (m_Tree.GetCheck(hitem)){

		// Get the text content of the node 
		CString hitem_text = m_Tree.GetItemText(hitem);
		MessageBox(hitem_text);
	}

	// Get child node 
	HTREEITEM hitem_child = NULL;

	if (m_Tree.ItemHasChildren(hitem)){
		// Get child nodes 
		hitem_child = m_Tree.GetChildItem(hitem);
		// If child nodes continue to exist 
		do
		{
			// Child nodes enter recursion 
			EnumTreeItem(hitem_child);
			// Find the sibling of the child node , If it is empty, it will jump out 
			hitem_child = m_Tree.GetNextItem(hitem_child, TVGN_NEXT);;
		} while (hitem_child);
	}
}

4. Fully unfolded / Merge all nodes

Add code

Add one Button Control to control expansion and merging

void CMFC_Test1Dlg::OnBnClickedButton20()
{
	// TODO:   Add control notification handler code here 
	static UINT opcode = TVE_EXPAND;
	// Extend parent node 
	ExpendTreeItem(hroot, opcode);
	// If it is expanded, it is set to merge 
	opcode = (opcode == TVE_EXPAND) ? TVE_COLLAPSE : TVE_EXPAND;
}

Merge and expand functions recursively

void CMFC_Test1Dlg::ExpendTreeItem(HTREEITEM hitem, UINT opcode){
	if (!hitem)  return;
	// Expand 
	m_Tree.Expand(hitem, opcode);
	// If the current node has children 
	if (m_Tree.ItemHasChildren(hitem)){
		// Get child node 
		HTREEITEM hchild = m_Tree.GetChildItem(hitem);
		while (hchild){
			// Expand or merge child nodes 
			ExpendTreeItem(hchild, opcode);
			// When the child node is finished, go to the adjacent node 
			hchild = m_Tree.GetNextItem(hchild,TVGN_NEXT);
		}
	}
}

 
5. How to edit the text content of a node and save changes

(1) Set the... Of the tree control label Can edit

(2) Add variable event

Start editing events

 

End editing event

 

 (3) Code completion

// Start editing labels 
void CMFC_Test1Dlg::OnTvnBeginlabeleditTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTVDISPINFO pTVDispInfo = reinterpret_cast<LPNMTVDISPINFO>(pNMHDR);
	// TODO:   Add control notification handler code here 
	// Set the length of characters that can only be entered 
	m_Tree.GetEditControl()->LimitText(5);
	*pResult = 0;// If this parameter =1, Cannot edit 
}


// End editing label 
void CMFC_Test1Dlg::OnTvnEndlabeleditTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTVDISPINFO pTVDispInfo = reinterpret_cast<LPNMTVDISPINFO>(pNMHDR);
	// TODO:   Add control notification handler code here 
	// If the content of the node is not empty and the length is greater than 0
	if (pTVDispInfo->item.pszText && _tcslen(pTVDispInfo->item.pszText)>0){
		// Set the text content of the node 
		m_Tree.SetItemText(pTVDispInfo->item.hItem, pTVDispInfo->item.pszText);
	}
	*pResult = 0;
}

6. Insert node with icon

You can add icons in front of each node , When selected and unchecked, different icons are displayed

(1) Add resources

Several of the ico Add icon files to project resources ,ID Respectively :IDI_ICON1、IDI_ICON2、IDI_ICON3

Import resources into the project resource clock

 

 (2) Add variables and complete the code

Add a picture resource variable CImageList

// Define the image resource variable 
	CImageList m_imglist;

Create image resources and bind them to tree control variables

// Create resources , The parameters are long 、 wide 、 sign 、 Initialize several 、 growth 
	m_imglist.Create(16,16,ILC_MASK|ILC_COLOR32,2,1);
	// Add resources 
	m_imglist.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
	m_imglist.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
	m_imglist.Add(AfxGetApp()->LoadIcon(IDI_ICON3));

	// Tree controls are associated with picture resources , The type of icon 
	m_Tree.SetImageList(&m_imglist,TVSIL_NORMAL);

Set the selected and unselected icon sequence numbers of nodes

You can set the icon when inserting the node

	// The parameter is the name of the node at a time , Icon sequence number when selected , Icon sequence number when not selected 
	hroot = m_Tree.InsertItem(_T(" father "),0,1);
	HTREEITEM hroot_sub1 = m_Tree.InsertItem(_T(" son 1"),0,1, hroot);
	HTREEITEM hroot_sub2 = m_Tree.InsertItem(_T(" son 2"), 0, 1, hroot);

 

原网站

版权声明
本文为[Demo. demo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010832398245.html