Sign in to follow this  

(WINAPI) TreeView controls with check boxes on the nodes

This topic is 4268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am wondering how to set (and get) the check states of the nodes in a TreeView with the TVS_CHECKBOXES style. MSDN suggests the TreeView_SetCheck macro, but for some reason it is not in my headers (must be out of date, I am using VC++ 6). It mentions that failing that, one should just use TVM_SETITEM (or TreeView_SetItem), but I am having trouble figuring out how exactly to do this. I've been fiddling with it for a few days, and I havn't had much luck. Anyone know how to do this? Thanks, ~SPH

Share this post


Link to post
Share on other sites
Since VC++ 6.0 is about 8 years old it's very possible that your Platform SDK is out of date. If possible, you might want to look into upgrading (for free) to Visual C++ Express.

Back to your problem:
From what I gather from the MSDN you must create your TVITEM in the following way


// Set the state
TVITEM tvi;

tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;

// INDEXTOSTATEIMAGEMASK(2) is the checked state
// INDEXTOSTATEIMAGEMASK(1) is the unchecked state
// INDEXTOSTATEIMAGEMASK(0) removes the checkbox (no state)
tvi.state = INDEXTOSTATEIMAGEMASK(2);

SendMessage(hwnd_treeview, TVM_SETITEM, 0, (LPARAM)(&TVITEM));




// Get the state
UINT state = SendMessage(hwnd_treeview, TVM_GETITEMSTATE, (WPARAM)htreeitem, TVIS_STATEIMAGEMASK);

switch (state) {
case INDEXTOSTATEIMAGEMASK(0):
// No state
break;

case INDEXTOSTATEIMAGEMASK(1):
// Unchecked
break;

case INDEXTOSTATEIMAGEMASK(2):
// Checked
break;
}

Share this post


Link to post
Share on other sites
Those macros are actually named TreeView_SetCheckState and TreeView_SetItemState (note the "State" on the end).

Just to help you out, from the latest Platform SDK (VC2005) the TreeView_SetCheckState macro is defined as:
#define TreeView_SetCheckState(hwndTV, hti, fCheck)
TreeView_SetItemState(hwndTV, hti, INDEXTOSTATEIMAGEMASK((fCheck)?2:1), TVIS_STATEIMAGEMASK)

This only seems to be available if _WIN32_IE is defined to be greater than or equal to 0x0500 -- however, TreeView_SetItemState is also under that same #define. So if you have TreeView_SetItemState, you should also have TreeView_SetCheckState...

Share this post


Link to post
Share on other sites
Thank you for the response, unfortunately it doesn't seem to be having any effect. Here is what I have:


TVITEM item;
item.mask=TVIF_STATE;
item.stateMask=TVIS_STATEIMAGEMASK;
for(int i=0;i<DEFDEVICE_NLINES;i++)
{
item.hItem = IOItems[i];
item.state = INDEXTOSTATEIMAGEMASK(2);
TreeView_SetItem(treeHandle,&item);
}





Perhaps you can see what I am doing wrong?

Thanks, ~SPH

EDIT: I do not seem to have TreeView_SetItemState either.

Share this post


Link to post
Share on other sites

No one has any idea on this? Anyone have a working sample source perhaps?

Even though I can't see any difference from my code, I have also tried directly pasting in what was posted as an example for me:


// Set the state
TVITEM tvi;

tvi.hItem = IOItems[0];
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;

// INDEXTOSTATEIMAGEMASK(2) is the checked state
// INDEXTOSTATEIMAGEMASK(1) is the unchecked state
// INDEXTOSTATEIMAGEMASK(0) removes the checkbox (no state)
tvi.state = INDEXTOSTATEIMAGEMASK(2);

SendMessage(treeHandle, TVM_SETITEM, 0, (LPARAM)(&tvi));



But to no avail...

Thanks for any suggestions/examples, ~SPH

Share this post


Link to post
Share on other sites
I think this is a bug in the API.. not sure though, but I had problems until I saw code at this page:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_ctreectrl.3a3a.setcheck.asp

Notice the lines:
mTree.Modifystyle( TVS_CHECKBOXES, 0 );
mTree.Modifystyle( 0, TVS_CHECKBOXES );

so, I did this:
DWORD style = GetWindowLong(m_ControlHandle,GWL_style);
style ^= TVS_CHECKBOXES;
SetWindowLong(m_ControlHandle,GWL_style, style);
style |= TVS_CHECKBOXES;
SetWindowLong(m_ControlHandle,GWL_style, style);

That seemed to do the trick for me... try it out... otherwise, checkboxes weren't being set by the normal commands.

Jeff.

Share this post


Link to post
Share on other sites

This topic is 4268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this