ComboListCtrl4.cpp 9.0 KB

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