TreeComboBox.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // TreeComboBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TreeComboBox.h"
  5. #include "ylgl.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CTreeComboBox
  13. CTreeComboBox::CTreeComboBox()
  14. :m_bTree(FALSE)
  15. ,m_bAlertBkg(FALSE)
  16. ,m_bAlertText(FALSE)
  17. ,m_droppedHeight(250)
  18. ,m_droppedWidth(100)
  19. {
  20. m_hBrushAlert = CreateSolidBrush(COLOR_ALERT);
  21. }
  22. CTreeComboBox::~CTreeComboBox()
  23. {
  24. DeleteObject(m_hBrushAlert);
  25. }
  26. BEGIN_MESSAGE_MAP(CTreeComboBox, CComboBox)
  27. //{{AFX_MSG_MAP(CTreeComboBox)
  28. ON_WM_LBUTTONDOWN()
  29. ON_WM_LBUTTONDBLCLK()
  30. ON_WM_CTLCOLOR_REFLECT()
  31. //}}AFX_MSG_MAP
  32. ON_MESSAGE(WMU_CLOSE_CONTROL,OnCloseControl)
  33. END_MESSAGE_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CTreeComboBox message handlers
  36. void CTreeComboBox::PreSubclassWindow()
  37. {
  38. // TODO: Add your specialized code here and/or call the base class
  39. CComboBox::PreSubclassWindow();
  40. CRect rect(0, 0, 0, 0);
  41. DWORD dwStyle = WS_POPUP | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT ;
  42. m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
  43. m_Tree.Init(this);
  44. GetClientRect(rect);
  45. SetDroppedWidth(rect.Width());
  46. SetDroppedHeight(m_droppedHeight);
  47. dwStyle = 0x03 & GetStyle();
  48. ASSERT(dwStyle == CBS_DROPDOWNLIST);
  49. InitTree();
  50. }
  51. BOOL CTreeComboBox::PreTranslateMessage(MSG* pMsg)
  52. {
  53. // TODO: Add your specialized code here and/or call the base class
  54. if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
  55. {
  56. DisplayTree();
  57. return TRUE;
  58. }
  59. return CComboBox::PreTranslateMessage(pMsg);
  60. }
  61. void CTreeComboBox::OnLButtonDown(UINT nFlags, CPoint point)
  62. {
  63. // TODO: Add your message handler code here and/or call default
  64. m_bTree = ! m_bTree;
  65. if(m_bTree)DisplayTree();
  66. // CComboBox::OnLButtonDown(nFlags, point);
  67. }
  68. void CTreeComboBox::OnLButtonDblClk(UINT nFlags, CPoint point)
  69. {
  70. // TODO: Add your message handler code here and/or call default
  71. OnLButtonDown(nFlags,point);
  72. // CComboBox::OnLButtonDblClk(nFlags, point);
  73. }
  74. void CTreeComboBox::TreeCtrlDone()
  75. {
  76. CWnd* pParent = GetParent();
  77. if(pParent != NULL)
  78. {
  79. CString str;GetWindowText(str);
  80. if(m_pretext!=str)
  81. {
  82. m_pretext=str;
  83. WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(),CBN_CLOSEUP);
  84. pParent->SendMessage(WM_COMMAND,wParam,(LPARAM)m_hWnd);
  85. }
  86. }
  87. }
  88. LRESULT CTreeComboBox::OnCloseControl(WPARAM wParam, LPARAM lParam)
  89. {
  90. m_Tree.ShowWindow(SW_HIDE);
  91. m_bTree = FALSE;
  92. HTREEITEM hItemChanged = (HTREEITEM)lParam;
  93. if(hItemChanged)
  94. {
  95. SetTitle(m_Tree.GetItemText(hItemChanged));
  96. }
  97. AlertBkg(FALSE);
  98. TreeCtrlDone();
  99. return 1;
  100. }
  101. HBRUSH CTreeComboBox::CtlColor(CDC* pDC, UINT nCtlColor)
  102. {
  103. HBRUSH hbr = NULL;
  104. // TODO: Change any attributes of the DC here
  105. if(nCtlColor == CTLCOLOR_EDIT)
  106. {
  107. if(! m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
  108. else pDC->SetTextColor(COLOR_RED);
  109. if(! m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  110. else pDC->SetBkColor(COLOR_ALERT);
  111. if(m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
  112. pDC->SetBkMode(TRANSPARENT);
  113. }
  114. // TODO: Return a different brush if the default is not desired
  115. return hbr;
  116. }
  117. void CTreeComboBox::DisplayTree()
  118. {
  119. CRect rc;
  120. GetWindowRect(rc);
  121. rc.top = rc.bottom + 1;
  122. rc.right = GetDroppedWidth();
  123. rc.bottom = GetDroppedHeight();
  124. CString strText(_T(""));
  125. GetWindowText(strText);
  126. m_Tree.Display(rc, strText);
  127. // SetCurSel(-1);
  128. }
  129. void CTreeComboBox::SetTitle(CString sTitle)
  130. {
  131. ResetContent();
  132. AddString(sTitle);
  133. SetCurSel(0);
  134. }
  135. void CTreeComboBox::RefDroppedWidth()
  136. {
  137. CRect rect;
  138. GetClientRect(rect);
  139. SetDroppedWidth(rect.Width());
  140. }
  141. void CTreeComboBox::InitTree()
  142. {
  143. CTreeCtrl& Tree = GetTreeCtrl();
  144. CString bm="xxxyyyzzz";
  145. HTREEITEM parent;
  146. for(int i=0; i<g_userarray.GetSize (); i++)
  147. {
  148. if(bm!=g_userarray.ElementAt (i).ElementAt (2))
  149. {
  150. bm=g_userarray.ElementAt (i).ElementAt (2);
  151. parent = Tree.InsertItem(bm);
  152. Tree.InsertItem("",parent);
  153. Tree.InsertItem(g_userarray.ElementAt (i).ElementAt (1),parent);
  154. }
  155. else
  156. {
  157. Tree.InsertItem(g_userarray.ElementAt (i).ElementAt (1),parent);
  158. }
  159. }
  160. }