123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- // MultiLineListBox.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MultiLineListBox.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMultiLineListBox
- BEGIN_MESSAGE_MAP(CMultiLineListBox, CListBox)
- //{{AFX_MSG_MAP(CMultiLineListBox)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMultiLineListBox message handlers
- void CMultiLineListBox::AddEntry(LPCTSTR lpszItem, COLORREF color, int nIndex /* 0 */)
- {
- int index = InsertString(nIndex, lpszItem);
- SetItemData(index,color);
- }
- void CMultiLineListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
- {
- // all items are of fixed size
- // must use LBS_OWNERDRAWVARIABLE for this to work
- int nItem = lpMIS->itemID;
- CPaintDC dc(this);
- CString sLabel;
- CRect rcLabel;
- GetText( nItem, sLabel );
- GetItemRect(nItem, rcLabel);
- // Using the flags below, calculate the required rectangle for
- // the text and set the item height for this specific item based
- // on the return value (new height).
- int itemHeight = dc.DrawText( sLabel, -1, rcLabel, DT_WORDBREAK | DT_CALCRECT );
- lpMIS->itemHeight = itemHeight;
- }
- void CMultiLineListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
- {
- CDC* pDC = CDC::FromHandle(lpDIS->hDC);
- COLORREF rColor = (COLORREF)lpDIS->itemData; // RGB in item data
- CString sLabel;
- GetText(lpDIS->itemID, sLabel);
- // item selected
- if ((lpDIS->itemState & ODS_SELECTED) &&
- (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
- {
- // draw color box
- CBrush colorBrush(rColor);
- CRect colorRect = lpDIS->rcItem;
- // draw label background
- CBrush labelBrush(::GetSysColor(COLOR_HIGHLIGHT));
- CRect labelRect = lpDIS->rcItem;
- pDC->FillRect(&labelRect,&labelBrush);
- // draw label text
- COLORREF colorTextSave;
- COLORREF colorBkSave;
- colorTextSave = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
- colorBkSave = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
- pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
- pDC->SetTextColor(colorTextSave);
- pDC->SetBkColor(colorBkSave);
- return;
- }
- // item brought into box
- if (lpDIS->itemAction & ODA_DRAWENTIRE)
- {
- CBrush brush(rColor);
- CRect rect = lpDIS->rcItem;
- pDC->SetBkColor(rColor);
- pDC->FillRect(&rect,&brush);
- pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
- return;
- }
- // item deselected
- if (!(lpDIS->itemState & ODS_SELECTED) &&
- (lpDIS->itemAction & ODA_SELECT))
- {
- CRect rect = lpDIS->rcItem;
- CBrush brush(rColor);
- pDC->SetBkColor(rColor);
- pDC->FillRect(&rect,&brush);
- pDC->DrawText( sLabel, -1, &lpDIS->rcItem, DT_WORDBREAK );
- return;
- }
- }
|