ComboListCtrl3.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. CComboBoxListCtrl3::CComboBoxListCtrl3()
  18. {
  19. m_ComboBox.m_hWnd = NULL;
  20. }
  21. CComboBoxListCtrl3::~CComboBoxListCtrl3()
  22. {
  23. }
  24. BEGIN_MESSAGE_MAP(CComboBoxListCtrl3, CListCtrl)
  25. //{{AFX_MSG_MAP(CComboBoxListCtrl3)
  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. END_MESSAGE_MAP()
  32. #undef SubclassWindow
  33. void CComboBoxListCtrl3::PreSubclassWindow()
  34. {
  35. // the list control must have the report style.
  36. ASSERT( GetStyle() & LVS_REPORT );
  37. CListCtrl::PreSubclassWindow();
  38. VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) );
  39. }
  40. void CComboBoxListCtrl3::OnCustomdrawList ( NMHDR* pNMHDR, LRESULT* pResult )
  41. {
  42. NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
  43. // Take the default processing unless we set this to something else below.
  44. *pResult = 0;
  45. // First thing - check the draw stage. If it's the control's prepaint
  46. // stage, then tell Windows we want messages for every item.
  47. if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
  48. {
  49. *pResult = CDRF_NOTIFYITEMDRAW;
  50. }
  51. else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
  52. {
  53. // This is the prepaint stage for an item. Here's where we set the
  54. // item's text color. Our return value will tell Windows to draw the
  55. // item itself, but it will use the new color we set here.
  56. // We'll cycle the colors through red, green, and light blue.
  57. COLORREF crText;
  58. int pos=pLVCD->nmcd.dwItemSpec;
  59. if(pos%2)
  60. {
  61. pLVCD->clrTextBk = g_gridcol1;
  62. }
  63. else
  64. {
  65. pLVCD->clrTextBk = g_gridcol2;
  66. }
  67. // Tell Windows to paint the control itself.
  68. *pResult = CDRF_DODEFAULT;
  69. }
  70. }
  71. void CComboBoxListCtrl3::OnLButtonDblClk(UINT nFlags, CPoint point)
  72. {
  73. CRect rcCtrl;
  74. LVHITTESTINFO lvhti;
  75. lvhti.pt = point;
  76. int nItem = CListCtrl::SubItemHitTest(&lvhti);
  77. if(nItem == -1)
  78. return;
  79. int nSubItem = lvhti.iSubItem;
  80. CListCtrl::GetSubItemRect(nItem,nSubItem,LVIR_LABEL,rcCtrl);
  81. rcCtrl.top -=4;
  82. rcCtrl.bottom +=200;
  83. if(nSubItem==3)
  84. ShowComboBox(TRUE,nItem,nSubItem,rcCtrl);
  85. CListCtrl::OnLButtonDblClk(nFlags, point);
  86. }
  87. void CComboBoxListCtrl3::ShowComboBox(BOOL bShow,int nItem,int nIndex,CRect rcCtrl)
  88. {
  89. if(m_ComboBox.m_hWnd == NULL)
  90. {
  91. m_ComboBox.Create(WS_VSCROLL|WS_CHILD|CBS_DROPDOWNLIST,CRect(0,0,0,0),this,IDC_ComboBox);
  92. m_ComboBox.ShowWindow(SW_HIDE);
  93. }
  94. if(bShow == TRUE)
  95. {
  96. CString strItem = CListCtrl::GetItemText(nItem,nIndex);
  97. m_ComboBox.MoveWindow(rcCtrl);
  98. m_ComboBox.ShowWindow(SW_SHOW);
  99. ::SetFocus(m_ComboBox.GetSafeHwnd());
  100. m_ComboBox.ResetContent ();
  101. m_ComboBox.AddString ("δ»¹");
  102. m_ComboBox.AddString ("ÒÑ»¹");
  103. m_ComboBox.SetCurSel (m_ComboBox.FindString (0, strItem));
  104. m_ComboBox.SetCtrlData(MAKEWPARAM(nIndex,nItem));
  105. }
  106. else
  107. m_ComboBox.ShowWindow(SW_HIDE);
  108. }
  109. void CComboBoxListCtrl3::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  110. {
  111. if(wParam == TRUE)
  112. {
  113. CString strText(_T(""));
  114. int pos=m_ComboBox.GetCurSel ();
  115. if(pos!=-1)
  116. {
  117. m_ComboBox.GetLBText (pos, strText);
  118. DWORD dwData = m_ComboBox.GetCtrlData();
  119. int nItem= dwData>>16;
  120. int nIndex = dwData&0x0000ffff;
  121. CListCtrl::SetItemText(nItem,nIndex,strText);
  122. if(nIndex==3)
  123. {
  124. if(strText!=m_sparray->ElementAt (nItem).ElementAt (4))
  125. {
  126. SetItemText(nItem,4,g_user.name);
  127. SetItemText(nItem,5,g_date);
  128. }
  129. }
  130. }
  131. }
  132. else
  133. {
  134. }
  135. if(lParam == FALSE)
  136. m_ComboBox.ShowWindow(SW_HIDE);
  137. }
  138. void CComboBoxListCtrl3::OnParentNotify(UINT message, LPARAM lParam)
  139. {
  140. CListCtrl::OnParentNotify(message, lParam);
  141. //////////////////////////////////////////////////////////////////////////
  142. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  143. if(pHeaderCtrl == NULL)
  144. return;
  145. CRect rcHeader;
  146. pHeaderCtrl->GetWindowRect(rcHeader);
  147. ScreenToClient(rcHeader);
  148. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  149. CPoint pt;
  150. pt.x = GET_X_LPARAM(lParam);
  151. pt.y = GET_Y_LPARAM(lParam);
  152. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  153. {
  154. if(m_ComboBox.m_hWnd != NULL)
  155. {
  156. DWORD dwStyle = m_ComboBox.GetStyle();
  157. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  158. {
  159. m_ComboBox.ShowWindow(SW_HIDE);
  160. }
  161. }
  162. }
  163. }
  164. BOOL CComboBoxListCtrl3::PreTranslateMessage(MSG* pMsg)
  165. {
  166. if(0)//pMsg->message == WM_KEYDOWN)
  167. {
  168. if(pMsg->wParam == VK_TAB && m_ComboBox.m_hWnd!= NULL)
  169. {
  170. DWORD dwStyle = m_ComboBox.GetStyle();
  171. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  172. {
  173. OnComboBoxEnd(TRUE,TRUE);
  174. CRect rcCtrl;
  175. int nItem;
  176. int nSub;
  177. if(FALSE == Key_Ctrl(nItem,nSub))
  178. Key_Shift(nItem,nSub);
  179. CListCtrl::GetSubItemRect(nItem,nSub,LVIR_LABEL,rcCtrl);
  180. CPoint pt(rcCtrl.left+1,rcCtrl.top+1);
  181. OnLButtonDblClk(0,pt);
  182. POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
  183. if (pos == NULL)
  184. {
  185. }
  186. else
  187. {
  188. while (pos)
  189. {
  190. int ntpItem = CListCtrl::GetNextSelectedItem(pos);
  191. CListCtrl::SetItemState(ntpItem,0,LVIS_SELECTED);
  192. }
  193. }
  194. CListCtrl::SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  195. return TRUE;
  196. }
  197. }
  198. }
  199. return CListCtrl::PreTranslateMessage(pMsg);
  200. }
  201. BOOL CComboBoxListCtrl3::Key_Shift(int& nItem,int& nSub)
  202. {
  203. int nItemCount = CListCtrl::GetItemCount();
  204. DWORD dwData = m_ComboBox.GetCtrlData();
  205. nItem= dwData>>16;
  206. nSub = dwData&0x0000ffff;
  207. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  208. if(pHeader == NULL)
  209. return FALSE;
  210. short sRet = GetKeyState(VK_SHIFT);
  211. int nSubcCount = pHeader->GetItemCount();
  212. sRet = sRet >>15;
  213. if(sRet == 0)
  214. {
  215. nSub += 1;
  216. if(nSub >= nSubcCount)
  217. {
  218. if(nItem == nItemCount-1)
  219. {
  220. nItem = 0;
  221. nSub = 0;
  222. }
  223. else
  224. {
  225. nSub = 0;
  226. nItem += 1;
  227. }
  228. }
  229. if(nItem >= nItemCount)
  230. nItem = nItemCount-1;
  231. return FALSE;
  232. }
  233. else
  234. {
  235. nSub -= 1;
  236. if(nSub < 0)
  237. {
  238. nSub = nSubcCount -1;
  239. nItem --;
  240. }
  241. if(nItem < 0)
  242. nItem = nItemCount-1;
  243. return TRUE;
  244. }
  245. return FALSE;
  246. }
  247. BOOL CComboBoxListCtrl3::Key_Ctrl(int& nItem,int &nSub)
  248. {
  249. short sRet = GetKeyState(VK_CONTROL);
  250. DWORD dwData = m_ComboBox.GetCtrlData();
  251. nItem= dwData>>16;
  252. nSub = dwData&0x0000ffff;
  253. sRet = sRet >>15;
  254. int nItemCount = CListCtrl::GetItemCount();
  255. if(sRet == 0)
  256. {
  257. }
  258. else
  259. {
  260. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  261. return TRUE;
  262. }
  263. return FALSE;
  264. }
  265. //////////////////////////////////////////////////////////////////////////
  266. //////////////////////////////////////////////////////////////////////////
  267. //////////////////////////////////////////////////////////////////////////
  268. CListCtrlComboBox3::CListCtrlComboBox3()
  269. {
  270. }
  271. CListCtrlComboBox3::~CListCtrlComboBox3()
  272. {
  273. }
  274. BEGIN_MESSAGE_MAP(CListCtrlComboBox3, CComboBox)
  275. //{{AFX_MSG_MAP(CListCtrlComboBox3)
  276. ON_WM_KILLFOCUS()
  277. ON_WM_SETFOCUS()
  278. ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
  279. //}}AFX_MSG_MAP
  280. END_MESSAGE_MAP()
  281. /////////////////////////////////////////////////////////////////////////////
  282. // CListCtrlComboBox3 message handlers
  283. void CListCtrlComboBox3::SetCtrlData(DWORD dwData)
  284. {
  285. m_dwData = dwData;
  286. }
  287. DWORD CListCtrlComboBox3::GetCtrlData()
  288. {
  289. return m_dwData;
  290. }
  291. void CListCtrlComboBox3::OnKillFocus(CWnd* pNewWnd)
  292. {
  293. CComboBox::OnKillFocus(pNewWnd);
  294. CWnd* pParent = this->GetParent();
  295. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  296. }
  297. BOOL CListCtrlComboBox3::PreTranslateMessage(MSG* pMsg)
  298. {
  299. if(pMsg->message == WM_KEYDOWN)
  300. {
  301. if(pMsg->wParam == VK_RETURN)
  302. {
  303. CWnd* pParent = this->GetParent();
  304. m_bExchange = TRUE;
  305. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  306. }
  307. else if(pMsg->wParam == VK_ESCAPE)
  308. {
  309. CWnd* pParent = this->GetParent();
  310. m_bExchange = FALSE;
  311. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  312. }
  313. }
  314. return CComboBox::PreTranslateMessage(pMsg);
  315. }
  316. void CListCtrlComboBox3::OnSetFocus(CWnd* pOldWnd)
  317. {
  318. CComboBox::OnSetFocus(pOldWnd);
  319. m_bExchange = TRUE;
  320. }
  321. void CComboBoxListCtrl3::InitStyle()
  322. {
  323. SetFont (&g_listctrlfont);
  324. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  325. }
  326. void CListCtrlComboBox3::OnCloseup()
  327. {
  328. // TODO: Add your control notification handler code here
  329. CWnd* pParent = this->GetParent();
  330. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  331. }