11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Common Controls<br />

The first step of enabling label editing is to set “Edit labels” style when adding tree control resource to<br />

the dialog template. The following lsts necessary steps of doing this: 1) Invoke “Tree Control Properties”<br />

property sheet, click “Styles” tab. 2) Check “Edit labels” check box (Figure 5-11).<br />

Label editing will be enabled if this style is selected. However, if we do not add code to change the<br />

label at the end of editing, the label will remain unchanged after it is edited. To make this happen, we must<br />

handle message TVN_ENDLABELEDIT.<br />

Standard TVN_ENDLABELEDIT message handler added by Class Wizard will have the following format:<br />

void MCTreeCtrl::OnEndlabeledit(NMHDR *pNMHDR, LRESULT *pResult)<br />

{<br />

TV_DISPINFO *pTVDispInfo = (TV_DISPINFO*)pNMHDR;<br />

}<br />

*pResult=0;<br />

Figure 5-11. Check “Edit labels” to enable label<br />

Here pTVDispInfo is a pointer to TV_DISPINFO type object, which can be obtained from the message<br />

parameter. The most useful member of TV_DISPINFO is item, which is a TV_ITEM type object. Three<br />

members of item contain valid information: hItem, lParam, and pszText. We could use hItem to identify<br />

the node and use pszText to obtain the updated text string. If pszText is a NULL pointer, this means the<br />

editing is canceled (Label editing can be canceled through pressing ESC key). Otherwise it will contain a<br />

NULL-terminated string. The following is the implementation of this message handler:<br />

void MCTreeCtrl::OnEndlabeledit(NMHDR *pNMHDR, LRESULT *pResult)<br />

{<br />

TV_DISPINFO *pTVDispInfo = (TV_DISPINFO*)pNMHDR;<br />

}<br />

if(pTVDispInfo->item.pszText != NULL)<br />

{<br />

SetItemText<br />

(<br />

pTVDispInfo->item.hItem,<br />

pTVDispInfo->item.pszText<br />

);<br />

}<br />

*pResult=0;<br />

If the editing is not canceled, we need to call function CTreeCtrl::SetItemText(…) to set the node’s<br />

new text, which has the following format:<br />

BOOL CTreeCtrl::SetItemText(HTREEITEM hItem, LPCTSTR lpszItem);<br />

This function is similar to CTreeCtrl::SetItemImage(…). Its first parameter is the handle of tree<br />

control, and the second parameter is a string pointer to the new label text.<br />

There are other messages associated with label editing, one useful message is TVN_BEGINLABELEDIT,<br />

which will be sent when the editing is about to begin. We can handle this message to disable label editing<br />

for certain nodes. In the message handler, if we assign a non-zero value to the content of pResult, the edit<br />

will stop. Otherwise the label editing will go on as usual.<br />

124

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!