TreeComboBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // TreeComboBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TreeComboBox.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CTreeComboBox
  12. CTreeComboBox::CTreeComboBox()
  13. :m_bTree(FALSE)
  14. , m_bAlertBkg(FALSE)
  15. , m_bAlertText(FALSE)
  16. , m_droppedHeight(250)
  17. , m_droppedWidth(100)
  18. {
  19. m_hBrushAlert = CreateSolidBrush(COLOR_ALERT);
  20. }
  21. CTreeComboBox::~CTreeComboBox()
  22. {
  23. DeleteObject(m_hBrushAlert);
  24. }
  25. BEGIN_MESSAGE_MAP(CTreeComboBox, CComboBox)
  26. //{{AFX_MSG_MAP(CTreeComboBox)
  27. ON_WM_LBUTTONDOWN()
  28. ON_WM_LBUTTONDBLCLK()
  29. ON_WM_CTLCOLOR_REFLECT()
  30. //}}AFX_MSG_MAP
  31. ON_MESSAGE(WMU_CLOSE_CONTROL, OnCloseControl)
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CTreeComboBox message handlers
  35. void CTreeComboBox::PreSubclassWindow()
  36. {
  37. // TODO: Add your specialized code here and/or call the base class
  38. CComboBox::PreSubclassWindow();
  39. CRect rect(0, 0, 0, 0);
  40. DWORD dwStyle = WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | WS_POPUPWINDOW;
  41. ((CWnd&)m_Tree).CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), NULL);
  42. m_Tree.Init(this);
  43. GetClientRect(rect);
  44. SetDroppedWidth(rect.Width());
  45. SetDroppedHeight(m_droppedHeight);
  46. }
  47. BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg)
  48. {
  49. if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
  50. {
  51. DisplayTree();
  52. return TRUE;
  53. }
  54. return CComboBox::PreTranslateMessage(pMsg);
  55. }
  56. void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point)
  57. {
  58. m_bTree = !m_bTree;
  59. if (m_bTree)
  60. DisplayTree();
  61. }
  62. void CTreeComboBox::OnLButtonDblClk(UINT nFlags, CPoint point)
  63. {
  64. OnLButtonDown(nFlags, point);
  65. }
  66. void CTreeComboBox::TreeCtrlDone()
  67. {
  68. CWnd* pParent = GetParent();
  69. if (pParent != NULL)
  70. {
  71. CString str; GetWindowText(str);
  72. if (m_pretext != str)
  73. {
  74. m_pretext = str;
  75. WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(), CBN_CLOSEUP);
  76. pParent->SendMessage(WM_COMMAND, wParam, (LPARAM)m_hWnd);
  77. }
  78. }
  79. }
  80. LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
  81. {
  82. m_Tree.ShowWindow(SW_HIDE);
  83. m_bTree = FALSE;
  84. HTREEITEM hItemChanged = (HTREEITEM)lParam;
  85. if (hItemChanged)
  86. {
  87. SetTitle(m_Tree.GetItemText(hItemChanged));
  88. }
  89. AlertBkg(FALSE);
  90. TreeCtrlDone();
  91. return 1;
  92. }
  93. HBRUSH CTreeComboBox::CtlColor(CDC* pDC, UINT nCtlColor)
  94. {
  95. HBRUSH hbr = NULL;
  96. if (nCtlColor == CTLCOLOR_EDIT)
  97. {
  98. if (!m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
  99. else pDC->SetTextColor(COLOR_RED);
  100. if (!m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  101. else pDC->SetBkColor(COLOR_ALERT);
  102. if (m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
  103. pDC->SetBkMode(TRANSPARENT);
  104. }
  105. return hbr;
  106. }
  107. void CTreeComboBox::DisplayTree()
  108. {
  109. CRect rc;
  110. GetWindowRect(rc);
  111. rc.top = rc.bottom + 1;
  112. rc.right = GetDroppedWidth();
  113. rc.bottom = GetDroppedHeight();
  114. m_Tree.Display(rc);
  115. }
  116. void CTreeComboBox::SetTitle(CString sTitle)
  117. {
  118. ResetContent();
  119. AddString(sTitle);
  120. SetCurSel(0);
  121. }
  122. void CTreeComboBox::RefDroppedWidth()
  123. {
  124. CRect rect;
  125. GetClientRect(rect);
  126. SetDroppedWidth(rect.Width());
  127. }
  128. /************************************************************************/
  129. /* 函数:InsertMyItem[12/7/2016 IT];
  130. /* 描述:;
  131. /* 参数:;
  132. /* [IN] :;
  133. /* [IN] :;
  134. /* 返回:void;
  135. /* 注意:只适合二层目录;
  136. /* 示例:;
  137. /*
  138. /* 修改:;
  139. /* 日期:;
  140. /* 内容:;
  141. /************************************************************************/
  142. void CTreeComboBox::InsertMyItem(IN CString strRoot, IN CString strItem)
  143. {
  144. BOOL bExist = FALSE;
  145. HTREEITEM hRoot = m_Tree.GetRootItem();
  146. if (hRoot == NULL)
  147. {
  148. hRoot = m_Tree.InsertItem(strRoot, NULL);
  149. m_Tree.InsertItem(strItem, hRoot);
  150. return;
  151. }
  152. CString strName = m_Tree.GetItemText(hRoot);
  153. if (strName == strRoot)
  154. {
  155. HTREEITEM hSubItem = m_Tree.GetChildItem(hRoot);
  156. // 遍历所有子项;
  157. while (hSubItem)
  158. {
  159. // 当前子项是否是要查找的子项;
  160. if (strItem == m_Tree.GetItemText(hSubItem))
  161. {
  162. bExist = TRUE;
  163. break;
  164. }
  165. // 若未到,查找下一个兄弟项;
  166. hSubItem = m_Tree.GetNextSiblingItem(hSubItem);
  167. }
  168. if (!bExist)
  169. {
  170. // 插入新项;
  171. m_Tree.InsertItem(strItem, hRoot);
  172. }
  173. }
  174. else
  175. {
  176. hRoot = m_Tree.GetNextSiblingItem(hRoot);
  177. while (hRoot)
  178. {
  179. // 当前兄弟项是否是要查找的子项;
  180. if (strRoot == m_Tree.GetItemText(hRoot))
  181. {
  182. HTREEITEM hSubItem = m_Tree.GetChildItem(hRoot);
  183. // 遍历所有子项;
  184. while (hSubItem)
  185. {
  186. // 当前子项是否是要查找的子项;
  187. if (strItem == m_Tree.GetItemText(hSubItem))
  188. {
  189. bExist = TRUE;
  190. break;
  191. }
  192. // 若未到,查找下一个兄弟项;
  193. hSubItem = m_Tree.GetNextSiblingItem(hSubItem);
  194. }
  195. if (!bExist)
  196. {
  197. // 插入新项;
  198. bExist = TRUE;
  199. m_Tree.InsertItem(strItem, hRoot);
  200. }
  201. break;
  202. }
  203. // 若未到,查找下一个兄弟项;
  204. hRoot = m_Tree.GetNextSiblingItem(hRoot);
  205. }
  206. if (!bExist)
  207. {
  208. // 插入新项;
  209. hRoot = m_Tree.InsertItem(strRoot, NULL);
  210. m_Tree.InsertItem(strItem, hRoot);
  211. }
  212. }
  213. }