ComboListCtrl.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // ComboBoxListCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ComboListCtrl.h"
  5. #include <windowsx.h>
  6. #include "UPhoneBox.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. // CComboBoxListCtrl
  17. extern CFont g_listctrlfont;
  18. CComboBoxListCtrl::CComboBoxListCtrl()
  19. {
  20. m_ComboBox.m_hWnd = NULL;
  21. }
  22. CComboBoxListCtrl::~CComboBoxListCtrl()
  23. {
  24. }
  25. BEGIN_MESSAGE_MAP(CComboBoxListCtrl, CListCtrl)
  26. //{{AFX_MSG_MAP(CComboBoxListCtrl)
  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 CComboBoxListCtrl::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 CComboBoxListCtrl::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>1)
  77. if(nSubItem==2||nSubItem==4||nSubItem==6||nSubItem==8)
  78. ShowComboBox(TRUE,nItem,nSubItem,rcCtrl);
  79. CListCtrl::OnLButtonDblClk(nFlags, point);
  80. }
  81. void CComboBoxListCtrl::ShowComboBox(BOOL bShow,int nItem,int nIndex,CRect rcCtrl)
  82. {
  83. m_ComboBox.ShowWindow(SW_HIDE);
  84. }
  85. #if 0
  86. void CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  87. {
  88. m_ComboBox.ShowWindow(SW_HIDE);
  89. }
  90. #else
  91. LRESULT CComboBoxListCtrl::OnComboBoxEnd(WPARAM wParam,LPARAM lParam)
  92. {
  93. m_ComboBox.ShowWindow(SW_HIDE);
  94. return 0;
  95. }
  96. #endif
  97. void CComboBoxListCtrl::OnParentNotify(UINT message, LPARAM lParam)
  98. {
  99. CListCtrl::OnParentNotify(message, lParam);
  100. //////////////////////////////////////////////////////////////////////////
  101. CHeaderCtrl* pHeaderCtrl = CListCtrl::GetHeaderCtrl();
  102. if(pHeaderCtrl == NULL)
  103. return;
  104. CRect rcHeader;
  105. pHeaderCtrl->GetWindowRect(rcHeader);
  106. ScreenToClient(rcHeader);
  107. //The x coordinate is in the low-order word and the y coordinate is in the high-order word.
  108. CPoint pt;
  109. pt.x = GET_X_LPARAM(lParam);
  110. pt.y = GET_Y_LPARAM(lParam);
  111. if(rcHeader.PtInRect(pt) && message == WM_LBUTTONDOWN)
  112. {
  113. if(m_ComboBox.m_hWnd != NULL)
  114. {
  115. DWORD dwStyle = m_ComboBox.GetStyle();
  116. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  117. {
  118. m_ComboBox.ShowWindow(SW_HIDE);
  119. }
  120. }
  121. }
  122. }
  123. BOOL CComboBoxListCtrl::PreTranslateMessage(MSG* pMsg)
  124. {
  125. if(0)//pMsg->message == WM_KEYDOWN)
  126. {
  127. if(pMsg->wParam == VK_TAB && m_ComboBox.m_hWnd!= NULL)
  128. {
  129. DWORD dwStyle = m_ComboBox.GetStyle();
  130. if((dwStyle&WS_VISIBLE) == WS_VISIBLE)
  131. {
  132. OnComboBoxEnd(TRUE,TRUE);
  133. CRect rcCtrl;
  134. int nItem;
  135. int nSub;
  136. if(FALSE == Key_Ctrl(nItem,nSub))
  137. Key_Shift(nItem,nSub);
  138. CListCtrl::GetSubItemRect(nItem,nSub,LVIR_LABEL,rcCtrl);
  139. CPoint pt(rcCtrl.left+1,rcCtrl.top+1);
  140. OnLButtonDblClk(0,pt);
  141. POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
  142. if (pos == NULL)
  143. {
  144. }
  145. else
  146. {
  147. while (pos)
  148. {
  149. int ntpItem = CListCtrl::GetNextSelectedItem(pos);
  150. CListCtrl::SetItemState(ntpItem,0,LVIS_SELECTED);
  151. }
  152. }
  153. CListCtrl::SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
  154. return TRUE;
  155. }
  156. }
  157. }
  158. return CListCtrl::PreTranslateMessage(pMsg);
  159. }
  160. BOOL CComboBoxListCtrl::Key_Shift(int& nItem,int& nSub)
  161. {
  162. int nItemCount = CListCtrl::GetItemCount();
  163. DWORD dwData = m_ComboBox.GetCtrlData();
  164. nItem= dwData>>16;
  165. nSub = dwData&0x0000ffff;
  166. CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
  167. if(pHeader == NULL)
  168. return FALSE;
  169. short sRet = GetKeyState(VK_SHIFT);
  170. int nSubcCount = pHeader->GetItemCount();
  171. sRet = sRet >>15;
  172. if(sRet == 0)
  173. {
  174. nSub += 1;
  175. if(nSub >= nSubcCount)
  176. {
  177. if(nItem == nItemCount-1)
  178. {
  179. nItem = 0;
  180. nSub = 0;
  181. }
  182. else
  183. {
  184. nSub = 0;
  185. nItem += 1;
  186. }
  187. }
  188. if(nItem >= nItemCount)
  189. nItem = nItemCount-1;
  190. return FALSE;
  191. }
  192. else
  193. {
  194. nSub -= 1;
  195. if(nSub < 0)
  196. {
  197. nSub = nSubcCount -1;
  198. nItem --;
  199. }
  200. if(nItem < 0)
  201. nItem = nItemCount-1;
  202. return TRUE;
  203. }
  204. return FALSE;
  205. }
  206. BOOL CComboBoxListCtrl::Key_Ctrl(int& nItem,int &nSub)
  207. {
  208. short sRet = GetKeyState(VK_CONTROL);
  209. DWORD dwData = m_ComboBox.GetCtrlData();
  210. nItem= dwData>>16;
  211. nSub = dwData&0x0000ffff;
  212. sRet = sRet >>15;
  213. int nItemCount = CListCtrl::GetItemCount();
  214. if(sRet == 0)
  215. {
  216. }
  217. else
  218. {
  219. nItem = nItem >=nItemCount-1? 0:nItem+=1;
  220. return TRUE;
  221. }
  222. return FALSE;
  223. }
  224. //////////////////////////////////////////////////////////////////////////
  225. //////////////////////////////////////////////////////////////////////////
  226. //////////////////////////////////////////////////////////////////////////
  227. CListCtrlComboBox::CListCtrlComboBox()
  228. {
  229. }
  230. CListCtrlComboBox::~CListCtrlComboBox()
  231. {
  232. }
  233. BEGIN_MESSAGE_MAP(CListCtrlComboBox, CComboBox)
  234. //{{AFX_MSG_MAP(CListCtrlComboBox)
  235. ON_WM_KILLFOCUS()
  236. ON_WM_SETFOCUS()
  237. ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseup)
  238. //}}AFX_MSG_MAP
  239. END_MESSAGE_MAP()
  240. /////////////////////////////////////////////////////////////////////////////
  241. // CListCtrlComboBox message handlers
  242. void CListCtrlComboBox::SetCtrlData(DWORD dwData)
  243. {
  244. m_dwData = dwData;
  245. }
  246. DWORD CListCtrlComboBox::GetCtrlData()
  247. {
  248. return m_dwData;
  249. }
  250. void CListCtrlComboBox::OnKillFocus(CWnd* pNewWnd)
  251. {
  252. CComboBox::OnKillFocus(pNewWnd);
  253. CWnd* pParent = this->GetParent();
  254. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  255. }
  256. BOOL CListCtrlComboBox::PreTranslateMessage(MSG* pMsg)
  257. {
  258. if(pMsg->message == WM_KEYDOWN)
  259. {
  260. if(pMsg->wParam == VK_RETURN)
  261. {
  262. CWnd* pParent = this->GetParent();
  263. m_bExchange = TRUE;
  264. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  265. }
  266. else if(pMsg->wParam == VK_ESCAPE)
  267. {
  268. CWnd* pParent = this->GetParent();
  269. m_bExchange = FALSE;
  270. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  271. }
  272. }
  273. return CComboBox::PreTranslateMessage(pMsg);
  274. }
  275. void CListCtrlComboBox::OnSetFocus(CWnd* pOldWnd)
  276. {
  277. CComboBox::OnSetFocus(pOldWnd);
  278. m_bExchange = TRUE;
  279. }
  280. void CComboBoxListCtrl::InitStyle()
  281. {
  282. SetFont (&g_listctrlfont);
  283. SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
  284. }
  285. void CListCtrlComboBox::OnCloseup()
  286. {
  287. // TODO: Add your control notification handler code here
  288. CWnd* pParent = this->GetParent();
  289. ::PostMessage(pParent->GetSafeHwnd(),WM_USER_ComboBox_END,m_bExchange,0);
  290. }
  291. #undef SubclassWindow
  292. void CComboBoxListCtrl::PreSubclassWindow()
  293. {
  294. // the list control must have the report style.
  295. CListCtrl::PreSubclassWindow();
  296. m_ctlHeader.SubclassWindow ( GetHeaderCtrl()->GetSafeHwnd());
  297. }