TreeComboBox3.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // TreeComboBox.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TreeComboBox3.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. // CTreeComboBox3
  13. CTreeComboBox3::CTreeComboBox3()
  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. CTreeComboBox3::~CTreeComboBox3()
  23. {
  24. DeleteObject(m_hBrushAlert);
  25. }
  26. BEGIN_MESSAGE_MAP(CTreeComboBox3, CComboBox)
  27. //{{AFX_MSG_MAP(CTreeComboBox3)
  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. // CTreeComboBox3 message handlers
  36. void CTreeComboBox3::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. }
  50. BOOL CTreeComboBox3::PreTranslateMessage(MSG* pMsg)
  51. {
  52. // TODO: Add your specialized code here and/or call the base class
  53. if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
  54. {
  55. DisplayTree();
  56. return TRUE;
  57. }
  58. return CComboBox::PreTranslateMessage(pMsg);
  59. }
  60. void CTreeComboBox3::OnLButtonDown(UINT nFlags, CPoint point)
  61. {
  62. // TODO: Add your message handler code here and/or call default
  63. m_bTree = ! m_bTree;
  64. if(m_bTree)DisplayTree();
  65. // CComboBox::OnLButtonDown(nFlags, point);
  66. }
  67. void CTreeComboBox3::OnLButtonDblClk(UINT nFlags, CPoint point)
  68. {
  69. // TODO: Add your message handler code here and/or call default
  70. OnLButtonDown(nFlags,point);
  71. // CComboBox::OnLButtonDblClk(nFlags, point);
  72. }
  73. void CTreeComboBox3::TreeCtrlDone()
  74. {
  75. CWnd* pParent = GetParent();
  76. if(pParent != NULL)
  77. {
  78. CString str;GetWindowText(str);
  79. if(m_pretext!=str)
  80. {
  81. m_pretext=str;
  82. WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(),CBN_CLOSEUP);
  83. pParent->SendMessage(WM_COMMAND,wParam,(LPARAM)m_hWnd);
  84. }
  85. }
  86. }
  87. LRESULT CTreeComboBox3::OnCloseControl(WPARAM wParam, LPARAM lParam)
  88. {
  89. m_Tree.ShowWindow(SW_HIDE);
  90. m_bTree = FALSE;
  91. HTREEITEM hItemChanged = (HTREEITEM)lParam;
  92. if(hItemChanged)
  93. {
  94. SetTitle(m_Tree.GetItemText(hItemChanged));
  95. }
  96. AlertBkg(FALSE);
  97. TreeCtrlDone();
  98. return 1;
  99. }
  100. HBRUSH CTreeComboBox3::CtlColor(CDC* pDC, UINT nCtlColor)
  101. {
  102. HBRUSH hbr = NULL;
  103. // TODO: Change any attributes of the DC here
  104. if(nCtlColor == CTLCOLOR_EDIT)
  105. {
  106. if(! m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
  107. else pDC->SetTextColor(COLOR_RED);
  108. if(! m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  109. else pDC->SetBkColor(COLOR_ALERT);
  110. if(m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
  111. pDC->SetBkMode(TRANSPARENT);
  112. }
  113. // TODO: Return a different brush if the default is not desired
  114. return hbr;
  115. }
  116. void CTreeComboBox3::DisplayTree()
  117. {
  118. CRect rc;
  119. GetWindowRect(rc);
  120. rc.top = rc.bottom + 1;
  121. rc.right = GetDroppedWidth();
  122. rc.bottom = GetDroppedHeight();
  123. CString strText(_T(""));
  124. GetWindowText(strText);
  125. m_Tree.Display(rc, strText);
  126. // SetCurSel(-1);
  127. }
  128. void CTreeComboBox3::SetTitle(CString sTitle)
  129. {
  130. ResetContent();
  131. AddString(sTitle);
  132. SetCurSel(0);
  133. }
  134. void CTreeComboBox3::RefDroppedWidth()
  135. {
  136. CRect rect;
  137. GetClientRect(rect);
  138. SetDroppedWidth(rect.Width());
  139. InitTree();
  140. }
  141. void CTreeComboBox3::InitTree()
  142. {
  143. CTreeCtrl& Tree = GetTreeCtrl();
  144. CString bm="xxxyyyzzz";
  145. HTREEITEM parent;
  146. for(int i=0; i<m_pList1array->GetSize (); i++)
  147. {
  148. if(bm!=m_pList1array->ElementAt (i).ElementAt (4))
  149. {
  150. bm=m_pList1array->ElementAt (i).ElementAt (4);
  151. parent = Tree.InsertItem(bm);
  152. Tree.InsertItem("",parent);
  153. Tree.InsertItem(m_pList1array->ElementAt (i).ElementAt (1),parent);
  154. }
  155. else
  156. {
  157. Tree.InsertItem(m_pList1array->ElementAt (i).ElementAt (1),parent);
  158. }
  159. }
  160. }