// ODrawCombo.cpp : implementation file // #include "stdafx.h" #include "mybox.h" // CMyBox IMPLEMENT_DYNAMIC(CMyBox, CComboBox) CMyBox::CMyBox() { } CMyBox::~CMyBox() { } BEGIN_MESSAGE_MAP(CMyBox, CComboBox) END_MESSAGE_MAP() // CMyBox message handlers void CMyBox::Init() { } void CMyBox::AddItem(CString nItem) { this->AddString(nItem); } void CMyBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX); if(lpDrawItemStruct->itemID != 0xffffffff) { // 1 - Get combobox item ID UINT id = (UINT) (WORD) lpDrawItemStruct->itemID; // Consistency checks ASSERT(id == lpDrawItemStruct->itemID); // 2 - Get device context & item rectangle CDC dc; dc.Attach(lpDrawItemStruct->hDC); CRect rcItem = lpDrawItemStruct->rcItem; int nCurrentX = rcItem.left; // 3 - Select background and foreground // colors depending on item selected state COLORREF crBack, crFore; if(lpDrawItemStruct->itemState & ODS_SELECTED) { crBack = ::GetSysColor(COLOR_HIGHLIGHT); crFore = ::GetSysColor(COLOR_HIGHLIGHTTEXT); } else { crBack = ::GetSysColor(COLOR_WINDOW); crFore = ::GetSysColor(COLOR_WINDOWTEXT); } // 4 - Fill item background CRect rcBack(nCurrentX, rcItem.top, rcItem.right, rcItem.bottom); CBrush brFill(crBack); dc.FillRect(&rcBack, &brFill); // 5 - Draw item color rectangle CBrush brColor(::GetSysColor(COLOR_WINDOWTEXT)); CBrush* pOldBrush = dc.SelectObject(&brColor); CRect rcColor(nCurrentX, rcItem.top, nCurrentX, rcItem.bottom); dc.Rectangle(rcColor); dc.SelectObject(pOldBrush); // 6 - Draw item text COLORREF crOldTextColor = dc.SetTextColor(crFore); int nOldBkMode = dc.SetBkMode(TRANSPARENT); CString str; this->GetLBText(id, str); dc.DrawText(str, -1, rcItem, DT_LEFT|DT_WORDBREAK); dc.SetTextColor(crOldTextColor); dc.SetBkMode(nOldBkMode); dc.Detach(); } } void CMyBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { /*DWORD dwStyle = ::GetWindowLong( m_hWnd, GWL_STYLE );*/ /*ASSERT( dwStyle & CBS_DROPDOWNLIST );*/ CFont* pFont = GetFont(); CFont* pOldFont = 0; CString str; CDC* pDC = GetDC(); pOldFont = pDC->SelectObject(pFont); // Get text from combo box this->GetLBText(lpMeasureItemStruct->itemID, str); // Obtain rectangle that text is originally in CRect rc; GetClientRect(&rc); //GetWindowRect(&rc); rc.left = 0; rc.top = 0; rc.bottom = 0; // With the inclusion of DT_CALCRECT // this DrawText does not draw the text // it just measures the rectangle pDC->DrawText(str, str.GetLength(), &rc, DT_CALCRECT|DT_LEFT|DT_WORDBREAK); lpMeasureItemStruct->itemHeight = rc.bottom; pDC->SelectObject(pOldFont); ReleaseDC(pDC); }