ListBoxEx.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // ListBoxEx.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "ListBoxEx.h"
  5. // CListBoxEx
  6. IMPLEMENT_DYNAMIC(CListBoxEx, CListBox)
  7. CListBoxEx::CListBoxEx()
  8. {
  9. m_nItemHeight = 12;
  10. }
  11. CListBoxEx::~CListBoxEx()
  12. {
  13. }
  14. BEGIN_MESSAGE_MAP(CListBoxEx, CListBox)
  15. ON_WM_LBUTTONDOWN()
  16. END_MESSAGE_MAP()
  17. // CListBoxEx 消息处理程序
  18. void CListBoxEx::SetItemHeight( int nItemHeight )
  19. {
  20. m_nItemHeight = nItemHeight;
  21. }
  22. void CListBoxEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  23. {
  24. lpMeasureItemStruct->itemHeight=m_nItemHeight;
  25. }
  26. void CListBoxEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  27. {
  28. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  29. bool bIsSel = lpDrawItemStruct->itemState & ODS_SELECTED;
  30. CRect rcItem = lpDrawItemStruct->rcItem;
  31. //绘制背景
  32. pDC->FillSolidRect(rcItem,bIsSel?RGB(234,241,249):RGB(195,215,238));
  33. CString strItemText;
  34. GetText( lpDrawItemStruct->itemID, strItemText );
  35. pDC->SetBkMode(TRANSPARENT);
  36. pDC->SetTextColor(bIsSel?RGB(105,146,1):RGB(0,0,0));
  37. CFont font;
  38. font.CreateFont(18,0,0,0,FW_NORMAL,0,FALSE,0,0,0,0,0,0,TEXT("微软雅黑"));
  39. CFont* pOldFont = pDC->SelectObject(&font);
  40. pDC->DrawText(strItemText,&CRect(rcItem.left,rcItem.top,rcItem.right-10,rcItem.bottom),DT_CENTER|DT_VCENTER);
  41. pDC->SelectObject(pOldFont);
  42. }
  43. int CListBoxEx::CompareItem(LPCOMPAREITEMSTRUCT /*lpCompareItemStruct*/)
  44. {
  45. // TODO: 添加您的代码以确定指定项的排序顺序
  46. // 返回 -1 表示项 1 排在项 2 之前
  47. // 返回 0 表示项 1 和项 2 顺序相同
  48. // 返回 1 表示项 1 排在项 2 之后
  49. return 0;
  50. }
  51. void CListBoxEx::OnLButtonDown(UINT nFlags, CPoint point)
  52. {
  53. CRect rcItem(0,0,0,0);
  54. for (int i =0; i<GetCount(); i++)
  55. {
  56. GetItemRect(i, &rcItem);
  57. if(rcItem.PtInRect(point))
  58. {
  59. CWnd *pParentWnd = GetParent();
  60. if ( pParentWnd != NULL && pParentWnd->GetSafeHwnd() != NULL )
  61. {
  62. pParentWnd->PostMessage(WM_ACTIVE_ITEM,i,0);
  63. }
  64. break;
  65. }
  66. }
  67. CListBox::OnLButtonDown(nFlags, point);
  68. }