Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

CPropTree v1.0 - Property Tree Control

Rate me:
Please Sign up or sign in to vote.
4.92/5 (64 votes)
1 Jan 20023 min read 634.3K   12.4K   199   95
Custom MFC tree control that support dynamic editable items

Sample Image Sample Image Sample Image

Introduction

The CPropTree class implements a tree control that is similar to the property view seen in Visual Studio .Net. The source project compiles to a DLL so you can easily incorporate it into your project. You can check my web page for more information on the control.

The control has two window areas:

  • Tree Control
  • Descriptive area

The tree control section functions just a like a normal tree control with an addition of a splitter bar separating the tree item properties. Tree item properties are inherited from the CPropTreeItem class. These items can be edit controls, static text, dropdown combo list, or any type of control you wish to create. Items on the root level of the tree control do not have editable properties. They are used as section headers.

Tree items can contain checkboxes. By default properties that are editable (non-readonly), are displayed in bold lettering. Read only or non-editable items are displayed in a normal font.

The descriptive area displays the selected item's text and any informational text. This section can be displayed or hidden by calling CPropTree::ShowInfoText().

Implementation

To use the control, create an instance of the class in your dialog or view class header. In the OnInitDialog() or OnCreate() initialize the control:

DWORD dwStyle;
CRect rc;

// PTS_NOTIFY - CPropTree will send notification messages to the parent window
dwStyle = WS_CHILD|WS_VISIBLE|PTS_NOTIFY;

// Init the control's size to cover the entire client area
GetClientRect(rc);

// Create CPropTree control
m_Tree.Create(dwStyle, rc, this, IDC_PROPERTYTREE);

To add items to the control, dynamically create objects inherited by the CPropTreeItem class and call the CPropTree::InsertItem() method.

CPropTreeItem* pItem1;

pItem1 = m_Tree.InsertItem(new CPropTreeItem());
pItem1->SetLabelText(_T("Properties"));
pItem1->SetInfoText(_T("This is a root level item"));

CPropTreeItem* pItem2;

pItem2 = m_Tree.InsertItem(new CPropTreeItemEdit(), pItem1);
pItem2->SetLabelText(_T("EditItem"));
pItem2->SetInfoText(_T("This is a child item of Properties, with an edit control"));

pItem2->SetItemValue((LPARAM)_T("This is some default text for the edit control"));

Advanced Features

Notification Messages

The control uses WM_NOTIFY notification messages to signal events. Add a ON_NOTIFY message handler to the parent window to process notifications. The control supports most of the common NM_ codes with addition of the following CPropTree specific WM_NOTIFY codes.

  • PTN_INSERTITEM when an item is inserted
  • PTN_DELETEITEM when an item is about to be deleted
  • PTN_DELETEALLITEMS when a call is made to delete all items
  • PTN_ITEMCHANGED when an item's property has been modified
  • PTN_SELCHANGE when the current selection changed
  • PTN_ITEMEXPANDING when an item is about to expand or close
  • PTN_COLUMNCLICK when the mouse clicks on the splitter bar
  • PTN_PROPCLICK when the mouse clicks on an item's property area
  • PTN_CHECKCLICK when the mouse clicks on an item's checkbox

PTN_
notification messages returns the NMPROPTREE structure. You can use this structure along with the CPropTreeItem::SetCtrlID() method to determine the tree item that sent an event.

[...]
	//}}AFX_MSG_MAP
	ON_NOTIFY(PTN_ITEMCHANGED, IDC_PROPERTYTREE, OnItemChanged)
END_MESSAGE_MAP()


void CMyDialog::OnItemChanged(NMHDR* pNotifyStruct, LRESULT* plResult)
{
	LPNMPROPTREE pNMPropTree = (LPNMPROPTREE)pNotifyStruct;

	if (pNMPropTree->pItem)
	{
		// retrieve pItem's changed data
	}

	*plResult = 0;
}

Custom Property Items

The library already has some default derived CPropTreeItem classes ready to use. Such as an edit control CPropTreeItemEdit, color picker CPropTreeItemColor, dropdown combobox CPropTreeItemCombo and static text CPropTreeItemStatic. To create your own custom property items:

  • Create your own class inherited by CPropTreeItem. This class usually will have multiple inheritance depending on what type of item you want to create.
    class CMyCustomEditPropItem : public CEdit, public CPropTreeItem
    {
    public:
    	CMyCustomEditPropItem();
    	virtual ~CMyCustomEditPropItem();
    
    	[...]
    };
    class CMyCustomComboBoxPropItem : public CComboBox, public CPropTreeItem
    {
    public:
    	CMyCustomComboBoxPropItem();
    	virtual ~CMyCustomComboBoxPropItem();
    
    	[...]
    };
  • Override the CPropTreeItem virtual methods to implement your property item.
  • Dynamically create an instance of your class and insert it into a CPropTree control.

