| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 | 
							
- #include "stdafx.h"
 
- #include "TreeComboBox.h"
 
- #ifdef _DEBUG
 
- #define new DEBUG_NEW
 
- #undef THIS_FILE
 
- static char THIS_FILE[] = __FILE__;
 
- #endif
 
- CTreeComboBox::CTreeComboBox()
 
- 	:m_bTree(FALSE)
 
- 	, m_bAlertBkg(FALSE)
 
- 	, m_bAlertText(FALSE)
 
- 	, m_droppedHeight(250)
 
- 	, m_droppedWidth(100)
 
- {
 
- 	m_hBrushAlert = CreateSolidBrush(COLOR_ALERT);
 
- }
 
- CTreeComboBox::~CTreeComboBox()
 
- {
 
- 	DeleteObject(m_hBrushAlert);
 
- }
 
- BEGIN_MESSAGE_MAP(CTreeComboBox, CComboBox)
 
- 	
 
- 	ON_WM_LBUTTONDOWN()
 
- 	ON_WM_LBUTTONDBLCLK()
 
- 	ON_WM_CTLCOLOR_REFLECT()
 
- 	
 
- 	ON_MESSAGE(WMU_CLOSE_CONTROL, OnCloseControl)
 
- END_MESSAGE_MAP()
 
- void CTreeComboBox::PreSubclassWindow()
 
- {
 
- 	
 
- 	CComboBox::PreSubclassWindow();
 
- 	CRect rect(0, 0, 0, 0);
 
- 	DWORD dwStyle = WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | WS_POPUPWINDOW;
 
- 	((CWnd&)m_Tree).CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), NULL);
 
- 	m_Tree.Init(this);
 
- 	GetClientRect(rect);
 
- 	SetDroppedWidth(rect.Width());
 
- 	SetDroppedHeight(m_droppedHeight);
 
- }
 
- BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg)
 
- {
 
- 	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
 
- 	{
 
- 		DisplayTree();
 
- 		return TRUE;
 
- 	}
 
- 	return CComboBox::PreTranslateMessage(pMsg);
 
- }
 
- void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point)
 
- {
 
- 	m_bTree = !m_bTree;
 
- 	if (m_bTree)
 
- 		DisplayTree();
 
- }
 
- void CTreeComboBox::OnLButtonDblClk(UINT nFlags, CPoint point)
 
- {
 
- 	OnLButtonDown(nFlags, point);
 
- }
 
- void CTreeComboBox::TreeCtrlDone()
 
- {
 
- 	CWnd* pParent = GetParent();
 
- 	if (pParent != NULL)
 
- 	{
 
- 		CString str; GetWindowText(str);
 
- 		if (m_pretext != str)
 
- 		{
 
- 			m_pretext = str;
 
- 			WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(), CBN_CLOSEUP);
 
- 			pParent->SendMessage(WM_COMMAND, wParam, (LPARAM)m_hWnd);
 
- 		}
 
- 	}
 
- }
 
- LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
 
- {
 
- 	m_Tree.ShowWindow(SW_HIDE);
 
- 	m_bTree = FALSE;
 
- 	HTREEITEM hItemChanged = (HTREEITEM)lParam;
 
- 	if (hItemChanged)
 
- 	{
 
- 		SetTitle(m_Tree.GetItemText(hItemChanged));
 
- 	}
 
- 	AlertBkg(FALSE);
 
- 	TreeCtrlDone();
 
- 	return 1;
 
- }
 
- HBRUSH CTreeComboBox::CtlColor(CDC* pDC, UINT nCtlColor)
 
- {
 
- 	HBRUSH hbr = NULL;
 
- 	if (nCtlColor == CTLCOLOR_EDIT)
 
- 	{
 
- 		if (!m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
 
- 		else pDC->SetTextColor(COLOR_RED);
 
- 		if (!m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
 
- 		else pDC->SetBkColor(COLOR_ALERT);
 
- 		if (m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
 
- 		pDC->SetBkMode(TRANSPARENT);
 
- 	}
 
- 	return hbr;
 
- }
 
- void CTreeComboBox::DisplayTree()
 
- {
 
- 	CRect rc;
 
- 	GetWindowRect(rc);
 
- 	rc.top = rc.bottom + 1;
 
- 	rc.right = GetDroppedWidth();
 
- 	rc.bottom = GetDroppedHeight();
 
- 	m_Tree.Display(rc);
 
- }
 
- void CTreeComboBox::SetTitle(CString sTitle)
 
- {
 
- 	ResetContent();
 
- 	AddString(sTitle);
 
- 	SetCurSel(0);
 
- }
 
- void CTreeComboBox::RefDroppedWidth()
 
- {
 
- 	CRect rect;
 
- 	GetClientRect(rect);
 
- 	SetDroppedWidth(rect.Width());
 
- }
 
- void CTreeComboBox::InsertMyItem(IN CString strRoot, IN CString strItem)
 
- {
 
- 	BOOL bExist = FALSE;
 
- 	HTREEITEM hRoot = m_Tree.GetRootItem();
 
- 	if (hRoot == NULL)
 
- 	{
 
- 		hRoot = m_Tree.InsertItem(strRoot, NULL);
 
- 		m_Tree.InsertItem(strItem, hRoot);
 
- 		return;
 
- 	}
 
- 	CString strName = m_Tree.GetItemText(hRoot);
 
- 	if (strName == strRoot)
 
- 	{
 
- 		HTREEITEM hSubItem = m_Tree.GetChildItem(hRoot);
 
- 		
 
- 		while (hSubItem)
 
- 		{
 
- 			
 
- 			if (strItem == m_Tree.GetItemText(hSubItem))
 
- 			{
 
- 				bExist = TRUE;
 
- 				break;
 
- 			}
 
- 			
 
- 			hSubItem = m_Tree.GetNextSiblingItem(hSubItem);
 
- 		}
 
- 		if (!bExist)
 
- 		{
 
- 			
 
- 			m_Tree.InsertItem(strItem, hRoot);
 
- 		}
 
- 	}
 
- 	else
 
- 	{
 
- 		hRoot = m_Tree.GetNextSiblingItem(hRoot);
 
- 		while (hRoot)
 
- 		{
 
- 			
 
- 			if (strRoot == m_Tree.GetItemText(hRoot))
 
- 			{
 
- 				HTREEITEM hSubItem = m_Tree.GetChildItem(hRoot);
 
- 				
 
- 				while (hSubItem)
 
- 				{
 
- 					
 
- 					if (strItem == m_Tree.GetItemText(hSubItem))
 
- 					{
 
- 						bExist = TRUE;
 
- 						break;
 
- 					}
 
- 					
 
- 					hSubItem = m_Tree.GetNextSiblingItem(hSubItem);
 
- 				}
 
- 				if (!bExist)
 
- 				{
 
- 					
 
- 					bExist = TRUE;
 
- 					m_Tree.InsertItem(strItem, hRoot);
 
- 				}
 
- 				break;
 
- 			}
 
- 			
 
- 			hRoot = m_Tree.GetNextSiblingItem(hRoot);
 
- 		}
 
- 		if (!bExist)
 
- 		{
 
- 			
 
- 			hRoot = m_Tree.InsertItem(strRoot, NULL);
 
- 			m_Tree.InsertItem(strItem, hRoot);
 
- 		}
 
- 	}
 
- }
 
 
  |