ComboListCtrl3.cpp 9.3 KB

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