The methods most often overriden from CPropTreeItem are:

  • CPropTreeItem::OnActivate(). Called when the property area is clicked by the mouse or the enter key has been pressed on the selected item. The OnActivate() method is where you show the property item's window if it has one (such as an edit control or a popup menu).
  • CPropTreeItem::OnCommit(). Called when data has been commited. In this method you would extract the changed data and hide the property's item's window. OnCommit() gets called when CommitChanges() is called. A derived CPropTreeItem class would call CommitChanges() during a loss of the input focus or if the "Enter" key is press as in a edit control.
  • CPropTreeItem::DrawAttribute(). CPropTree calls DrawAttribute() when the property item needs to be displayed. Drawing is done directly on the display context of the PropTree control.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to use this CPropTree? Pin
Member 128866316-Dec-16 23:40
Member 128866316-Dec-16 23:40 
QuestionHow can it support VS2008, It can't Draw Text in VS2008.Thanks! Pin
mconstor3-May-10 21:55
mconstor3-May-10 21:55 
AnswerRe: How can it support VS2008, It can't Draw Text in VS2008.Thanks! Pin
windsky11030-Mar-11 15:51
windsky11030-Mar-11 15:51 
GeneralLicense Pin
silent_neiep1-Dec-09 15:34
silent_neiep1-Dec-09 15:34 
Generalcolor select dialog bug Pin
yq_sun25-Jun-09 18:41
yq_sun25-Jun-09 18:41 
GeneralFind a bug work with sizecontrolbar [modified] Pin
m_harriss7-May-09 11:10
m_harriss7-May-09 11:10 
GeneralMemory leaks found and fixed [modified] Pin
Shashidhar Kamath23-Oct-08 12:42
Shashidhar Kamath23-Oct-08 12:42 
GeneralControl on the dialog box with other controls Pin
Shashidhar Kamath23-Oct-08 8:44
Shashidhar Kamath23-Oct-08 8:44 
GeneralCheckbox is on wrong side Pin
johnnyk427728-Jun-08 22:23
johnnyk427728-Jun-08 22:23 
GeneralImplementing tab key Pin
bioan19-Nov-07 8:17
professionalbioan19-Nov-07 8:17 
GeneralDebug assertion error [modified] Pin
bioan19-Nov-07 7:32
professionalbioan19-Nov-07 7:32 
GeneralRe: Debug assertion error Pin
rivan837-Feb-08 23:31
rivan837-Feb-08 23:31 
GeneralRe: Debug assertion error Pin
heqj061838-Oct-09 3:38
heqj061838-Oct-09 3:38 
GeneralI am in china.My English is poor,but I need you help urgently. Pin
Plainzeng29-Mar-07 16:11
Plainzeng29-Mar-07 16:11 
GeneralLanguages and combos questions Pin
andrewtruckle24-Mar-07 7:51
andrewtruckle24-Mar-07 7:51 
Question64-bit? Pin
Traal18-Oct-06 11:37
Traal18-Oct-06 11:37 
AnswerRe: 64-bit? Pin
Mo Hossny18-May-09 15:12
Mo Hossny18-May-09 15:12 
GeneralBug Hiding Info Text Pin
Merlin Avery21-Mar-06 11:55
Merlin Avery21-Mar-06 11:55 
QuestionHello,sramsay,can you help me? Pin
oldmonster11-Jun-05 17:12
oldmonster11-Jun-05 17:12 
QuestionIf there is a special Tree with root node on right side? Pin
Wang Jian-gong8-Jun-05 21:49
Wang Jian-gong8-Jun-05 21:49 
QuestionExtending CPropTree? Pin
Anonymous30-Apr-05 14:52
Anonymous30-Apr-05 14:52 
Questionhow to delete the root level items Pin
itsh1111-Apr-05 11:36
itsh1111-Apr-05 11:36 
Generalcolumn resizing Pin
Popyweb28-Jan-05 14:45
Popyweb28-Jan-05 14:45 
GeneralShow Tree Lines Pin
Manoj Singh K18-Jan-05 18:54
Manoj Singh K18-Jan-05 18:54 
QuestionHelp! How to set the width for each item ??? Pin
brapler14-Jan-05 23:16
brapler14-Jan-05 23:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.