TreeComboBox.cpp 4.8 KB

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