ComboListCtrl.cpp 12 KB

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