ComboListCtrl3.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // ComboBoxLISTCTRL3.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ComboListCtrl3.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. // CComboBoxListCtrl3
  17. extern CFont g_listctrlfont;
  18. CComboBoxListCtrl3::CComboBoxListCtrl3()
  19. {
  20. m_ComboBox.m_hWnd = NULL;
  21. }
  22. CComboBoxListCtrl3::~CComboBoxListCtrl3()
  23. {
  24. }
  25. BEGIN_MESSAGE_MAP(CComboBoxListCtrl3, CListCtrl)
  26. //{{AFX_MSG_MAP(CComboBoxListCtrl3)
  27. ON_WM_LBUTTONDBLCLK()
  28. ON_WM_PARENTNOTIFY()
  29. ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdrawList)
  30. //}}AFX_MSG_MAP
  31. ON_MESSAGE(WM_USER_ComboBox_END,OnComboBoxEnd)
  32. END_MESSAGE_MAP()
  33. #undef SubclassWindow
  34. void CComboBoxListCtrl3::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 CComboBoxListCtrl3::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 CComboBoxListCtrl3::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. rcCtrl.top -=4;
  83. rcCtrl.bottom +=200;
  84. if(nSubItem==3)
  85. ShowComboBox(TRUE,nItem,nSubItem,rcCtrl);
  86. CListCtrl::OnLButtonDblClk(nFlags, point);
  87. }
  88. void CComboBoxListCtrl3::ShowComboBox(BOOL bShow,int nItem,int nIndex,CRect rcCtrl)
  89. {
  90. if(m_ComboBox.m_hWnd == NULL)
  91. {
  92. m_ComboBox.Create(WS_VSCROLL|WS_CHILD|CBS_DROPDOWNLIST,CRect(0,0,0,0),this,IDC_ComboBox);
  93. m_ComboBox.ShowWindow(SW_HIDE);
  94. }
  95. if(bShow == TRUE)
  96. {
  97. CString strItem = CListCtrl::GetItemText(nItem,nIndex);
  98. m_ComboBox.MoveWindow(rcCtrl);
  99. m_ComboBox.ShowWindow(SW_SHOW);
  100. ::SetFocus(m_ComboBox.GetSafeHwnd());
  101. m_ComboBox.ResetContent ();
  102. m_ComboBox.AddString ("δ»¹");
  103. m_ComboBox.AddString ("ÒÑ»¹");
  104. m_ComboBox.SetCurSel (m_ComboBox.FindString (0, strItem));
  105. m_ComboBox.SetCtrlData(MAKEWPARAM(nIndex,nItem));
  106. }
  107. else
  108. m_ComboBox.ShowWindow(SW_HIDE);
  109. }
  110. #ifdef VC_60
  111. void CComboBoxListCtrl3::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  112. {
  113. if(wParam == TRUE)
  114. {
  115. CString strText(_T(""));
  116. int pos=m_ComboBox.GetCurSel ();
  117. if(pos!=-1)
  118. {
  119. m_ComboBox.GetLBText (pos, strText);
  120. DWORD dwData = m_ComboBox.GetCtrlData();
  121. int nItem= dwData>>16;
  122. int nIndex = dwData&0x0000ffff;
  123. CListCtrl::SetItemText(nItem,nIndex,strText);
  124. if(nIndex==3)
  125. {
  126. if(strText!=m_sparray->ElementAt (nItem).ElementAt (4))
  127. {
  128. SetItemText(nItem,4,g_user.name);
  129. SetItemText(nItem,5,g_date);
  130. }
  131. }
  132. }
  133. }
  134. else
  135. {
  136. }
  137. if(lParam == FALSE)
  138. m_ComboBox.ShowWindow(SW_HIDE);
  139. }
  140. #else
  141. LRESULT CComboBoxListCtrl3::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  142. {
  143. if(wParam == TRUE)
  144. {
  145. CString strText(_T(""));
  146. int pos=m_ComboBox.GetCurSel ();
  147. if(pos!=-1)
  148. {
  149. m_ComboBox.GetLBText (pos, strText);
  150. DWORD dwData = m_ComboBox.GetCtrlData();
  151. int nItem= dwData>>16;
  152. int nIndex = dwData&0x0000ffff;
  153. CListCtrl::SetItemText(nItem,nIndex,strText);
  154. if(nIndex==3)
  155. {
  156. if(strText!=m_sparray->ElementAt (nItem).ElementAt (4))
  157. {
  158. SetItemText(nItem,4,g_user.name);
  159. SetItemText(nItem,5,g_date);
  160. }
  161. }
  162. }
  163. }
  164. else
  165. {
  166. }
  167. if(lParam == FALSE)
  168. m_ComboBox.ShowWindow(SW_HIDE);
  169. return 0;
  170. }
  171. #endif
  172. void CComboBoxListCtrl3::OnParentNotify(UINT message, LPARAM lParam)
  173. {
  174. CListCtrl::OnParentNotify(message, lParam);
  175. //////////////////////////////////////////////////////////////////////////
  176. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  177. if(pHeaderCtrl == NULL)
  178. return;
  179. CRect rcHeader;
  180. pHeaderCtrl->GetWindowRect(rcHeader);
  181. ScreenToClient(rcHeader);
  182. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  183. CPoint pt;
  184. pt.x = GET_X_LPARAM(lParam);
  185. pt.y = GET_Y_LPARAM(lParam);
  186. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  187. {
  188. if(m_ComboBox.m_hWnd != NULL)
  189. {
  190. DWORD dwStyle = m_ComboBox.GetStyle();
  191. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  192. {
  193. m_ComboBox.ShowWindow(SW_HIDE);
  194. }
  195. }
  196. }
  197. }
  198. BOOL CComboBoxListCtrl3::PreTranslateMessage(MSG* pMsg)
  199. {
  200. if(0)//pMsg->message == WM_KEYDOWN)
  201. {
  202. if(pMsg->wParam == VK_TAB && m_ComboBox.m_hWnd!= NULL)
  203. {
  204. DWORD dwStyle = m_ComboBox.GetStyle();
  205. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  206. {
  207. OnComboBoxEnd(TRUE,TRUE);
  208. CRect rcCtrl;
  209. int nItem;
  210. int nSub;
  211. if(FALSE == Key_Ctrl(nItem,nSub))
  212. Key_Shift(nItem,nSub);
  213. CListCtrl::GetSubItemRect(nItem,nSub,LVIR_LABEL,rcCtrl);
  214. CPoint pt(rcCtrl.left+1,rcCtrl.top+1);
  215. OnLButtonDblClk(0,pt);
  216. POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
  217. if (pos == NULL)
  218. {
  219. }
  220. else
  221. {
  222. while (pos)
  223. {
  224. int ntpItem = CListCtrl::GetNextSelectedItem(pos);
  225. CListCtrl::SetItemState(ntpItem,0,LVIS_SELECTED);
  226. }
  227. }
  228. CListCtrl::SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  229. return TRUE;
  230. }
  231. }
  232. }
  233. return CListCtrl::PreTranslateMessage(pMsg);
  234. }
  235. BOOL CComboBoxListCtrl3::Key_Shift(int& nItem,int& nSub)
  236. {
  237. int nItemCount = CListCtrl::GetItemCount();
  238. DWORD dwData = m_ComboBox.GetCtrlData();
  239. nItem= dwData>>16;
  240. nSub = dwData&0x0000ffff;
  241. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  242. if(pHeader == NULL)
  243. return FALSE;
  244. short sRet = GetKeyState(VK_SHIFT);
  245. int nSubcCount = pHeader->GetItemCount();
  246. sRet = sRet >>15;
  247. if(sRet == 0)
  248. {
  249. nSub += 1;
  250. if(nSub >= nSubcCount)
  251. {
  252. if(nItem == nItemCount-1)
  253. {
  254. nItem = 0;
  255. nSub = 0;
  256. }
  257. else
  258. {
  259. nSub = 0;
  260. nItem += 1;
  261. }
  262. }
  263. if(nItem >= nItemCount)
  264. nItem = nItemCount-1;
  265. return FALSE;
  266. }
  267. else
  268. {
  269. nSub -= 1;
  270. if(nSub < 0)
  271. {
  272. nSub = nSubcCount -1;
  273. nItem --;
  274. }
  275. if(nItem < 0)
  276. nItem = nItemCount-1;
  277. return TRUE;
  278. }
  279. return FALSE;
  280. }
  281. BOOL CComboBoxListCtrl3::Key_Ctrl(int& nItem,int &nSub)
  282. {
  283. short sRet = GetKeyState(VK_CONTROL);
  284. DWORD dwData = m_ComboBox.GetCtrlData();
  285. nItem= dwData>>16;
  286. nSub = dwData&0x0000ffff;
  287. sRet = sRet >>15;
  288. int nItemCount = CListCtrl::GetItemCount();
  289. if(sRet == 0)
  290. {
  291. }
  292. else
  293. {
  294. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  295. return TRUE;
  296. }
  297. return FALSE;
  298. }
  299. //////////////////////////////////////////////////////////////////////////
  300. //////////////////////////////////////////////////////////////////////////
  301. //////////////////////////////////////////////////////////////////////////
  302. CListCtrlComboBox3::CListCtrlComboBox3()
  303. {
  304. }
  305. CListCtrlComboBox3::~CListCtrlComboBox3()
  306. {
  307. }
  308. BEGIN_MESSAGE_MAP(CListCtrlComboBox3, CComboBox)
  309. //{{AFX_MSG_MAP(CListCtrlComboBox3)
  310. ON_WM_KILLFOCUS()
  311. ON_WM_SETFOCUS()
  312. ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
  313. //}}AFX_MSG_MAP
  314. END_MESSAGE_MAP()
  315. /////////////////////////////////////////////////////////////////////////////
  316. // CListCtrlComboBox3 message handlers
  317. void CListCtrlComboBox3::SetCtrlData(DWORD dwData)
  318. {
  319. m_dwData = dwData;
  320. }
  321. DWORD CListCtrlComboBox3::GetCtrlData()
  322. {
  323. return m_dwData;
  324. }
  325. void CListCtrlComboBox3::OnKillFocus(CWnd* pNewWnd)
  326. {
  327. CComboBox::OnKillFocus(pNewWnd);
  328. CWnd* pParent = this->GetParent();
  329. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  330. }
  331. BOOL CListCtrlComboBox3::PreTranslateMessage(MSG* pMsg)
  332. {
  333. if(pMsg->message == WM_KEYDOWN)
  334. {
  335. if(pMsg->wParam == VK_RETURN)
  336. {
  337. CWnd* pParent = this->GetParent();
  338. m_bExchange = TRUE;
  339. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  340. }
  341. else if(pMsg->wParam == VK_ESCAPE)
  342. {
  343. CWnd* pParent = this->GetParent();
  344. m_bExchange = FALSE;
  345. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  346. }
  347. }
  348. return CComboBox::PreTranslateMessage(pMsg);
  349. }
  350. void CListCtrlComboBox3::OnSetFocus(CWnd* pOldWnd)
  351. {
  352. CComboBox::OnSetFocus(pOldWnd);
  353. m_bExchange = TRUE;
  354. }
  355. void CComboBoxListCtrl3::InitStyle()
  356. {
  357. SetFont (&g_listctrlfont);
  358. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  359. }
  360. void CListCtrlComboBox3::OnCloseup()
  361. {
  362. // TODO: Add your control notification handler code here
  363. CWnd* pParent = this->GetParent();
  364. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  365. }