ComboListCtrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // ComboBoxListCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ComboListCtrl.h"
  5. #include <windowsx.h>
  6. #include "ylgl.h"
  7. #include "ChooseReason.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #define IDC_ComboBox 0X01
  14. #define PROPERTY_ITEM 0x02
  15. #define PROPERTY_SUB 0x03
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CComboBoxListCtrl
  18. CComboBoxListCtrl::CComboBoxListCtrl()
  19. {
  20. m_ComboBox.m_hWnd = NULL;
  21. m_bNeedSel=0;
  22. }
  23. CComboBoxListCtrl::~CComboBoxListCtrl()
  24. {
  25. }
  26. BEGIN_MESSAGE_MAP(CComboBoxListCtrl, CListCtrl)
  27. //{{AFX_MSG_MAP(CComboBoxListCtrl)
  28. ON_WM_LBUTTONDBLCLK()
  29. ON_WM_PARENTNOTIFY()
  30. ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdrawList)
  31. //}}AFX_MSG_MAP
  32. ON_MESSAGE(WM_USER_ComboBox_END,OnComboBoxEnd)
  33. END_MESSAGE_MAP()
  34. void CComboBoxListCtrl::OnCustomdrawList ( NMHDR* pNMHDR, LRESULT* pResult )
  35. {
  36. NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
  37. // Take the default processing unless we set this to something else below.
  38. *pResult = 0;
  39. // First thing - check the draw stage. If it's the control's prepaint
  40. // stage, then tell Windows we want messages for every item.
  41. if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
  42. {
  43. *pResult = CDRF_NOTIFYITEMDRAW;
  44. }
  45. else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
  46. {
  47. // This is the prepaint stage for an item. Here's where we set the
  48. // item's text color. Our return value will tell Windows to draw the
  49. // item itself, but it will use the new color we set here.
  50. // We'll cycle the colors through red, green, and light blue.
  51. COLORREF crText;
  52. int pos=pLVCD->nmcd.dwItemSpec;
  53. if(pos%2)
  54. {
  55. pLVCD->clrTextBk = g_gridcol1;
  56. }
  57. else
  58. {
  59. pLVCD->clrTextBk = g_gridcol2;
  60. }
  61. // Tell Windows to paint the control itself.
  62. *pResult = CDRF_DODEFAULT;
  63. }
  64. }
  65. void CComboBoxListCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
  66. {
  67. CRect rcCtrl;
  68. LVHITTESTINFO lvhti;
  69. lvhti.pt = point;
  70. int nItem = CListCtrl::SubItemHitTest(&lvhti);
  71. if(nItem == -1)
  72. return;
  73. int nSubItem = lvhti.iSubItem;
  74. CListCtrl::GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rcCtrl);
  75. rcCtrl.top -=4;
  76. rcCtrl.bottom +=200;
  77. if(nSubItem==4||nSubItem==6||nSubItem==8||nSubItem==10||nSubItem==12||nSubItem==14)
  78. ShowComboBox(TRUE,nItem,nSubItem,rcCtrl);
  79. CListCtrl::OnLButtonDblClk(nFlags, point);
  80. }
  81. void CComboBoxListCtrl::ShowComboBox(BOOL bShow,int nItem,int nIndex,CRect rcCtrl)
  82. {
  83. if(m_ComboBox.m_hWnd == NULL)
  84. {
  85. m_ComboBox.Create(WS_VSCROLL|WS_CHILD|CBS_DROPDOWNLIST,CRect(0,0,0,0),this,IDC_ComboBox);
  86. m_ComboBox.ShowWindow(SW_HIDE);
  87. }
  88. if(bShow == TRUE)
  89. {
  90. if(nIndex==6 && GetItemText(nItem,4).IsEmpty ())
  91. return;
  92. CString strItem = CListCtrl::GetItemText(nItem,nIndex);
  93. m_ComboBox.MoveWindow(rcCtrl);
  94. m_ComboBox.ShowWindow(SW_SHOW);
  95. ::SetFocus(m_ComboBox.GetSafeHwnd());
  96. m_ComboBox.ResetContent ();
  97. if(nIndex==4)
  98. {
  99. m_ComboBox.AddString ("");
  100. for(int i=0; i<g_List1array.GetSize (); i++)
  101. m_ComboBox.AddString ("已发"+g_List1array.ElementAt (i).ElementAt (1));
  102. }
  103. else if(nIndex==6)
  104. {
  105. m_ComboBox.AddString ("返工");
  106. m_ComboBox.AddString (""); m_ComboBox.AddString (strItem);
  107. m_bNeedSel=1;
  108. }
  109. else if(nIndex==8)
  110. {
  111. m_ComboBox.AddString ("OK");
  112. m_ComboBox.AddString ("未完成");
  113. }
  114. #ifdef LKAY_VERSION
  115. else if(nIndex==10)
  116. {
  117. m_ComboBox.AddString ("OK");
  118. m_ComboBox.AddString ("未组");
  119. }
  120. else if(nIndex==12)
  121. {
  122. m_ComboBox.AddString ("OK");
  123. m_ComboBox.AddString ("未转");
  124. }
  125. else if(nIndex==14)
  126. {
  127. m_ComboBox.AddString ("OK");
  128. m_ComboBox.AddString ("未取");
  129. }
  130. #else
  131. else if(nIndex==10)
  132. {
  133. m_ComboBox.AddString ("OK");
  134. m_ComboBox.AddString ("未取");
  135. }
  136. #endif
  137. m_ComboBox.SetCurSel (m_ComboBox.FindString (0, strItem));
  138. m_ComboBox.SetCtrlData(MAKEWPARAM(nIndex,nItem));
  139. }
  140. else
  141. m_ComboBox.ShowWindow(SW_HIDE);
  142. }
  143. #ifdef VC60
  144. void CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  145. {
  146. if(wParam == TRUE)
  147. {
  148. CString strText(_T(""));
  149. int pos=m_ComboBox.GetCurSel ();
  150. if(pos!=-1)
  151. {
  152. m_ComboBox.GetLBText (pos, strText);
  153. DWORD dwData = m_ComboBox.GetCtrlData();
  154. int nItem= dwData>>16;
  155. int nIndex = dwData&0x0000ffff;
  156. CListCtrl::SetItemText(nItem,nIndex,strText);
  157. if(nIndex==4 && strText.IsEmpty ())
  158. SetItemText(nItem,6,"");
  159. if(nIndex==4)
  160. {
  161. if(strText!=m_sparray->ElementAt (nItem).ElementAt (4))
  162. SetItemText(nItem,5,g_date+"/"+g_user.name);
  163. }
  164. else if(nIndex==6)
  165. {
  166. // MessageBox(strText);
  167. if(strText.Find("返工")!=-1 && m_bNeedSel)
  168. {
  169. m_bNeedSel=0;
  170. SetItemText(nItem,7,g_date+"/"+g_user.name);
  171. CChooseReason dlg;
  172. if(dlg.DoModal ()==IDOK)
  173. {
  174. SetItemText( nItem, 6,"返工/("+dlg.sRet+")");
  175. m_ComboBox.SetCurSel (-1);
  176. }
  177. }
  178. }
  179. else if(nIndex==8)
  180. {
  181. if(strText!=m_sparray->ElementAt (nItem).ElementAt (6))
  182. SetItemText(nItem,9,g_date+"/"+g_user.name);
  183. }
  184. #ifdef LKAY_VERSION
  185. else if(nIndex==10)
  186. {
  187. if(strText!=m_sparray->ElementAt (nItem).ElementAt (23))
  188. SetItemText(nItem,11,g_date+"/"+g_user.name);
  189. }
  190. else if(nIndex==12)
  191. {
  192. if(strText!=m_sparray->ElementAt (nItem).ElementAt (24))
  193. SetItemText(nItem,13,g_date+"/"+g_user.name);
  194. }
  195. else if(nIndex==14)
  196. {
  197. if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
  198. SetItemText(nItem,15,g_date+"/"+g_user.name);
  199. }
  200. #else
  201. else if(nIndex==10)
  202. {
  203. if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
  204. SetItemText(nItem,11,g_date+"/"+g_user.name);
  205. }
  206. #endif
  207. }
  208. }
  209. else
  210. {
  211. }
  212. if(lParam == FALSE)
  213. m_ComboBox.ShowWindow(SW_HIDE);
  214. }
  215. #else
  216. LRESULT CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  217. {
  218. if(wParam == TRUE)
  219. {
  220. CString strText(_T(""));
  221. int pos=m_ComboBox.GetCurSel ();
  222. if(pos!=-1)
  223. {
  224. m_ComboBox.GetLBText (pos, strText);
  225. DWORD dwData = m_ComboBox.GetCtrlData();
  226. int nItem= dwData>>16;
  227. int nIndex = dwData&0x0000ffff;
  228. CListCtrl::SetItemText(nItem,nIndex,strText);
  229. if(nIndex==4 && strText.IsEmpty ())
  230. SetItemText(nItem,6,"");
  231. if(nIndex==4)
  232. {
  233. if(strText!=m_sparray->ElementAt (nItem).ElementAt (4))
  234. SetItemText(nItem,5,g_date+"/"+g_user.name);
  235. }
  236. else if(nIndex==6)
  237. {
  238. // MessageBox(strText);
  239. if(strText.Find("返工")!=-1 && m_bNeedSel)
  240. {
  241. m_bNeedSel=0;
  242. SetItemText(nItem,7,g_date+"/"+g_user.name);
  243. CChooseReason dlg;
  244. if(dlg.DoModal ()==IDOK)
  245. {
  246. SetItemText( nItem, 6,"返工/("+dlg.sRet+")");
  247. m_ComboBox.SetCurSel (-1);
  248. }
  249. }
  250. }
  251. else if(nIndex==8)
  252. {
  253. if(strText!=m_sparray->ElementAt (nItem).ElementAt (6))
  254. SetItemText(nItem,9,g_date+"/"+g_user.name);
  255. }
  256. #ifdef LKAY_VERSION
  257. else if(nIndex==10)
  258. {
  259. if(strText!=m_sparray->ElementAt (nItem).ElementAt (23))
  260. SetItemText(nItem,11,g_date+"/"+g_user.name);
  261. }
  262. else if(nIndex==12)
  263. {
  264. if(strText!=m_sparray->ElementAt (nItem).ElementAt (24))
  265. SetItemText(nItem,13,g_date+"/"+g_user.name);
  266. }
  267. else if(nIndex==14)
  268. {
  269. if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
  270. SetItemText(nItem,15,g_date+"/"+g_user.name);
  271. }
  272. #else
  273. else if(nIndex==10)
  274. {
  275. if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
  276. SetItemText(nItem,11,g_date+"/"+g_user.name);
  277. }
  278. #endif
  279. }
  280. }
  281. else
  282. {
  283. }
  284. if(lParam == FALSE)
  285. m_ComboBox.ShowWindow(SW_HIDE);
  286. return 0;
  287. }
  288. #endif
  289. void CComboBoxListCtrl::OnParentNotify(UINT message, LPARAM lParam)
  290. {
  291. CListCtrl::OnParentNotify(message, lParam);
  292. //////////////////////////////////////////////////////////////////////////
  293. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  294. if(pHeaderCtrl == NULL)
  295. return;
  296. CRect rcHeader;
  297. pHeaderCtrl->GetWindowRect(rcHeader);
  298. ScreenToClient(rcHeader);
  299. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  300. CPoint pt;
  301. pt.x = GET_X_LPARAM(lParam);
  302. pt.y = GET_Y_LPARAM(lParam);
  303. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  304. {
  305. if(m_ComboBox.m_hWnd != NULL)
  306. {
  307. DWORD dwStyle = m_ComboBox.GetStyle();
  308. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  309. {
  310. m_ComboBox.ShowWindow(SW_HIDE);
  311. }
  312. }
  313. }
  314. }
  315. BOOL CComboBoxListCtrl::PreTranslateMessage(MSG* pMsg)
  316. {
  317. if(0)//pMsg->message == WM_KEYDOWN)
  318. {
  319. if(pMsg->wParam == VK_TAB && m_ComboBox.m_hWnd!= NULL)
  320. {
  321. DWORD dwStyle = m_ComboBox.GetStyle();
  322. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  323. {
  324. OnComboBoxEnd(TRUE,TRUE);
  325. CRect rcCtrl;
  326. int nItem;
  327. int nSub;
  328. if(FALSE == Key_Ctrl(nItem,nSub))
  329. Key_Shift(nItem,nSub);
  330. CListCtrl::GetSubItemRect(nItem,nSub,LVIR_LABEL,rcCtrl);
  331. CPoint pt(rcCtrl.left+1,rcCtrl.top+1);
  332. OnLButtonDblClk(0,pt);
  333. POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
  334. if (pos == NULL)
  335. {
  336. }
  337. else
  338. {
  339. while (pos)
  340. {
  341. int ntpItem = CListCtrl::GetNextSelectedItem(pos);
  342. CListCtrl::SetItemState(ntpItem,0,LVIS_SELECTED);
  343. }
  344. }
  345. CListCtrl::SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  346. return TRUE;
  347. }
  348. }
  349. }
  350. return CListCtrl::PreTranslateMessage(pMsg);
  351. }
  352. BOOL CComboBoxListCtrl::Key_Shift(int& nItem,int& nSub)
  353. {
  354. int nItemCount = CListCtrl::GetItemCount();
  355. DWORD dwData = m_ComboBox.GetCtrlData();
  356. nItem= dwData>>16;
  357. nSub = dwData&0x0000ffff;
  358. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  359. if(pHeader == NULL)
  360. return FALSE;
  361. short sRet = GetKeyState(VK_SHIFT);
  362. int nSubcCount = pHeader->GetItemCount();
  363. sRet = sRet >>15;
  364. if(sRet == 0)
  365. {
  366. nSub += 1;
  367. if(nSub >= nSubcCount)
  368. {
  369. if(nItem == nItemCount-1)
  370. {
  371. nItem = 0;
  372. nSub = 0;
  373. }
  374. else
  375. {
  376. nSub = 0;
  377. nItem += 1;
  378. }
  379. }
  380. if(nItem >= nItemCount)
  381. nItem = nItemCount-1;
  382. return FALSE;
  383. }
  384. else
  385. {
  386. nSub -= 1;
  387. if(nSub < 0)
  388. {
  389. nSub = nSubcCount -1;
  390. nItem --;
  391. }
  392. if(nItem < 0)
  393. nItem = nItemCount-1;
  394. return TRUE;
  395. }
  396. return FALSE;
  397. }
  398. BOOL CComboBoxListCtrl::Key_Ctrl(int& nItem,int &nSub)
  399. {
  400. short sRet = GetKeyState(VK_CONTROL);
  401. DWORD dwData = m_ComboBox.GetCtrlData();
  402. nItem= dwData>>16;
  403. nSub = dwData&0x0000ffff;
  404. sRet = sRet >>15;
  405. int nItemCount = CListCtrl::GetItemCount();
  406. if(sRet == 0)
  407. {
  408. }
  409. else
  410. {
  411. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  412. return TRUE;
  413. }
  414. return FALSE;
  415. }
  416. //////////////////////////////////////////////////////////////////////////
  417. //////////////////////////////////////////////////////////////////////////
  418. //////////////////////////////////////////////////////////////////////////
  419. CListCtrlComboBox::CListCtrlComboBox()
  420. {
  421. }
  422. CListCtrlComboBox::~CListCtrlComboBox()
  423. {
  424. }
  425. BEGIN_MESSAGE_MAP(CListCtrlComboBox, CComboBox)
  426. //{{AFX_MSG_MAP(CListCtrlComboBox)
  427. ON_WM_KILLFOCUS()
  428. ON_WM_SETFOCUS()
  429. ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
  430. //}}AFX_MSG_MAP
  431. END_MESSAGE_MAP()
  432. /////////////////////////////////////////////////////////////////////////////
  433. // CListCtrlComboBox message handlers
  434. void CListCtrlComboBox::SetCtrlData(DWORD dwData)
  435. {
  436. m_dwData = dwData;
  437. }
  438. DWORD CListCtrlComboBox::GetCtrlData()
  439. {
  440. return m_dwData;
  441. }
  442. void CListCtrlComboBox::OnKillFocus(CWnd* pNewWnd)
  443. {
  444. CComboBox::OnKillFocus(pNewWnd);
  445. CWnd* pParent = this->GetParent();
  446. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  447. }
  448. BOOL CListCtrlComboBox::PreTranslateMessage(MSG* pMsg)
  449. {
  450. if(pMsg->message == WM_KEYDOWN)
  451. {
  452. if(pMsg->wParam == VK_RETURN)
  453. {
  454. CWnd* pParent = this->GetParent();
  455. m_bExchange = TRUE;
  456. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  457. }
  458. else if(pMsg->wParam == VK_ESCAPE)
  459. {
  460. CWnd* pParent = this->GetParent();
  461. m_bExchange = FALSE;
  462. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  463. }
  464. }
  465. return CComboBox::PreTranslateMessage(pMsg);
  466. }
  467. void CListCtrlComboBox::OnSetFocus(CWnd* pOldWnd)
  468. {
  469. CComboBox::OnSetFocus(pOldWnd);
  470. m_bExchange = TRUE;
  471. }
  472. void CComboBoxListCtrl::InitStyle()
  473. {
  474. SetFont (&g_listctrlfont);
  475. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  476. }
  477. void CListCtrlComboBox::OnCloseup()
  478. {
  479. // TODO: Add your control notification handler code here
  480. CWnd* pParent = this->GetParent();
  481. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  482. }
  483. #undef SubclassWindow
  484. void CComboBoxListCtrl::PreSubclassWindow()
  485. {
  486. // the list control must have the report style.
  487. CListCtrl::PreSubclassWindow();
  488. m_ctlHeader.SubclassWindow ( GetHeaderCtrl()->GetSafeHwnd());
  489. }