TreeComboBox2.cpp 5.1 KB

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