ComboListCtrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. void CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  144. {
  145. if(wParam == TRUE)
  146. {
  147. CString strText(_T(""));
  148. int pos=m_ComboBox.GetCurSel ();
  149. if(pos!=-1)
  150. {
  151. m_ComboBox.GetLBText (pos, strText);
  152. DWORD dwData = m_ComboBox.GetCtrlData();
  153. int nItem= dwData>>16;
  154. int nIndex = dwData&0x0000ffff;
  155. CListCtrl::SetItemText(nItem,nIndex,strText);
  156. if(nIndex==4 && strText.IsEmpty ())
  157. SetItemText(nItem,6,"");
  158. if(nIndex==4)
  159. {
  160. if(strText!=m_sparray->ElementAt (nItem).ElementAt (4))
  161. SetItemText(nItem,5,g_date+"/"+g_user.name);
  162. }
  163. else if(nIndex==6)
  164. {
  165. // MessageBox(strText);
  166. if(strText.Find("返工")!=-1 && m_bNeedSel)
  167. {
  168. m_bNeedSel=0;
  169. SetItemText(nItem,7,g_date+"/"+g_user.name);
  170. CChooseReason dlg;
  171. if(dlg.DoModal ()==IDOK)
  172. {
  173. SetItemText( nItem, 6,"返工/("+dlg.sRet+")");
  174. m_ComboBox.SetCurSel (-1);
  175. }
  176. }
  177. }
  178. else if(nIndex==8)
  179. {
  180. if(strText!=m_sparray->ElementAt (nItem).ElementAt (6))
  181. SetItemText(nItem,9,g_date+"/"+g_user.name);
  182. }
  183. #ifdef LKAY_VERSION
  184. else if(nIndex==10)
  185. {
  186. if(strText!=m_sparray->ElementAt (nItem).ElementAt (23))
  187. SetItemText(nItem,11,g_date+"/"+g_user.name);
  188. }
  189. else if(nIndex==12)
  190. {
  191. if(strText!=m_sparray->ElementAt (nItem).ElementAt (24))
  192. SetItemText(nItem,13,g_date+"/"+g_user.name);
  193. }
  194. else if(nIndex==14)
  195. {
  196. if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
  197. SetItemText(nItem,15,g_date+"/"+g_user.name);
  198. }
  199. #else
  200. else if(nIndex==10)
  201. {
  202. if(strText!=m_sparray->ElementAt (nItem).ElementAt (7))
  203. SetItemText(nItem,11,g_date+"/"+g_user.name);
  204. }
  205. #endif
  206. }
  207. }
  208. else
  209. {
  210. }
  211. if(lParam == FALSE)
  212. m_ComboBox.ShowWindow(SW_HIDE);
  213. }
  214. void CComboBoxListCtrl::OnParentNotify(UINT message, LPARAM lParam)
  215. {
  216. CListCtrl::OnParentNotify(message, lParam);
  217. //////////////////////////////////////////////////////////////////////////
  218. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  219. if(pHeaderCtrl == NULL)
  220. return;
  221. CRect rcHeader;
  222. pHeaderCtrl->GetWindowRect(rcHeader);
  223. ScreenToClient(rcHeader);
  224. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  225. CPoint pt;
  226. pt.x = GET_X_LPARAM(lParam);
  227. pt.y = GET_Y_LPARAM(lParam);
  228. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  229. {
  230. if(m_ComboBox.m_hWnd != NULL)
  231. {
  232. DWORD dwStyle = m_ComboBox.GetStyle();
  233. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  234. {
  235. m_ComboBox.ShowWindow(SW_HIDE);
  236. }
  237. }
  238. }
  239. }
  240. BOOL CComboBoxListCtrl::PreTranslateMessage(MSG* pMsg)
  241. {
  242. if(0)//pMsg->message == WM_KEYDOWN)
  243. {
  244. if(pMsg->wParam == VK_TAB && m_ComboBox.m_hWnd!= NULL)
  245. {
  246. DWORD dwStyle = m_ComboBox.GetStyle();
  247. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  248. {
  249. OnComboBoxEnd(TRUE,TRUE);
  250. CRect rcCtrl;
  251. int nItem;
  252. int nSub;
  253. if(FALSE == Key_Ctrl(nItem,nSub))
  254. Key_Shift(nItem,nSub);
  255. CListCtrl::GetSubItemRect(nItem,nSub,LVIR_LABEL,rcCtrl);
  256. CPoint pt(rcCtrl.left+1,rcCtrl.top+1);
  257. OnLButtonDblClk(0,pt);
  258. POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
  259. if (pos == NULL)
  260. {
  261. }
  262. else
  263. {
  264. while (pos)
  265. {
  266. int ntpItem = CListCtrl::GetNextSelectedItem(pos);
  267. CListCtrl::SetItemState(ntpItem,0,LVIS_SELECTED);
  268. }
  269. }
  270. CListCtrl::SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  271. return TRUE;
  272. }
  273. }
  274. }
  275. return CListCtrl::PreTranslateMessage(pMsg);
  276. }
  277. BOOL CComboBoxListCtrl::Key_Shift(int& nItem,int& nSub)
  278. {
  279. int nItemCount = CListCtrl::GetItemCount();
  280. DWORD dwData = m_ComboBox.GetCtrlData();
  281. nItem= dwData>>16;
  282. nSub = dwData&0x0000ffff;
  283. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  284. if(pHeader == NULL)
  285. return FALSE;
  286. short sRet = GetKeyState(VK_SHIFT);
  287. int nSubcCount = pHeader->GetItemCount();
  288. sRet = sRet >>15;
  289. if(sRet == 0)
  290. {
  291. nSub += 1;
  292. if(nSub >= nSubcCount)
  293. {
  294. if(nItem == nItemCount-1)
  295. {
  296. nItem = 0;
  297. nSub = 0;
  298. }
  299. else
  300. {
  301. nSub = 0;
  302. nItem += 1;
  303. }
  304. }
  305. if(nItem >= nItemCount)
  306. nItem = nItemCount-1;
  307. return FALSE;
  308. }
  309. else
  310. {
  311. nSub -= 1;
  312. if(nSub < 0)
  313. {
  314. nSub = nSubcCount -1;
  315. nItem --;
  316. }
  317. if(nItem < 0)
  318. nItem = nItemCount-1;
  319. return TRUE;
  320. }
  321. return FALSE;
  322. }
  323. BOOL CComboBoxListCtrl::Key_Ctrl(int& nItem,int &nSub)
  324. {
  325. short sRet = GetKeyState(VK_CONTROL);
  326. DWORD dwData = m_ComboBox.GetCtrlData();
  327. nItem= dwData>>16;
  328. nSub = dwData&0x0000ffff;
  329. sRet = sRet >>15;
  330. int nItemCount = CListCtrl::GetItemCount();
  331. if(sRet == 0)
  332. {
  333. }
  334. else
  335. {
  336. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  337. return TRUE;
  338. }
  339. return FALSE;
  340. }
  341. //////////////////////////////////////////////////////////////////////////
  342. //////////////////////////////////////////////////////////////////////////
  343. //////////////////////////////////////////////////////////////////////////
  344. CListCtrlComboBox::CListCtrlComboBox()
  345. {
  346. }
  347. CListCtrlComboBox::~CListCtrlComboBox()
  348. {
  349. }
  350. BEGIN_MESSAGE_MAP(CListCtrlComboBox, CComboBox)
  351. //{{AFX_MSG_MAP(CListCtrlComboBox)
  352. ON_WM_KILLFOCUS()
  353. ON_WM_SETFOCUS()
  354. ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
  355. //}}AFX_MSG_MAP
  356. END_MESSAGE_MAP()
  357. /////////////////////////////////////////////////////////////////////////////
  358. // CListCtrlComboBox message handlers
  359. void CListCtrlComboBox::SetCtrlData(DWORD dwData)
  360. {
  361. m_dwData = dwData;
  362. }
  363. DWORD CListCtrlComboBox::GetCtrlData()
  364. {
  365. return m_dwData;
  366. }
  367. void CListCtrlComboBox::OnKillFocus(CWnd* pNewWnd)
  368. {
  369. CComboBox::OnKillFocus(pNewWnd);
  370. CWnd* pParent = this->GetParent();
  371. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  372. }
  373. BOOL CListCtrlComboBox::PreTranslateMessage(MSG* pMsg)
  374. {
  375. if(pMsg->message == WM_KEYDOWN)
  376. {
  377. if(pMsg->wParam == VK_RETURN)
  378. {
  379. CWnd* pParent = this->GetParent();
  380. m_bExchange = TRUE;
  381. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  382. }
  383. else if(pMsg->wParam == VK_ESCAPE)
  384. {
  385. CWnd* pParent = this->GetParent();
  386. m_bExchange = FALSE;
  387. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  388. }
  389. }
  390. return CComboBox::PreTranslateMessage(pMsg);
  391. }
  392. void CListCtrlComboBox::OnSetFocus(CWnd* pOldWnd)
  393. {
  394. CComboBox::OnSetFocus(pOldWnd);
  395. m_bExchange = TRUE;
  396. }
  397. void CComboBoxListCtrl::InitStyle()
  398. {
  399. SetFont (&g_listctrlfont);
  400. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  401. }
  402. void CListCtrlComboBox::OnCloseup()
  403. {
  404. // TODO: Add your control notification handler code here
  405. CWnd* pParent = this->GetParent();
  406. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  407. }
  408. #undef SubclassWindow
  409. void CComboBoxListCtrl::PreSubclassWindow()
  410. {
  411. // the list control must have the report style.
  412. CListCtrl::PreSubclassWindow();
  413. m_ctlHeader.SubclassWindow ( GetHeaderCtrl()->GetSafeHwnd());
  414. }