ComboListCtrl5.cpp 12 KB

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