VC + + How to change the background color of the node and the color of the node text in the tree controls control __c++

Source: Internet
Author: User
Tags add time

One of the most recent problems in your project is to set the background color of the nodes and the color of the node text in the tree controls control. Looking for a lot of information on the Internet, found that the most common way is to respond to the control of the Nm_customdraw message. The specific implementation steps are as follows:

1, in VS inside the new MFC dialog box project name is "Testtreecontrol", delete the default added static text box. Then drag a tree control and a button control in the Toolbox to adjust the position and size of the control.

2, modify the tree control ID is idc_treeview, the button's ID is idc_addnode,caption property for add Node.

3, add an associated variable to the tree control M_treectrl type is CTreeCtrl. (vs Add methods: Select the control Right click, select "Add Variable" in the pop-up menu, pop-up Add Variable dialog box, the default is to add control variables, categories for the controls, give the variable a name and click "OK" button).

4, double click the "Add Node" button to jump to the button click event function, here Add the node to the tree control code. As shown below:

void Ctesttreecontroldlg::onbnclickedaddnode ()
{
	//TODO: Add control Notification Handler code
	CString strtext=_t ("") here;
	Htreeitem Hroot;
	Hroot = M_treectrl.insertitem (_t ("root Node"))//Insert root

	tv_insertstruct tcitem;//Insert data item structure
        tcitem.hparent=hroot;/ /Add the root item
        tcitem.hinsertafter=tvi_last;//after the last item
        tcitem.item.mask=tvif_text| tvif_param| tvif_image;//Set Shielding
        tcitem.item.psztext= "NODE1";
        tcitem.item.lparam=1000;//serial number
        tcitem.item.iimage=0;//normal icon
	m_treectrl.insertitem (&tcitem);

        tcitem.item.psztext= "NODE2";
        tcitem.item.lparam=2000;//Serial number
	M_treectrl.insertitem (&tcitem);

        tcitem.item.psztext= "NODE3";
        tcitem.item.lparam=3000;//Serial number
	M_treectrl.insertitem (&tcitem);

	M_treectrl.expand (Hroot, Tve_expand);
}

5, add the following code in the OnInitDialog () function

	TODO: Add additional initialization code here

	static CImageList Clst;
	Clst. Create (16,16,ilc_color8,2,2);
	Clst. ADD (AfxGetApp ()->loadicona (idi_picture));
	M_treectrl.setimagelist (&clst,tvsil_normal);
	DWORD Dwstyles=getwindowlong (M_treectrl.m_hwnd,gwl_style);//Get Tree control original style
     Dwstyles |= tvs_hasbuttons| tvs_haslines| Tvs_linesatroot;
    SetWindowLong (m_treectrl.m_hwnd,gwl_style,dwstyles);//Set style
6. Add an event handler for the tree control. Select the tree control right click, pop-up menu select "Add Time Handler", in the pop-up dialog box, in the Message type Drop-down list, select the Nm_customdraw message, and then click the "Add Edit" button to enter the time processing function.

7. Add the following code in the event handler function

void Ctesttreecontroldlg::onnmcustomdrawtreeview (NMHDR *pnmhdr, Lresult *presult) {*presult = Cdrf_dodefault;

	nmtvcustomdraw* plvoid = reinterpret_cast<nmtvcustomdraw*> (PNMHDR);
	if (Cdds_prepaint = = plvoid->nmcd.dwdrawstage) {*presult = Cdrf_notifyitemdraw;
		
		else if (Cdds_itemprepaint = = plvoid->nmcd.dwdrawstage) {colorref crtext, crbkgnd; if (Plvoid->ilevel = = 1)//To determine the level at which the node is located, the root node in the No. 0 layer {if (Plvoid->nmcd.litemlparam = = 1000)////////When the node is added, the LParam property of the node is set.
				Use to determine the specific node {crtext = RGB (0,0,0);

				Crbkgnd = RGB (125,125,125);  Plvoid->clrtext = Crtext;  Sets the text color PLVOID-&GT;CLRTEXTBK = CRBKGND;
				Sets the background color} else if (Plvoid->nmcd.litemlparam = =) {Crtext = RGB (0,0,0);

				Crbkgnd = RGB (125,125,0);
				Plvoid->clrtext = Crtext;
			PLVOID-&GT;CLRTEXTBK = Crbkgnd;   
				else {crtext = RGB (0,0,0);
			Crbkgnd = RGB (255,255,255);
			} else {crtext = RGB (0,0,0);
		Crbkgnd = RGB (255,255,255);
}
		*presult = Cdrf_dodefault; }
}

Compile and run the program, click the "Add Node" button will add nodes to the tree control, and will change the corresponding node background color and text color. As shown in the following illustration:




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.