ComboListCtrl5.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. 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. void CComboBoxListCtrl5::OnParentNotify(UINT message, LPARAM lParam)
  301. {
  302. CListCtrl::OnParentNotify(message, lParam);
  303. //////////////////////////////////////////////////////////////////////////
  304. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  305. if(pHeaderCtrl == NULL)
  306. return;
  307. CRect rcHeader;
  308. pHeaderCtrl->GetWindowRect(rcHeader);
  309. ScreenToClient(rcHeader);
  310. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  311. CPoint pt;
  312. pt.x = GET_X_LPARAM(lParam);
  313. pt.y = GET_Y_LPARAM(lParam);
  314. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  315. {
  316. if(m_pComboBox->m_hWnd != NULL)
  317. {
  318. DWORD dwStyle = m_pComboBox->GetStyle();
  319. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  320. {
  321. m_pComboBox->ShowWindow(SW_HIDE);
  322. }
  323. }
  324. if(m_DateCtrl.m_hWnd)
  325. {
  326. if(m_DateCtrl.IsWindowVisible ())m_DateCtrl.ShowWindow(SW_HIDE);
  327. }
  328. }
  329. }
  330. BOOL CComboBoxListCtrl5::PreTranslateMessage(MSG* pMsg)
  331. {
  332. /* if(pMsg->message == WM_LBUTTONUP)
  333. {
  334. if(m_DateCtrl.m_hWnd)
  335. {
  336. if(m_DateCtrl.IsWindowVisible ())
  337. {
  338. ::PostMessage(this->GetSafeHwnd(),WM_USER_ComboBox_END,1,0);
  339. m_DateCtrl.ShowWindow(SW_HIDE);
  340. }
  341. }
  342. }*/
  343. return CListCtrl::PreTranslateMessage(pMsg);
  344. }
  345. BOOL CComboBoxListCtrl5::Key_Shift(int& nItem,int& nSub)
  346. {
  347. int nItemCount = CListCtrl::GetItemCount();
  348. DWORD dwData = m_pComboBox->GetCtrlData();
  349. nItem= dwData>>16;
  350. nSub = dwData&0x0000ffff;
  351. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  352. if(pHeader == NULL)
  353. return FALSE;
  354. short sRet = GetKeyState(VK_SHIFT);
  355. int nSubcCount = pHeader->GetItemCount();
  356. sRet = sRet >>15;
  357. if(sRet == 0)
  358. {
  359. nSub += 1;
  360. if(nSub >= nSubcCount)
  361. {
  362. if(nItem == nItemCount-1)
  363. {
  364. nItem = 0;
  365. nSub = 0;
  366. }
  367. else
  368. {
  369. nSub = 0;
  370. nItem += 1;
  371. }
  372. }
  373. if(nItem >= nItemCount)
  374. nItem = nItemCount-1;
  375. return FALSE;
  376. }
  377. else
  378. {
  379. nSub -= 1;
  380. if(nSub < 0)
  381. {
  382. nSub = nSubcCount -1;
  383. nItem --;
  384. }
  385. if(nItem < 0)
  386. nItem = nItemCount-1;
  387. return TRUE;
  388. }
  389. return FALSE;
  390. }
  391. BOOL CComboBoxListCtrl5::Key_Ctrl(int& nItem,int &nSub)
  392. {
  393. short sRet = GetKeyState(VK_CONTROL);
  394. DWORD dwData = m_pComboBox->GetCtrlData();
  395. nItem= dwData>>16;
  396. nSub = dwData&0x0000ffff;
  397. sRet = sRet >>15;
  398. int nItemCount = CListCtrl::GetItemCount();
  399. if(sRet == 0)
  400. {
  401. }
  402. else
  403. {
  404. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  405. return TRUE;
  406. }
  407. return FALSE;
  408. }
  409. void CComboBoxListCtrl5::InitStyle()
  410. {
  411. SetFont (&g_listctrlfont);
  412. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  413. }
  414. #undef SubclassWindow
  415. void CComboBoxListCtrl5::PreSubclassWindow()
  416. {
  417. // the list control must have the report style.
  418. CListCtrl::PreSubclassWindow();
  419. m_ctlHeader.SubclassWindow ( GetHeaderCtrl()->GetSafeHwnd());
  420. }