TreeComboBox2.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // TreeComboBox2.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TreeComboBox2.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. #define WM_USER_ComboBox_END WM_USER+1001
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CTreeComboBox2
  14. CTreeComboBox2::CTreeComboBox2()
  15. :m_bTree(FALSE)
  16. ,m_bAlertBkg(FALSE)
  17. ,m_bAlertText(FALSE)
  18. ,m_droppedHeight(250)
  19. ,m_droppedWidth(100)
  20. {
  21. m_hBrushAlert = CreateSolidBrush(COLOR_ALERT);
  22. m_pMsgParent=NULL;
  23. m_mode=0;
  24. }
  25. CTreeComboBox2::~CTreeComboBox2()
  26. {
  27. DeleteObject(m_hBrushAlert);
  28. }
  29. BEGIN_MESSAGE_MAP(CTreeComboBox2, CComboBox)
  30. //{{AFX_MSG_MAP(CTreeComboBox2)
  31. ON_WM_LBUTTONDOWN()
  32. ON_WM_LBUTTONDBLCLK()
  33. ON_WM_CTLCOLOR_REFLECT()
  34. ON_WM_SETFOCUS()
  35. //}}AFX_MSG_MAP
  36. ON_MESSAGE(WMU_CLOSE_CONTROL,OnCloseControl)
  37. END_MESSAGE_MAP()
  38. void CTreeComboBox2::OnSetFocus(CWnd* pOldWnd)
  39. {
  40. CComboBox::OnSetFocus(pOldWnd);
  41. m_bExchange = TRUE;
  42. }
  43. void CTreeComboBox2::SetCtrlData(DWORD dwData)
  44. {
  45. m_dwData = dwData;
  46. }
  47. DWORD CTreeComboBox2::GetCtrlData()
  48. {
  49. return m_dwData;
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CTreeComboBox2 message handlers
  53. void CTreeComboBox2::PreSubclassWindow()
  54. {
  55. // TODO: Add your specialized code here and/or call the base class
  56. CComboBox::PreSubclassWindow();
  57. CRect rect(0, 0, 0, 0);
  58. #ifdef VC60
  59. DWORD dwStyle = WS_POPUP | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT ;
  60. m_Tree.CreateEx(0, WC_TREEVIEW, NULL, dwStyle, rect, GetParent(), 0, NULL);
  61. #else// CTreeCtrl会自己调CWnd的CreateEx传递WC_TREEVIEW
  62. //DWORD dwStyle = WS_POPUPWINDOW | WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT ;
  63. DWORD dwStyle = WS_BORDER | TVS_DISABLEDRAGDROP | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_FULLROWSELECT | WS_POPUPWINDOW;
  64. // m_Tree.CreateEx(0, dwStyle, rect, GetParent(), NULL); // VS2008中,CtrlTree不允许调用WS_POPUP或WS_POPUPWINDOW属性,否则Debug模式出错,同时CTreeCtrl调用CreateEx,会再调用Create,而不是CWnd::CreateEx;
  65. ((CWnd&)m_Tree).CreateEx(0, WC_TREEVIEW,NULL, dwStyle, rect, GetParent(), NULL); // VS2008中,CtrlTree不允许调用WS_POPUP或WS_POPUPWINDOW属性,否则Debug模式出错;
  66. #endif //#ifdef VC60
  67. m_Tree.Init(this);
  68. GetClientRect(rect);
  69. SetDroppedWidth(rect.Width());
  70. SetDroppedHeight(m_droppedHeight);
  71. InitTree();
  72. }
  73. BOOL CTreeComboBox2::PreTranslateMessage(MSG* pMsg)
  74. {
  75. // TODO: Add your specialized code here and/or call the base class
  76. if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
  77. {
  78. DisplayTree();
  79. return TRUE;
  80. }
  81. /* if(pMsg->message == WM_KEYDOWN)
  82. {
  83. if(pMsg->wParam == VK_RETURN)
  84. {
  85. CWnd* pParent = this->GetParent();
  86. m_bExchange = TRUE;
  87. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_EDIT_END,m_bExchange,1);
  88. }
  89. else if(pMsg->wParam == VK_ESCAPE)
  90. {
  91. CWnd* pParent = this->GetParent();
  92. m_bExchange = FALSE;
  93. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_EDIT_END,m_bExchange,1);
  94. }
  95. }*/
  96. return CComboBox::PreTranslateMessage(pMsg);
  97. }
  98. void CTreeComboBox2::OnLButtonDown(UINT nFlags, CPoint point)
  99. {
  100. // TODO: Add your message handler code here and/or call default
  101. m_bTree = ! m_bTree;
  102. if(m_bTree)
  103. {
  104. DisplayTree();
  105. }
  106. // CComboBox::OnLButtonDown(nFlags, point);
  107. }
  108. void CTreeComboBox2::OnLButtonDblClk(UINT nFlags, CPoint point)
  109. {
  110. // TODO: Add your message handler code here and/or call default
  111. OnLButtonDown(nFlags,point);
  112. // CComboBox::OnLButtonDblClk(nFlags, point);
  113. }
  114. void CTreeComboBox2::TreeCtrlDone()
  115. {
  116. CWnd* pParent = GetParent();
  117. if(pParent != NULL)
  118. {
  119. CString str;GetWindowText(str);
  120. if(m_pretext!=str)
  121. {
  122. m_pretext=str;
  123. WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(),CBN_CLOSEUP);
  124. pParent->SendMessage(WM_COMMAND,wParam,(LPARAM)m_hWnd);
  125. }
  126. }
  127. }
  128. LRESULT CTreeComboBox2::OnCloseControl(WPARAM wParam, LPARAM lParam)
  129. {
  130. TreeCtrlDone();
  131. m_Tree.ShowWindow(SW_HIDE);
  132. m_bTree = FALSE;
  133. HTREEITEM hItemChanged = (HTREEITEM)lParam;
  134. if(hItemChanged)
  135. {
  136. SetTitle(m_Tree.GetItemText(hItemChanged));
  137. }
  138. AlertBkg(FALSE);
  139. if(m_pMsgParent)
  140. {
  141. if(m_mode)
  142. ::PostMessage(m_pMsgParent->GetSafeHwnd(),WM_USER+1003,m_bExchange,0);
  143. else
  144. ::PostMessage(m_pMsgParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  145. }
  146. return 1;
  147. }
  148. HBRUSH CTreeComboBox2::CtlColor(CDC* pDC, UINT nCtlColor)
  149. {
  150. HBRUSH hbr = NULL;
  151. // TODO: Change any attributes of the DC here
  152. if(nCtlColor == CTLCOLOR_EDIT)
  153. {
  154. if(! m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
  155. else pDC->SetTextColor(COLOR_RED);
  156. if(! m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  157. else pDC->SetBkColor(COLOR_ALERT);
  158. if(m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
  159. pDC->SetBkMode(TRANSPARENT);
  160. }
  161. // TODO: Return a different brush if the default is not desired
  162. return hbr;
  163. }
  164. void CTreeComboBox2::DisplayTree()
  165. {
  166. CRect rc;
  167. GetWindowRect(rc);
  168. rc.top = rc.bottom + 1;
  169. rc.right = GetDroppedWidth();
  170. rc.bottom = GetDroppedHeight();
  171. CString strText(_T(""));
  172. GetWindowText(strText);
  173. m_Tree.Display(rc, strText);
  174. // SetCurSel(-1);
  175. }
  176. void CTreeComboBox2::SetTitle(CString sTitle)
  177. {
  178. ResetContent();
  179. AddString(sTitle);
  180. SetCurSel(0);
  181. }
  182. void CTreeComboBox2::RefDroppedWidth()
  183. {
  184. CRect rect;
  185. GetClientRect(rect);
  186. SetDroppedWidth(rect.Width());
  187. }
  188. void CTreeComboBox2::InitTree()
  189. {
  190. CTreeCtrl& Tree = GetTreeCtrl();
  191. CString bm="xxxyyyzzz";
  192. HTREEITEM parent;
  193. for(int i=0; i<g_userarray.GetSize (); i++)
  194. {
  195. if(bm!=g_userarray.ElementAt (i).ElementAt (2))
  196. {
  197. bm=g_userarray.ElementAt (i).ElementAt (2);
  198. parent = Tree.InsertItem(bm);
  199. Tree.InsertItem("",parent);
  200. Tree.InsertItem(g_userarray.ElementAt (i).ElementAt (1),parent);
  201. }
  202. else
  203. {
  204. Tree.InsertItem(g_userarray.ElementAt (i).ElementAt (1),parent);
  205. }
  206. }
  207. }