#include "stdafx.h"
class CCustomListBox : public CListBox
{
public:
CCustomListBox()
{
m_rgbNormalText = RGB( 0, 0, 0 );
m_rgbNormalBack = RGB( 255, 255, 255);
m_rgbWarningText = RGB( 255, 0, 0 );
m_rgbWarningBack = RGB( 255, 255, 0 );
m_nHeight = 20;
m_szMargin = CRect(5, 0, 0, 0);
}
COLORREF m_rgbNormalText, m_rgbNormalBack; // 일반글자색과배경색
COLORREF m_rgbWarningText, m_rgbWarningBack; // 경고일때
UINT m_nHeight; // 셀의높이
CRect m_szMargin; // 글자의마진
int AddString(LPCTSTR lpszItem)
{
int n = CListBox::AddString( lpszItem );
SendMessage(WM_VSCROLL, SB_BOTTOM, NULL);
return n;
}
public:
virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
virtual void MeasureItem(LPMEASUREITEMSTRUCT /*lpMeasureItemStruct*/);
};
#include "ListBoxEx.h"
void CCustomListBox::DrawItem(LPDRAWITEMSTRUCT pDIStruct)
{
CDC dc;
// 코드를간결하게하기위하여전달된HDC를CDC에어태치한다.
if( !dc.Attach( pDIStruct -> hDC ) )
return;
// 현재전달된아이템이선택되어진넘인이확인한다.
if( pDIStruct -> itemState & ODS_SELECTED )
{
// 속성에맞게글자색, 배경색상을지정한다.
dc.SetTextColor((0x00FFFFFF & ~(GetSysColor(COLOR_WINDOWTEXT))));
dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT));
dc.FillSolidRect(&pDIStruct->rcItem, GetSysColor(COLOR_HIGHLIGHT));
}
else
{
dc.SetTextColor(m_rgbNormalText);
dc.SetBkColor(m_rgbNormalBack);
dc.FillSolidRect(&pDIStruct->rcItem, m_rgbNormalBack);
}
// 아이템의인덱스가-1이아니면
if(pDIStruct->itemID != -1)
{
// 선택된아이템의문자열을읽어온다.
CString m_SelText;
GetText(pDIStruct->itemID, m_SelText);
if( wcsstr( m_SelText, _T("[경고]") ) )
{
dc.SetTextColor(m_rgbWarningText);
dc.SetBkColor(m_rgbWarningBack);
dc.FillSolidRect(&pDIStruct->rcItem, m_rgbWarningBack);
}
// 만약선택된아이템이디저블속성이면글자색상을회색으로
if(pDIStruct->itemState & ODS_DISABLED)
dc.SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
// 배경은투명속성으로
dc.SetBkMode(TRANSPARENT);
// 글자를그릴영역의여백을조절한다..
CRect rcText = pDIStruct->rcItem;
rcText.left += m_szMargin.left;
rcText.top += m_szMargin.top;
rcText.right -= m_szMargin.right;
rcText.bottom -= m_szMargin.bottom;
dc.DrawText(m_SelText, rcText, DT_VCENTER | DT_SINGLELINE);
}
dc.Detach();
}
void CCustomListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = m_nHeight;
}