123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- // 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_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)
- //{{AFX_MSG_MAP(CTreeComboBox)
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_CTLCOLOR_REFLECT()
- //}}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_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());
- }
- /************************************************************************/
- /* 函数:InsertMyItem[12/7/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [IN] :;
- /* 返回:void;
- /* 注意:只适合二层目录;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- 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);
- }
- }
- }
|