Programming Tips - Win32 and MFC: Why are my TreeView checkboxes not working? (solved)

Date: 2010may18 OS: Windows Platform: Windows Language: C/C++ Q. Win32 and MFC: Why are my TreeView checkboxes not working? (solved) A. MSDN says: If you want to use this style, you must set the TVS_CHECKBOXES style with SetWindowLong after you create the treeview control, and before you populate the tree. Otherwise, the checkboxes might appear unchecked, depending on timing issues. So even if you set it in .rc file that isn't enough! My practice is to never set it in the .rc file (since it only half works) and suggests that it is working. In MFC:
m_MyTree.ModifyStyle(TVS_CHECKBOXES, TVS_CHECKBOXES);
In Win32:
void EnableCheckboxes(HWND hwndTreeView) { DWORD dwStyle = GetWindowLong(hwndTreeView, GWL_STYLE); dwStyle |= TVS_CHECKBOXES; SetWindowLong(hwndTreeView, GWL_STYLE, dwStyle); }