Sign in to follow this  
hick18

[win32] TreeView State Images

Recommended Posts

Im trying to create my tree where when creating a node, the check box is initially checked. Yet with the following code, they are not checked. Any ideas?

[code]
TreeView::TreeView( const std::string& Name, bool CheckBoxes )
: Window(Name)
{
DWORD Style = WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_LINESATROOT | TVS_NOHSCROLL;

if( CheckBoxes )
{
Style |= TVS_CHECKBOXES;
}

m_hWinapiTreeView = CreateWindow( WC_TREEVIEW, Name.c_str(), Style,
0, 0, 0, 0, GetHandle(), (HMENU)0, g_hInstance, NULL);
}
[/code]

[code]
HTREEITEM TreeView::AddNode( const std::string& Name, HTREEITEM ParentHandle ) // Default ParentHandle == TVI_ROOT
{
TVITEM tvItem;
ZeroMemory( &tvItem, sizeof(TVITEM) );
tvItem.mask = TVIF_TEXT | TVIF_STATE;
tvItem.stateMask = TVIS_STATEIMAGEMASK;
tvItem.state = INDEXTOSTATEIMAGEMASK(2);
tvItem.pszText = (LPSTR)Name.c_str();
tvItem.cchTextMax = Name.size();

TVINSERTSTRUCT tvInsert;
ZeroMemory( &tvInsert, sizeof(TVINSERTSTRUCT) );
tvInsert.item = tvItem;
tvInsert.hInsertAfter = TVI_LAST;
tvInsert.hParent = ParentHandle;

return (HTREEITEM)SendMessage( m_hWinapiTreeView, TVM_INSERTITEM, 0, (LPARAM)(LPTVINSERTSTRUCT)&tvInsert);
}
[/code]

Share this post


Link to post
Share on other sites
Edit :

[quote] [url="http://msdn.microsoft.com/en-us/library/bb776779%28v=VS.85%29.aspx"]Version 4.70[/url]. Enables check boxes for items in a tree-view control. A check box is displayed only if an image is associated with the item. When set to this style, the control effectively uses [url="http://msdn.microsoft.com/en-us/library/dd162480%28v=VS.85%29.aspx"][b]DrawFrameControl[/b][/url] to create and set a state image list containing two images. State image 1 is the unchecked box and state image 2 is the checked box. Setting the state image to zero removes the check box altogether. For more information, see [url="http://msdn.microsoft.com/en-us/library/bb760017%28v=VS.85%29.aspx"]Working with state image indexes[/url].

[url="http://msdn.microsoft.com/en-us/library/bb776779%28v=VS.85%29.aspx"]Version 5.80[/url]. Displays a check box even if no image is associated with the item.

Once a tree-view control is created with this style, the style cannot be removed. Instead, you must destroy the control and create a new one in its place. Destroying the tree-view control does not destroy the check box state image list. You must destroy it explicitly. Get the handle to the state image list by sending the tree-view control a [url="http://msdn.microsoft.com/en-us/library/bb773585%28v=VS.85%29.aspx"][b]TVM_GETIMAGELIST[/b][/url] message. Then destroy the image list with [url="http://msdn.microsoft.com/en-us/library/bb761524%28v=VS.85%29.aspx"][b]ImageList_Destroy[/b][/url].

[color="#ff0000"]If you want to use this style, you must set the TVS_CHECKBOXES style with [url="http://msdn.microsoft.com/en-us/library/ms633591%28v=VS.85%29.aspx"][b]SetWindowLong[/b][/url] after you create the treeview control, and before you populate the tree. Otherwise, the checkboxes might appear unchecked, depending on timing issues.[/color]

[/quote]

[code]
DWORD Style = WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_LINESATROOT | TVS_NOHSCROLL | TVS_SHOWSELALWAYS;

m_hWinapiTreeView = CreateWindow( WC_TREEVIEW, Name.c_str(), Style, 0, 0, 0, 0, GetHandle(), (HMENU)0, g_hInstance, NULL);

if( CheckBoxes )
{
SetWindowLong( m_hWinapiTreeView, GWL_STYLE, Style | TVS_CHECKBOXES );
}
[/code]

Share this post


Link to post
Share on other sites

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