ComboListCtrl4.cpp 8.5 KB

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