CTreeCtrl TVITEM structure - text length

124 views Asked by At

I am not able to find (in MSDN), if parameter int cchTextMax of TVITEM structure is included the end null terminated string character \0 or no.

TVITEM structure is used by CTreeCtrl or CTreeView class.

I mean, is it right cchTextMax = wcslen(lsTxt) or cchTextMax = wcslen(lsTxt)+1 ??

Thanks for advice.

2

There are 2 answers

0
Remy Lebeau On

Unfortunately, the TreeView documentation does not say explicitly whether or not cchTextMax includes the null terminator (on the other hand, the ListView documentation does say, and ListView and TreeView are implemented in the same library, so it is likely that they behave similarly).

Since pszText points to a null-terminated string, and cchTextMax is the size of the pszText buffer in characters, it stands to reason that cchTextMax includes the null terminator. Particularly when retrieving text, since TVM_GETITEM and similar messages need to know how large the buffer is so they can store the null terminator and truncate text if needed.

1
Constantine Georgiou On

Per the documentation:

cchTextMax

Type: int

Size of the buffer pointed to by the pszText member, in characters. If this structure is being used to set item attributes, this member is ignored.

So, you don't use cchTextMax to set the text of an item.

But to retrieve the text you need a buffer to receive it, and cchTextMax is the buffer size, so this includes the terminating null. For example:

TVITEM tvi;
TCHAR sBuff[BUF_MAX];

.
.
tvi.pszText = sBuff;
tvi.cchTextMax = BUF_MAX;  // Or:
tvi.cchTextMax = sizeof(sBuff)/sizeof(TCHAR);

EDIT:

If you insert callback items you don't insert text, instead you set pszText to LPSTR_TEXTCALLBACK, and you must process the TVN_GETDISPINFO notification, ie the system is asking you "what text should I display for this item?". You can just set pszText to point to a buffer or copy your data to the buffer already pointed by pszText - you may copy up to cchTextMax TCHARs in the latter case.