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