TreeComboBox3.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #ifdef VC60
  42. DWORD dwStyle = WS_POPUP | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT ;
  43. m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
  44. #else// CTreeCtrl会自己调CWnd的CreateEx传递WC_TREEVIEW
  45. //DWORD dwStyle = WS_POPUPWINDOW | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT ;
  46. DWORD dwStyle = CBS_DROPDOWNLIST | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | WS_POPUP ;
  47. // m_Tree.CreateEx(0, dwStyle, rect, GetParent(), NULL); // VS2008中,CtrlTree不允许调用WS_POPUP或WS_POPUPWINDOW属性,否则Debug模式出错,同时CTreeCtrl调用CreateEx,会再调用Create,而不是CWnd::CreateEx;
  48. ((CWnd&)m_Tree).CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), NULL); // VS2008中,CtrlTree不允许调用WS_POPUP或WS_POPUPWINDOW属性,否则Debug模式出错;
  49. #endif //#ifdef VC60
  50. m_Tree.Init(this);
  51. GetClientRect(rect);
  52. SetDroppedWidth(rect.Width());
  53. SetDroppedHeight(m_droppedHeight);
  54. #ifdef VC60
  55. dwStyle = 0x03 & GetStyle();
  56. ASSERT(dwStyle == CBS_DROPDOWNLIST);
  57. #endif
  58. }
  59. BOOL CTreeComboBox3::PreTranslateMessage(MSG* pMsg)
  60. {
  61. // TODO: Add your specialized code here and/or call the base class
  62. if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
  63. {
  64. DisplayTree();
  65. return TRUE;
  66. }
  67. return CComboBox::PreTranslateMessage(pMsg);
  68. }
  69. void CTreeComboBox3::OnLButtonDown(UINT nFlags, CPoint point)
  70. {
  71. // TODO: Add your message handler code here and/or call default
  72. m_bTree = ! m_bTree;
  73. if(m_bTree)DisplayTree();
  74. // CComboBox::OnLButtonDown(nFlags, point);
  75. }
  76. void CTreeComboBox3::OnLButtonDblClk(UINT nFlags, CPoint point)
  77. {
  78. // TODO: Add your message handler code here and/or call default
  79. OnLButtonDown(nFlags,point);
  80. // CComboBox::OnLButtonDblClk(nFlags, point);
  81. }
  82. void CTreeComboBox3::TreeCtrlDone()
  83. {
  84. CWnd* pParent = GetParent();
  85. if(pParent != NULL)
  86. {
  87. CString str;GetWindowText(str);
  88. if(m_pretext!=str)
  89. {
  90. m_pretext=str;
  91. WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(),CBN_CLOSEUP);
  92. pParent->SendMessage(WM_COMMAND,wParam,(LPARAM)m_hWnd);
  93. }
  94. }
  95. }
  96. LRESULT CTreeComboBox3::OnCloseControl(WPARAM wParam, LPARAM lParam)
  97. {
  98. m_Tree.ShowWindow(SW_HIDE);
  99. m_bTree = FALSE;
  100. HTREEITEM hItemChanged = (HTREEITEM)lParam;
  101. if(hItemChanged)
  102. {
  103. SetTitle(m_Tree.GetItemText(hItemChanged));
  104. }
  105. AlertBkg(FALSE);
  106. TreeCtrlDone();
  107. return 1;
  108. }
  109. HBRUSH CTreeComboBox3::CtlColor(CDC* pDC, UINT nCtlColor)
  110. {
  111. HBRUSH hbr = NULL;
  112. // TODO: Change any attributes of the DC here
  113. if(nCtlColor == CTLCOLOR_EDIT)
  114. {
  115. if(! m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
  116. else pDC->SetTextColor(COLOR_RED);
  117. if(! m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  118. else pDC->SetBkColor(COLOR_ALERT);
  119. if(m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
  120. pDC->SetBkMode(TRANSPARENT);
  121. }
  122. // TODO: Return a different brush if the default is not desired
  123. return hbr;
  124. }
  125. void CTreeComboBox3::DisplayTree()
  126. {
  127. CRect rc;
  128. GetWindowRect(rc);
  129. rc.top = rc.bottom + 1;
  130. rc.right = GetDroppedWidth();
  131. rc.bottom = GetDroppedHeight();
  132. CString strText(_T(""));
  133. GetWindowText(strText);
  134. m_Tree.Display(rc, strText);
  135. // SetCurSel(-1);
  136. }
  137. void CTreeComboBox3::SetTitle(CString sTitle)
  138. {
  139. ResetContent();
  140. AddString(sTitle);
  141. SetCurSel(0);
  142. }
  143. void CTreeComboBox3::RefDroppedWidth()
  144. {
  145. CRect rect;
  146. GetClientRect(rect);
  147. SetDroppedWidth(rect.Width());
  148. InitTree();
  149. }
  150. void CTreeComboBox3::InitTree()
  151. {
  152. CTreeCtrl& Tree = GetTreeCtrl();
  153. CString bm="xxxyyyzzz";
  154. HTREEITEM parent;
  155. for(int i=0; i<m_pList1array->GetSize(); i++)
  156. {
  157. if(bm!=m_pList1array->ElementAt(i).ElementAt(4))
  158. {
  159. bm=m_pList1array->ElementAt(i).ElementAt(4);
  160. parent = Tree.InsertItem(bm);
  161. Tree.InsertItem("",parent);
  162. Tree.InsertItem(m_pList1array->ElementAt(i).ElementAt(1),parent);
  163. }
  164. else
  165. {
  166. Tree.InsertItem(m_pList1array->ElementAt(i).ElementAt(1),parent);
  167. }
  168. }
  169. }