TreeComboBox.cpp 4.9 KB

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