ComboListCtrl.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // ComboBoxListCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ComboListCtrl.h"
  5. #include <windowsx.h>
  6. #include "LYFZIPManage.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. #define IDC_ComboBox 0X01
  13. #define PROPERTY_ITEM 0x02
  14. #define PROPERTY_SUB 0x03
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CComboBoxListCtrl
  17. extern CFont g_listctrlfont;
  18. CComboBoxListCtrl::CComboBoxListCtrl()
  19. {
  20. m_ComboBox.m_hWnd = NULL;
  21. }
  22. CComboBoxListCtrl::~CComboBoxListCtrl()
  23. {
  24. }
  25. BEGIN_MESSAGE_MAP(CComboBoxListCtrl, CListCtrl)
  26. //{{AFX_MSG_MAP(CComboBoxListCtrl)
  27. ON_WM_LBUTTONDBLCLK()
  28. ON_WM_PARENTNOTIFY()
  29. //}}AFX_MSG_MAP
  30. ON_MESSAGE(WM_USER_ComboBox_END,OnComboBoxEnd)
  31. END_MESSAGE_MAP()
  32. void CComboBoxListCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
  33. {
  34. CRect rcCtrl;
  35. LVHITTESTINFO lvhti;
  36. lvhti.pt = point;
  37. int nItem = CListCtrl::SubItemHitTest(&lvhti);
  38. if(nItem == -1)
  39. return;
  40. int nSubItem = lvhti.iSubItem;
  41. CListCtrl::GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rcCtrl);
  42. rcCtrl.top -=4;
  43. rcCtrl.bottom +=200;
  44. // if(nSubItem>1)
  45. if(nSubItem==2||nSubItem==4||nSubItem==6||nSubItem==8)
  46. ShowComboBox(TRUE,nItem,nSubItem,rcCtrl);
  47. CListCtrl::OnLButtonDblClk(nFlags, point);
  48. }
  49. void CComboBoxListCtrl::ShowComboBox(BOOL bShow,int nItem,int nIndex,CRect rcCtrl)
  50. {
  51. if(m_ComboBox.m_hWnd == NULL)
  52. {
  53. m_ComboBox.Create(WS_VSCROLL|WS_CHILD|CBS_DROPDOWNLIST,CRect(0,0,0,0),this,IDC_ComboBox);
  54. m_ComboBox.ShowWindow(SW_HIDE);
  55. }
  56. if(bShow == TRUE)
  57. {
  58. if(nIndex==4 && GetItemText(nItem,2).IsEmpty ())
  59. return;
  60. CString strItem = CListCtrl::GetItemText(nItem,nIndex);
  61. m_ComboBox.MoveWindow(rcCtrl);
  62. m_ComboBox.ShowWindow(SW_SHOW);
  63. ::SetFocus(m_ComboBox.GetSafeHwnd());
  64. m_ComboBox.ResetContent ();
  65. if(nIndex==2)
  66. {
  67. m_ComboBox.AddString ("");
  68. for(int i=0; i<g_List1array.GetSize (); i++)
  69. m_ComboBox.AddString ("ÒÑ·¢"+g_List1array.ElementAt (i).ElementAt (1));
  70. }
  71. else if(nIndex==4)
  72. {
  73. m_ComboBox.AddString ("OK");
  74. m_ComboBox.AddString ("");
  75. }
  76. else if(nIndex==6)
  77. {
  78. m_ComboBox.AddString ("OK");
  79. m_ComboBox.AddString ("δÍê³É");
  80. }
  81. else if(nIndex==8)
  82. {
  83. m_ComboBox.AddString ("OK");
  84. m_ComboBox.AddString ("δȡ");
  85. }
  86. m_ComboBox.SetCurSel (m_ComboBox.FindString (0, strItem));
  87. m_ComboBox.SetCtrlData(MAKEWPARAM(nIndex,nItem));
  88. }
  89. else
  90. m_ComboBox.ShowWindow(SW_HIDE);
  91. }
  92. void CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  93. {
  94. if(wParam == TRUE)
  95. {
  96. CString strText(_T(""));
  97. int pos=m_ComboBox.GetCurSel ();
  98. m_ComboBox.GetLBText (pos, strText);
  99. DWORD dwData = m_ComboBox.GetCtrlData();
  100. int nItem= dwData>>16;
  101. int nIndex = dwData&0x0000ffff;
  102. CListCtrl::SetItemText(nItem,nIndex,strText);
  103. if(nIndex==2 && strText.IsEmpty ())
  104. SetItemText(nItem,4,"");
  105. if(nIndex==2)
  106. {
  107. if(strText!=m_sparray->ElementAt (nItem).ElementAt (2))
  108. SetItemText(nItem,3,g_date+"/"+g_user.name);
  109. }
  110. else if(nIndex==4)
  111. {
  112. if(strText!=m_sparray->ElementAt (nItem).ElementAt (3))
  113. SetItemText(nItem,5,g_date+"/"+g_user.name);
  114. }
  115. else if(nIndex==6)
  116. {
  117. if(strText!=m_sparray->ElementAt (nItem).ElementAt (4))
  118. SetItemText(nItem,7,g_date+"/"+g_user.name);
  119. }
  120. else if(nIndex==8)
  121. {
  122. if(strText!=m_sparray->ElementAt (nItem).ElementAt (5))
  123. SetItemText(nItem,9,g_date+"/"+g_user.name);
  124. }
  125. }
  126. else
  127. {
  128. }
  129. if(lParam == FALSE)
  130. m_ComboBox.ShowWindow(SW_HIDE);
  131. }
  132. void CComboBoxListCtrl::OnParentNotify(UINT message, LPARAM lParam)
  133. {
  134. CListCtrl::OnParentNotify(message, lParam);
  135. //////////////////////////////////////////////////////////////////////////
  136. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  137. if(pHeaderCtrl == NULL)
  138. return;
  139. CRect rcHeader;
  140. pHeaderCtrl->GetWindowRect(rcHeader);
  141. ScreenToClient(rcHeader);
  142. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  143. CPoint pt;
  144. pt.x = GET_X_LPARAM(lParam);
  145. pt.y = GET_Y_LPARAM(lParam);
  146. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  147. {
  148. if(m_ComboBox.m_hWnd != NULL)
  149. {
  150. DWORD dwStyle = m_ComboBox.GetStyle();
  151. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  152. {
  153. m_ComboBox.ShowWindow(SW_HIDE);
  154. }
  155. }
  156. }
  157. }
  158. BOOL CComboBoxListCtrl::PreTranslateMessage(MSG* pMsg)
  159. {
  160. if(0)//pMsg->message == WM_KEYDOWN)
  161. {
  162. if(pMsg->wParam == VK_TAB && m_ComboBox.m_hWnd!= NULL)
  163. {
  164. DWORD dwStyle = m_ComboBox.GetStyle();
  165. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  166. {
  167. OnComboBoxEnd(TRUE,TRUE);
  168. CRect rcCtrl;
  169. int nItem;
  170. int nSub;
  171. if(FALSE == Key_Ctrl(nItem,nSub))
  172. Key_Shift(nItem,nSub);
  173. CListCtrl::GetSubItemRect(nItem,nSub,LVIR_LABEL,rcCtrl);
  174. CPoint pt(rcCtrl.left+1,rcCtrl.top+1);
  175. OnLButtonDblClk(0,pt);
  176. POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
  177. if (pos == NULL)
  178. {
  179. }
  180. else
  181. {
  182. while (pos)
  183. {
  184. int ntpItem = CListCtrl::GetNextSelectedItem(pos);
  185. CListCtrl::SetItemState(ntpItem,0,LVIS_SELECTED);
  186. }
  187. }
  188. CListCtrl::SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  189. return TRUE;
  190. }
  191. }
  192. }
  193. return CListCtrl::PreTranslateMessage(pMsg);
  194. }
  195. BOOL CComboBoxListCtrl::Key_Shift(int& nItem,int& nSub)
  196. {
  197. int nItemCount = CListCtrl::GetItemCount();
  198. DWORD dwData = m_ComboBox.GetCtrlData();
  199. nItem= dwData>>16;
  200. nSub = dwData&0x0000ffff;
  201. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  202. if(pHeader == NULL)
  203. return FALSE;
  204. short sRet = GetKeyState(VK_SHIFT);
  205. int nSubcCount = pHeader->GetItemCount();
  206. sRet = sRet >>15;
  207. if(sRet == 0)
  208. {
  209. nSub += 1;
  210. if(nSub >= nSubcCount)
  211. {
  212. if(nItem == nItemCount-1)
  213. {
  214. nItem = 0;
  215. nSub = 0;
  216. }
  217. else
  218. {
  219. nSub = 0;
  220. nItem += 1;
  221. }
  222. }
  223. if(nItem >= nItemCount)
  224. nItem = nItemCount-1;
  225. return FALSE;
  226. }
  227. else
  228. {
  229. nSub -= 1;
  230. if(nSub < 0)
  231. {
  232. nSub = nSubcCount -1;
  233. nItem --;
  234. }
  235. if(nItem < 0)
  236. nItem = nItemCount-1;
  237. return TRUE;
  238. }
  239. return FALSE;
  240. }
  241. BOOL CComboBoxListCtrl::Key_Ctrl(int& nItem,int &nSub)
  242. {
  243. short sRet = GetKeyState(VK_CONTROL);
  244. DWORD dwData = m_ComboBox.GetCtrlData();
  245. nItem= dwData>>16;
  246. nSub = dwData&0x0000ffff;
  247. sRet = sRet >>15;
  248. int nItemCount = CListCtrl::GetItemCount();
  249. if(sRet == 0)
  250. {
  251. }
  252. else
  253. {
  254. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  255. return TRUE;
  256. }
  257. return FALSE;
  258. }
  259. //////////////////////////////////////////////////////////////////////////
  260. //////////////////////////////////////////////////////////////////////////
  261. //////////////////////////////////////////////////////////////////////////
  262. CListCtrlComboBox::CListCtrlComboBox()
  263. {
  264. }
  265. CListCtrlComboBox::~CListCtrlComboBox()
  266. {
  267. }
  268. BEGIN_MESSAGE_MAP(CListCtrlComboBox, CComboBox)
  269. //{{AFX_MSG_MAP(CListCtrlComboBox)
  270. ON_WM_KILLFOCUS()
  271. ON_WM_SETFOCUS()
  272. ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
  273. //}}AFX_MSG_MAP
  274. END_MESSAGE_MAP()
  275. /////////////////////////////////////////////////////////////////////////////
  276. // CListCtrlComboBox message handlers
  277. void CListCtrlComboBox::SetCtrlData(DWORD dwData)
  278. {
  279. m_dwData = dwData;
  280. }
  281. DWORD CListCtrlComboBox::GetCtrlData()
  282. {
  283. return m_dwData;
  284. }
  285. void CListCtrlComboBox::OnKillFocus(CWnd* pNewWnd)
  286. {
  287. CComboBox::OnKillFocus(pNewWnd);
  288. CWnd* pParent = this->GetParent();
  289. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  290. }
  291. BOOL CListCtrlComboBox::PreTranslateMessage(MSG* pMsg)
  292. {
  293. if(pMsg->message == WM_KEYDOWN)
  294. {
  295. if(pMsg->wParam == VK_RETURN)
  296. {
  297. CWnd* pParent = this->GetParent();
  298. m_bExchange = TRUE;
  299. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  300. }
  301. else if(pMsg->wParam == VK_ESCAPE)
  302. {
  303. CWnd* pParent = this->GetParent();
  304. m_bExchange = FALSE;
  305. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  306. }
  307. }
  308. return CComboBox::PreTranslateMessage(pMsg);
  309. }
  310. void CListCtrlComboBox::OnSetFocus(CWnd* pOldWnd)
  311. {
  312. CComboBox::OnSetFocus(pOldWnd);
  313. m_bExchange = TRUE;
  314. }
  315. void CComboBoxListCtrl::InitStyle()
  316. {
  317. SetFont (&g_listctrlfont);
  318. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  319. }
  320. void CListCtrlComboBox::OnCloseup()
  321. {
  322. // TODO: Add your control notification handler code here
  323. CWnd* pParent = this->GetParent();
  324. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  325. }