Recently, I needed a tree control inside a
combobox, and during my search on Google I found something, but it was a little more complicated than I required. I started to draft one myself, and am sharing the result with you.
Using the Code
Everything is based on
CTreeCom<code>boBox, which is derived from standard
CComboBox. Here I have
CComboTreeCtrl variable member and methods characteristic to tree control. In
CTreeComboBox::PreSubclassWindow, I create and initialize
m_Tree and stretch on
combobox dropdown list.
CRect rect(0, 0, 0, 0);
DWORD dwStyle = WS_POPUP | WS_BORDER | TVS_DISABLEDRAGDROP |
TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT |
m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
dwStyle = 0x03 & GetStyle();
ASSERT(dwStyle == CBS_DROPDOWNLIST);
At first click, or arrow key down, the tree window will open.
void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point)
m_bTree = ! m_bTree;
BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg)
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
But tree window should close at second click, escape key, enter key or lost focus. I tried a few ways but I failed, so my solution was to send to the parent (
CTreeComboBox) message to close.
LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
m_bTree = FALSE;
ComboBox must setup with
CBS_DROPDOWNLIST or else you'll be ASSERT :)
In order to use this control , you need include #include "TreeComboBox.h" and further handle
combobox like it is a tree control (you can see how in the sample project).
- 26th April, 2011: Initial version
- 20th February, 2012: Added
GetTreeCtrl() method to
CTreeComboBox class, so now it has full access to tree control