Wraps the tree view control
2025-12-02
Written by: xiaobin
CTreeCtrl and CTreeView:
m_myTreeCtl
default value:
GetTreeCtrl().ModifyStyle(0, TVS_HASBUTTONS);
GetTreeCtrl().ModifyStyle(0, TVS_HASLINES);
GetTreeCtrl().ModifyStyle(0, TVS_LINESATROOT);
First obtain the handle, then set the Tree-View Control Window Styles.
void CLeftView::SetNewStyle(long lStyleMask, BOOL bSetBits)
{
long lStyleOld;
lStyleOld = GetWindowLongPtr(GetTreeCtrl().GetSafeHwnd(), GWL_STYLE);
lStyleOld &= ~lStyleMask;
if (bSetBits)
lStyleOld |= lStyleMask;
SetWindowLongPtr(GetTreeCtrl().GetSafeHwnd(), GWL_STYLE, lStyleOld);
SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
The example comes from “hyun” and “CMyTreeCtrl::SetNewStyle”
for (iItem = 0; iItem < sizeof(strItems) / sizeof(strItems[0]); iItem++)
{
curTreeItem.hParent = (iItem % 4 == 0)? NULL : m_rghItem[iItem / 4 * 4];
curTreeItem.hInsertAfter = TVI_SORT;
curTreeItem.item.iImage = iItem / 4 * 2;
curTreeItem.item.iSelectedImage = curTreeItem.item.iImage + 1;
curTreeItem.item.pszText = strItems[iItem].GetBuffer(strItems[iItem].GetLength());
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
m_rghItem[iItem] = GetTreeCtrl().InsertItem(&curTreeItem);
}
HTREEITEM m_rghItem[12];
CString strItems[12];
int iItem;
TV_INSERTSTRUCT curTreeItem;
strItems[0] = _T("Dogs");
strItems[1] = _T("German Shepherd");
strItems[2] = _T("Dalmatian");
strItems[3] = _T("Great Dane");
strItems[4] = _T("Birds");
strItems[5] = _T("Hummingbird");
strItems[6] = _T("Pigeon");
strItems[7] = _T("Eagle");
strItems[8] = _T("Fish");
strItems[9] = _T("Snapper");
strItems[10] = _T("Sole");
strItems[11] = _T("Salmon");