ComboListCtrl5.cpp 12 KB

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