We help IT Professionals succeed at work.

CTReeCtrl Hide Checkbox

Ocrana
Ocrana used Ask the Experts™
on
Hello,
is it possible to hide a checkbox inside a Treectrl? I found some things in the internet but none of them work. I want to hide the checkbox of "parent" items in my treectrl.

Thanks,

Ingo
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
idx = m_ctrTree.InsertItemAndData(Course.cName,CAdminView::ICOIDX_COURSE,NULL,(LPARAM)(oldpos+2*sizeof(LPVOID)),0);
// remove checkboxes for root items - this makes the item think it is checked but we don't display the state
m_ctrTree.SetItemState(idx, INDEXTOSTATEIMAGEMASK(0), TVIS_STATEIMAGEMASK); 

Open in new window


is how I did it in an application where m_ctrTree was the CTreeCtrl. First line is where the item was inserted and returns the idx to use when disabling the checkbox.

Cheers,
   Chris

Author

Commented:
Hi, do not have any eefect in my app. Still shows the checkbox :(
Yep, just tested it out, my code only works if you have created a CImageList and attached it to the tree control first.

I suppose you could create an empty transparent icon, create the CImageList and add the icon to it, then attach the imagelist to the control.

Cheers,
   Chris
Here's a little Visual Studio 10 project doing what you want to do

edit >> Experts Exchange won't let me upload the project in a zip file because it contains an ico file! I'm attaching the source file by itself instead.

Cheers,
  Chris
TreeTestDlg.cpp

Author

Commented:
OK, that works now, but now I have at the right hand side of the checkbox a empty icon, how can I now hide the empty icon?

Ingo
I'm not sure if you can remove the checkbox and hide the icon too using a standard CTreeCtrl.

The checkbox is part of the state image and displaying it is controlled within the source of CTreeCtrl when it reads the dialog template. Telling the CTreeCtrl you are adding to the state image with an image list, then telling it not to show any state image for an item removes the checkbox too. Its taking advantage of a quirk in the way CTreeCtrl works.

I'll have a play about and look at the source code for CTreeCtrl but I don't hold out a lot of hope at the moment without doing a major sub-classing of the control.

Cheers,
   Chris
BOOL CTreeTestDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon


	CImageList TreeImageList;
	TreeImageList.Create(16,16,ILC_COLOR32,0,1);	
	TreeImageList.Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
	mTreeCtrl.SetImageList(&TreeImageList,TVSIL_NORMAL);

	HTREEITEM root;
	root = mTreeCtrl.InsertItem(L"Item Root 1");
	// disable the checkbox for the root item
	mTreeCtrl.SetItemState(root, INDEXTOSTATEIMAGEMASK(0), TVIS_STATEIMAGEMASK); 
	mTreeCtrl.SetImageList(NULL,TVSIL_NORMAL);
	
	mTreeCtrl.InsertItem(L"Item child 1",root);
	mTreeCtrl.InsertItem(L"Item child 2",root);
	mTreeCtrl.InsertItem(L"Item child 3",root);

	root = mTreeCtrl.InsertItem(L"Item Root 2");
	mTreeCtrl.SetItemState(root, INDEXTOSTATEIMAGEMASK(0), TVIS_STATEIMAGEMASK); 
	mTreeCtrl.InsertItem(L"Item child 1",root);
	mTreeCtrl.InsertItem(L"Item child 2",root);
	mTreeCtrl.InsertItem(L"Item child 3",root);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

Open in new window


works  by setting the imagelist to null after displaying the first root item. The quirks of CTreeCtrl allows you to subsequently set the state image to nothing successfully for future root items.

Cheers,

   Chris

Author

Commented:
Many thanks for the great help.

Ingo