TreeComboBox.cpp 5.1 KB

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