ComboListCtrl3.cpp 9.4 KB

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