123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // BaseComboBox.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "HJHomework.h"
- #include "BaseComboBox.h"
- // CBaseComboBox
- IMPLEMENT_DYNAMIC(CBaseComboBox, CComboBox)
- CBaseComboBox::CBaseComboBox()
- {
- m_dwStyle = WS_CHILD | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_OWNERDRAWVARIABLE | CBS_HASSTRINGS | CB_SETITEMHEIGHT;
- m_font.CreatePointFont(80,L"微软雅黑");
- //m_fontmin.CreatePointFont(80,L"微软雅黑");
- }
- CBaseComboBox::~CBaseComboBox()
- {
- m_font.DeleteObject();
- }
- BEGIN_MESSAGE_MAP(CBaseComboBox, CComboBox)
- ON_CONTROL_REFLECT(CBN_SELCHANGE, &CBaseComboBox::OnCbnSelchange)
- ON_WM_CREATE()
- END_MESSAGE_MAP()
- // CBaseComboBox 消息处理程序
- BOOL CBaseComboBox::Create(CWnd* pParentWnd, UINT nID, const RECT& rect)
- {
- // TODO: 在此添加专用代码和/或调用基类
- return CComboBox::Create(m_dwStyle, rect, pParentWnd, nID);
- }
- void CBaseComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpmis)
- {
- // TODO: 添加您的代码以确定指定项的大小
- lpmis->itemHeight = 31;
- }
- void CBaseComboBox::DrawItem(LPDRAWITEMSTRUCT lpdis)
- {
- // TODO: 添加您的代码以绘制指定项
- // 获取绘画环境
- CDC* pDC = CDC::FromHandle(lpdis->hDC);
- COLORREF fontColor, backColor;
- fontColor = RGB(50,50,50);
- // 如果该项被选中,则绘画选中的背景
- if ((lpdis->itemState & ODS_SELECTED) &&
- (lpdis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
- {
- backColor = GetSysColor(COLOR_HIGHLIGHT);
- fontColor = RGB(255,255,255);
- pDC->FillRect(&lpdis->rcItem, &CBrush(backColor));
- }
- // 如何该项没有被选中,则绘画一个白背景
- if (!(lpdis->itemState & ODS_SELECTED) &&
- (lpdis->itemAction & ODA_SELECT))
- {
- backColor = RGB(255, 255, 255);
- pDC->FillRect(&lpdis->rcItem, &CBrush(backColor));
- }
- CRect rcItem(lpdis->rcItem.left,lpdis->rcItem.top,lpdis->rcItem.right,lpdis->rcItem.bottom);
- rcItem.left += 5;
- rcItem.right -= 5;
- // 获取当前数据项的字符串
- CString lpszText;
- if((int)(lpdis->itemID) >= 0)
- GetLBText(lpdis->itemID, lpszText);
- pDC->SelectObject(m_font);
- pDC->SetBkMode(TRANSPARENT);
- pDC->SetTextColor(fontColor);
- CSize sz = pDC->GetTextExtent(lpszText);
- if(sz.cx > rcItem.Width()) // 多行
- {
- pDC->DrawText(lpszText,rcItem,DT_LEFT|DT_WORDBREAK);
- }
- else // 单行
- {
- pDC->DrawText(lpszText,rcItem,DT_SINGLELINE|DT_VCENTER|DT_LEFT);
- }
- //pDC->DrawText(lpszText,rcItem,DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS);
- }
- void CBaseComboBox::PreSubclassWindow()
- {
- // TODO: 在此添加专用代码和/或调用基类
- CComboBox::PreSubclassWindow();
- }
- LRESULT CBaseComboBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: 在此添加专用代码和/或调用基类
- return CComboBox::WindowProc(message, wParam, lParam);
- }
- void CBaseComboBox::OnCbnSelchange()
- {
- // TODO: 在此添加控件通知处理程序代码
- GetParent()->PostMessage(WM_COMBOBOX_SELCHANGE,0,0);
- }
- int CBaseComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CComboBox::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: 在此添加您专用的创建代码
- SetMinVisibleItems(5);
- return 0;
- }
|