TreeComboBox2.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. dwStyle = 0x03 & GetStyle();
  72. ASSERT(dwStyle == CBS_DROPDOWNLIST);
  73. InitTree();
  74. }
  75. BOOL CTreeComboBox2::PreTranslateMessage(MSG* pMsg)
  76. {
  77. // TODO: Add your specialized code here and/or call the base class
  78. if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DOWN)
  79. {
  80. DisplayTree();
  81. return TRUE;
  82. }
  83. /* if(pMsg->message == WM_KEYDOWN)
  84. {
  85. if(pMsg->wParam == VK_RETURN)
  86. {
  87. CWnd* pParent = this->GetParent();
  88. m_bExchange = TRUE;
  89. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_EDIT_END,m_bExchange,1);
  90. }
  91. else if(pMsg->wParam == VK_ESCAPE)
  92. {
  93. CWnd* pParent = this->GetParent();
  94. m_bExchange = FALSE;
  95. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_EDIT_END,m_bExchange,1);
  96. }
  97. }*/
  98. return CComboBox::PreTranslateMessage(pMsg);
  99. }
  100. void CTreeComboBox2::OnLButtonDown(UINT nFlags, CPoint point)
  101. {
  102. // TODO: Add your message handler code here and/or call default
  103. m_bTree = ! m_bTree;
  104. if(m_bTree)
  105. {
  106. DisplayTree();
  107. }
  108. // CComboBox::OnLButtonDown(nFlags, point);
  109. }
  110. void CTreeComboBox2::OnLButtonDblClk(UINT nFlags, CPoint point)
  111. {
  112. // TODO: Add your message handler code here and/or call default
  113. OnLButtonDown(nFlags,point);
  114. // CComboBox::OnLButtonDblClk(nFlags, point);
  115. }
  116. void CTreeComboBox2::TreeCtrlDone()
  117. {
  118. CWnd* pParent = GetParent();
  119. if(pParent != NULL)
  120. {
  121. CString str;GetWindowText(str);
  122. if(m_pretext!=str)
  123. {
  124. m_pretext=str;
  125. WPARAM wParam = MAKEWPARAM(GetDlgCtrlID(),CBN_CLOSEUP);
  126. pParent->SendMessage(WM_COMMAND,wParam,(LPARAM)m_hWnd);
  127. }
  128. }
  129. }
  130. LRESULT CTreeComboBox2::OnCloseControl(WPARAM wParam, LPARAM lParam)
  131. {
  132. TreeCtrlDone();
  133. m_Tree.ShowWindow(SW_HIDE);
  134. m_bTree = FALSE;
  135. HTREEITEM hItemChanged = (HTREEITEM)lParam;
  136. if(hItemChanged)
  137. {
  138. SetTitle(m_Tree.GetItemText(hItemChanged));
  139. }
  140. AlertBkg(FALSE);
  141. if(m_pMsgParent)
  142. {
  143. if(m_mode)
  144. ::PostMessage(m_pMsgParent->GetSafeHwnd(),WM_USER+1003,m_bExchange,0);
  145. else
  146. ::PostMessage(m_pMsgParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  147. }
  148. return 1;
  149. }
  150. HBRUSH CTreeComboBox2::CtlColor(CDC* pDC, UINT nCtlColor)
  151. {
  152. HBRUSH hbr = NULL;
  153. // TODO: Change any attributes of the DC here
  154. if(nCtlColor == CTLCOLOR_EDIT)
  155. {
  156. if(! m_bAlertText)pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
  157. else pDC->SetTextColor(COLOR_RED);
  158. if(! m_bAlertBkg)pDC->SetBkColor(GetSysColor(COLOR_WINDOW));
  159. else pDC->SetBkColor(COLOR_ALERT);
  160. if(m_bAlertText || m_bAlertBkg)hbr = m_hBrushAlert;
  161. pDC->SetBkMode(TRANSPARENT);
  162. }
  163. // TODO: Return a different brush if the default is not desired
  164. return hbr;
  165. }
  166. void CTreeComboBox2::DisplayTree()
  167. {
  168. CRect rc;
  169. GetWindowRect(rc);
  170. rc.top = rc.bottom + 1;
  171. rc.right = GetDroppedWidth();
  172. rc.bottom = GetDroppedHeight();
  173. CString strText(_T(""));
  174. GetWindowText(strText);
  175. m_Tree.Display(rc, strText);
  176. // SetCurSel(-1);
  177. }
  178. void CTreeComboBox2::SetTitle(CString sTitle)
  179. {
  180. ResetContent();
  181. AddString(sTitle);
  182. SetCurSel(0);
  183. }
  184. void CTreeComboBox2::RefDroppedWidth()
  185. {
  186. CRect rect;
  187. GetClientRect(rect);
  188. SetDroppedWidth(rect.Width());
  189. }
  190. void CTreeComboBox2::InitTree()
  191. {
  192. CTreeCtrl& Tree = GetTreeCtrl();
  193. CString bm="xxxyyyzzz";
  194. HTREEITEM parent;
  195. for(int i=0; i<g_userarray.GetSize (); i++)
  196. {
  197. if(bm!=g_userarray.ElementAt (i).ElementAt (2))
  198. {
  199. bm=g_userarray.ElementAt (i).ElementAt (2);
  200. parent = Tree.InsertItem(bm);
  201. Tree.InsertItem("",parent);
  202. Tree.InsertItem(g_userarray.ElementAt (i).ElementAt (1),parent);
  203. }
  204. else
  205. {
  206. Tree.InsertItem(g_userarray.ElementAt (i).ElementAt (1),parent);
  207. }
  208. }
  209. }