ComboListCtrl5.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. void CComboBoxListCtrl5::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  259. {
  260. if(m_DateCtrl.m_hWnd)
  261. {
  262. if(m_DateCtrl.IsWindowVisible ())
  263. {
  264. if(wParam == TRUE)
  265. {
  266. CString strText(_T(""));
  267. CTime tm;
  268. m_DateCtrl.GetTime(tm);
  269. strText=tm.Format ("%Y-%m-%d");
  270. CListCtrl::SetItemText(m_nItem,m_nSub,strText);
  271. }
  272. if(lParam == FALSE)
  273. m_DateCtrl.ShowWindow(SW_HIDE);
  274. return;
  275. }
  276. }
  277. if(m_pComboBox->m_hWnd)
  278. {if(!m_pComboBox->IsWindowVisible ())return;
  279. if(wParam == TRUE)
  280. {
  281. CString strText(_T(""));
  282. int pos=m_pComboBox->GetCurSel ();
  283. if(pos!=-1)
  284. {
  285. m_pComboBox->GetLBText (pos, strText);
  286. DWORD dwData = m_pComboBox->GetCtrlData();
  287. int nItem= dwData>>16;
  288. int nIndex = dwData&0x0000ffff;
  289. CListCtrl::SetItemText(nItem,nIndex,strText);
  290. }
  291. }
  292. else
  293. {
  294. }
  295. if(lParam == FALSE)
  296. m_pComboBox->ShowWindow(SW_HIDE);
  297. }
  298. }
  299. void CComboBoxListCtrl5::OnParentNotify(UINT message, LPARAM lParam)
  300. {
  301. CListCtrl::OnParentNotify(message, lParam);
  302. //////////////////////////////////////////////////////////////////////////
  303. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  304. if(pHeaderCtrl == NULL)
  305. return;
  306. CRect rcHeader;
  307. pHeaderCtrl->GetWindowRect(rcHeader);
  308. ScreenToClient(rcHeader);
  309. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  310. CPoint pt;
  311. pt.x = GET_X_LPARAM(lParam);
  312. pt.y = GET_Y_LPARAM(lParam);
  313. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  314. {
  315. if(m_pComboBox->m_hWnd != NULL)
  316. {
  317. DWORD dwStyle = m_pComboBox->GetStyle();
  318. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  319. {
  320. m_pComboBox->ShowWindow(SW_HIDE);
  321. }
  322. }
  323. if(m_DateCtrl.m_hWnd)
  324. {
  325. if(m_DateCtrl.IsWindowVisible ())m_DateCtrl.ShowWindow(SW_HIDE);
  326. }
  327. }
  328. }
  329. BOOL CComboBoxListCtrl5::PreTranslateMessage(MSG* pMsg)
  330. {
  331. /* if(pMsg->message == WM_LBUTTONUP)
  332. {
  333. if(m_DateCtrl.m_hWnd)
  334. {
  335. if(m_DateCtrl.IsWindowVisible ())
  336. {
  337. ::PostMessage(this->GetSafeHwnd(),WM_USER_ComboBox_END,1,0);
  338. m_DateCtrl.ShowWindow(SW_HIDE);
  339. }
  340. }
  341. }*/
  342. return CListCtrl::PreTranslateMessage(pMsg);
  343. }
  344. BOOL CComboBoxListCtrl5::Key_Shift(int& nItem,int& nSub)
  345. {
  346. int nItemCount = CListCtrl::GetItemCount();
  347. DWORD dwData = m_pComboBox->GetCtrlData();
  348. nItem= dwData>>16;
  349. nSub = dwData&0x0000ffff;
  350. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  351. if(pHeader == NULL)
  352. return FALSE;
  353. short sRet = GetKeyState(VK_SHIFT);
  354. int nSubcCount = pHeader->GetItemCount();
  355. sRet = sRet >>15;
  356. if(sRet == 0)
  357. {
  358. nSub += 1;
  359. if(nSub >= nSubcCount)
  360. {
  361. if(nItem == nItemCount-1)
  362. {
  363. nItem = 0;
  364. nSub = 0;
  365. }
  366. else
  367. {
  368. nSub = 0;
  369. nItem += 1;
  370. }
  371. }
  372. if(nItem >= nItemCount)
  373. nItem = nItemCount-1;
  374. return FALSE;
  375. }
  376. else
  377. {
  378. nSub -= 1;
  379. if(nSub < 0)
  380. {
  381. nSub = nSubcCount -1;
  382. nItem --;
  383. }
  384. if(nItem < 0)
  385. nItem = nItemCount-1;
  386. return TRUE;
  387. }
  388. return FALSE;
  389. }
  390. BOOL CComboBoxListCtrl5::Key_Ctrl(int& nItem,int &nSub)
  391. {
  392. short sRet = GetKeyState(VK_CONTROL);
  393. DWORD dwData = m_pComboBox->GetCtrlData();
  394. nItem= dwData>>16;
  395. nSub = dwData&0x0000ffff;
  396. sRet = sRet >>15;
  397. int nItemCount = CListCtrl::GetItemCount();
  398. if(sRet == 0)
  399. {
  400. }
  401. else
  402. {
  403. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  404. return TRUE;
  405. }
  406. return FALSE;
  407. }
  408. void CComboBoxListCtrl5::InitStyle()
  409. {
  410. SetFont (&g_listctrlfont);
  411. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  412. }
  413. #undef SubclassWindow
  414. void CComboBoxListCtrl5::PreSubclassWindow()
  415. {
  416. // the list control must have the report style.
  417. CListCtrl::PreSubclassWindow();
  418. m_ctlHeader.SubclassWindow ( GetHeaderCtrl()->GetSafeHwnd());
  419. }