Click here to Skip to main content
15,893,508 members
Articles / Desktop Programming / MFC

Tree ComboBox Control

Rate me:
Please Sign up or sign in to vote.
4.91/5 (18 votes)
16 Oct 2014CPOL1 min read 75.7K   4.8K   68  
Tree ComboBox Control
// TreeComboBox.cpp : implementation file
//

#include "stdafx.h"
#include "TreeComboBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTreeComboBox

CTreeComboBox::CTreeComboBox()
	:m_bControlActive(FALSE)
	,m_bAlertBkg(FALSE)
	,m_bAlertText(FALSE)
	,m_nDroppedHeight(150)
	,m_nDroppedWidth(100)
{
	m_crAlertBkg = GetSysColor(COLOR_WINDOW);
	m_crAlertText = GetSysColor(COLOR_WINDOWTEXT);
	m_BrushAlert.CreateSolidBrush(m_crAlertBkg);
}

CTreeComboBox::~CTreeComboBox()
{
	m_BrushAlert.DeleteObject();
}


BEGIN_MESSAGE_MAP(CTreeComboBox, CComboBox)
	//{{AFX_MSG_MAP(CTreeComboBox)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WMU_CLOSE_CONTROL, OnCloseControl)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeComboBox message handlers

void CTreeComboBox::PreSubclassWindow() 
{
	// TODO: Add your specialized code here and/or call the base class

	CComboBox::PreSubclassWindow();

	CRect rect(0, 0, 0, 0);
	DWORD dwStyle =  WS_POPUP | WS_BORDER;
	CWnd* pWnd = &m_Tree;
	pWnd->CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
	m_Tree.Init(this);

	GetClientRect(rect);
	SetDroppedWidth(rect.Width());
	SetDroppedHeight(m_nDroppedHeight);

	dwStyle = CBS_DROPDOWNLIST & GetStyle();
	ASSERT(CBS_DROPDOWNLIST == dwStyle);
}

BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

	if(WM_KEYDOWN == pMsg->message && (VK_DOWN == pMsg->wParam || VK_F4 == pMsg->wParam))
	{
		DisplayTree();
		return TRUE;
	}

	return CComboBox::PreTranslateMessage(pMsg);
}

void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	m_bControlActive = ! m_bControlActive;
	if(m_bControlActive)
		DisplayTree();

//	CComboBox::OnLButtonDown(nFlags, point);
}

void CTreeComboBox::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	OnLButtonDown(nFlags, point);

//	CComboBox::OnLButtonDblClk(nFlags, point);
}

void CTreeComboBox::TreeCtrlDone()
{
	CWnd* pParent = GetParent();

	if(pParent != NULL)
	{
		WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(), CBN_CLOSEUP);
		pParent->SendMessage(WM_COMMAND, wParam, (LPARAM)m_hWnd);
	}
}

LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
{
	TreeCtrlDone();
	if(NULL != m_Tree.GetSafeHwnd())
	{
		m_Tree.ShowWindow(SW_HIDE);
		CToolTipCtrl* pTooltip = m_Tree.GetToolTips();
		if(NULL != pTooltip)
			pTooltip->Pop();
	}
	m_bControlActive = FALSE;
	if(GetCount())
		SetCurSel(0);
	SetFocus();

	return 1;
}

BOOL CTreeComboBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) 
{
	// TODO: Add your specialized code here and/or call the base class

	if((! m_bAlertText && ! m_bAlertBkg) || WM_CTLCOLOREDIT != message)
		return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);

	HDC hdcChild = (HDC)wParam;
	if(NULL != hdcChild)
	{
		if(m_bAlertText)
			SetTextColor(hdcChild, m_crAlertText);
		if(m_bAlertBkg)
			SetBkColor(hdcChild, m_crAlertBkg);
		*pLResult = (LRESULT)(m_BrushAlert.GetSafeHandle());
	}

	return TRUE;
//	return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
}

void CTreeComboBox::DisplayTree()
{
	CRect rect;
	GetWindowRect(rect);

	int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
	int nDesiredDroppedHeight, nDroppedHeight = m_nDroppedHeight;

	if(NULL != m_Tree.GetSafeHwnd())
	{
		int nCount = 0;
		HTREEITEM hItem = m_Tree.GetRootItem();
		while(NULL != hItem)
		{
			hItem = m_Tree.GetNextSiblingItem(hItem);
			nCount++;
		}
		if(nCount > 1)
		{
			nDesiredDroppedHeight = nCount * (m_Tree.GetItemHeight() + GetSystemMetrics(SM_CXEDGE) / 2);
			if(nDesiredDroppedHeight > nScreenHeight / 2)
				nDesiredDroppedHeight = nScreenHeight / 2;
			if(nDroppedHeight < nDesiredDroppedHeight)
				nDroppedHeight = nDesiredDroppedHeight;
		}
	}

	if(rect.bottom + nDroppedHeight + GetSystemMetrics(SM_CYCAPTION) > nScreenHeight)
	{
		rect.top = rect.top - GetSystemMetrics(SM_CXEDGE) - nDroppedHeight;
		rect.bottom = nDroppedHeight + 1;
	}
	else
	{
		rect.top = rect.bottom;
		rect.bottom = nDroppedHeight;
	}
	rect.right = m_nDroppedWidth;

	m_Tree.Display(rect);
	SetCurSel(-1);
}

void CTreeComboBox::SetTitle(CString sTitle)
{
	if(GetCount())
	{
		SetCurSel(0);
		return;
	}

	ResetContent();
	AddString(sTitle);
	SetCurSel(0);
}

void CTreeComboBox::SetAlertColorBkg(const COLORREF crColor)
{
	m_crAlertBkg = crColor;
	m_BrushAlert.DeleteObject();
	m_BrushAlert.CreateSolidBrush(m_crAlertBkg);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions