ComboListCtrl2.cpp 11 KB

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