ComboListCtrl4.cpp 9.0 KB

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