BaseComboBox.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // BaseComboBox.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "HJHomework.h"
  5. #include "BaseComboBox.h"
  6. // CBaseComboBox
  7. IMPLEMENT_DYNAMIC(CBaseComboBox, CComboBox)
  8. CBaseComboBox::CBaseComboBox()
  9. {
  10. m_dwStyle = WS_CHILD | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_OWNERDRAWVARIABLE | CBS_HASSTRINGS | CB_SETITEMHEIGHT;
  11. m_font.CreatePointFont(80,L"微软雅黑");
  12. //m_fontmin.CreatePointFont(80,L"微软雅黑");
  13. }
  14. CBaseComboBox::~CBaseComboBox()
  15. {
  16. m_font.DeleteObject();
  17. }
  18. BEGIN_MESSAGE_MAP(CBaseComboBox, CComboBox)
  19. ON_CONTROL_REFLECT(CBN_SELCHANGE, &CBaseComboBox::OnCbnSelchange)
  20. ON_WM_CREATE()
  21. END_MESSAGE_MAP()
  22. // CBaseComboBox 消息处理程序
  23. BOOL CBaseComboBox::Create(CWnd* pParentWnd, UINT nID, const RECT& rect)
  24. {
  25. // TODO: 在此添加专用代码和/或调用基类
  26. return CComboBox::Create(m_dwStyle, rect, pParentWnd, nID);
  27. }
  28. void CBaseComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpmis)
  29. {
  30. // TODO: 添加您的代码以确定指定项的大小
  31. lpmis->itemHeight = 31;
  32. }
  33. void CBaseComboBox::DrawItem(LPDRAWITEMSTRUCT lpdis)
  34. {
  35. // TODO: 添加您的代码以绘制指定项
  36. // 获取绘画环境
  37. CDC* pDC = CDC::FromHandle(lpdis->hDC);
  38. COLORREF fontColor, backColor;
  39. fontColor = RGB(50,50,50);
  40. // 如果该项被选中,则绘画选中的背景
  41. if ((lpdis->itemState & ODS_SELECTED) &&
  42. (lpdis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
  43. {
  44. backColor = GetSysColor(COLOR_HIGHLIGHT);
  45. fontColor = RGB(255,255,255);
  46. pDC->FillRect(&lpdis->rcItem, &CBrush(backColor));
  47. }
  48. // 如何该项没有被选中,则绘画一个白背景
  49. if (!(lpdis->itemState & ODS_SELECTED) &&
  50. (lpdis->itemAction & ODA_SELECT))
  51. {
  52. backColor = RGB(255, 255, 255);
  53. pDC->FillRect(&lpdis->rcItem, &CBrush(backColor));
  54. }
  55. CRect rcItem(lpdis->rcItem.left,lpdis->rcItem.top,lpdis->rcItem.right,lpdis->rcItem.bottom);
  56. rcItem.left += 5;
  57. rcItem.right -= 5;
  58. // 获取当前数据项的字符串
  59. CString lpszText;
  60. if((int)(lpdis->itemID) >= 0)
  61. GetLBText(lpdis->itemID, lpszText);
  62. pDC->SelectObject(m_font);
  63. pDC->SetBkMode(TRANSPARENT);
  64. pDC->SetTextColor(fontColor);
  65. CSize sz = pDC->GetTextExtent(lpszText);
  66. if(sz.cx > rcItem.Width()) // 多行
  67. {
  68. pDC->DrawText(lpszText,rcItem,DT_LEFT|DT_WORDBREAK);
  69. }
  70. else // 单行
  71. {
  72. pDC->DrawText(lpszText,rcItem,DT_SINGLELINE|DT_VCENTER|DT_LEFT);
  73. }
  74. //pDC->DrawText(lpszText,rcItem,DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
  75. }
  76. void CBaseComboBox::PreSubclassWindow()
  77. {
  78. // TODO: 在此添加专用代码和/或调用基类
  79. CComboBox::PreSubclassWindow();
  80. }
  81. LRESULT CBaseComboBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  82. {
  83. // TODO: 在此添加专用代码和/或调用基类
  84. return CComboBox::WindowProc(message, wParam, lParam);
  85. }
  86. void CBaseComboBox::OnCbnSelchange()
  87. {
  88. // TODO: 在此添加控件通知处理程序代码
  89. GetParent()->PostMessage(WM_COMBOBOX_SELCHANGE,0,0);
  90. }
  91. int CBaseComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
  92. {
  93. if (CComboBox::OnCreate(lpCreateStruct) == -1)
  94. return -1;
  95. // TODO: 在此添加您专用的创建代码
  96. SetMinVisibleItems(5);
  97. return 0;
  98. }