// ListBoxEx.cpp : 实现文件
//

#include "stdafx.h"
#include "ListBoxEx.h"


// CListBoxEx

IMPLEMENT_DYNAMIC(CListBoxEx, CListBox)

CListBoxEx::CListBoxEx(): m_nCurSel(0)
, m_nPreSel(0)
{
	m_clrSel  = RGB(0,0,255);
	m_clrBack = RGB(255,255,255);
	m_clrText = RGB(255,255,255);
	m_bIsEdge = TRUE;
	m_nWidth  = 16;
	m_nHeight = 16;
}

CListBoxEx::~CListBoxEx()
{
	while(m_pItemList.GetCount())
	{
		CListItem* pItem = m_pItemList.GetHead();
		delete pItem;
		pItem = NULL;
		m_pItemList.RemoveHead();
	}
}


BEGIN_MESSAGE_MAP(CListBoxEx, CListBox)
	ON_CONTROL_REFLECT(LBN_SELCHANGE, /*&CListBoxEx::*/OnLbnSelchange)
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()



// CListBoxEx 消息处理程序



void CListBoxEx::OnLbnSelchange()
{
	// TODO: 在此添加控件通知处理程序代码
//	m_nPreSel = m_nCurSel;
	if (m_nPreSel != m_nCurSel)
	{
		CRect rect;
		GetItemRect(m_nPreSel, rect);
		InvalidateRect(rect, TRUE);

		GetItemRect(m_nCurSel, rect);
		InvalidateRect(rect, TRUE);
		m_nPreSel = m_nCurSel;
	}
}

void CListBoxEx::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	BOOL bData;
	m_nCurSel = ItemFromPoint(point,bData);
	OnLbnSelchange();
	CListBox::OnLButtonUp(nFlags, point);
}

void CListBoxEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{

	// TODO:  添加您的代码以确定指定项的大小
	lpMeasureItemStruct->itemHeight = m_nHeight; //设置Item高度
}

void CListBoxEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	TRACE("DrawItem");

	// TODO:  添加您的代码以绘制指定项
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	CRect rectItem(lpDrawItemStruct->rcItem);
	CRect rectIcon(rectItem.left, rectItem.top, rectItem.left+m_nWidth, rectItem.top+m_nHeight);
	CRect rectText(rectIcon.right,rectItem.top,rectItem.right,rectItem.bottom);

	CListItem* pItem = static_cast<CListItem*>(GetItemDataPtr(lpDrawItemStruct->itemID));
	pDC->SetBkMode(TRANSPARENT);

	UINT action, state;
	action = lpDrawItemStruct->itemAction;
	state  = lpDrawItemStruct->itemState;

	// 绘制整个Item或者Item没有被选中将要选中，填充背景
	if ((action & ODA_DRAWENTIRE) || ( !(state & ODS_SELECTED) && (action & ODA_SELECT)))
	{
		pDC->FillSolidRect(rectItem, m_clrBack);                        // 填充背景
		if (m_bIsEdge)
		{
			pDC->DrawEdge(rectItem, EDGE_SUNKEN, BF_BOTTOM); // 画边框
		}

		if (lpDrawItemStruct->itemData != NULL)
		{		
			// 画图标
			CRect rect(rectIcon);
			rect.DeflateRect(0,5,0,0);
			CDC dcMem;
			dcMem.CreateCompatibleDC(pDC);

			CBitmap bmp;
			bmp.LoadBitmap(pItem->m_nBitMap);
			CBitmap* pOldBmp = dcMem.SelectObject(&bmp);
			pDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);
			dcMem.SelectObject(pOldBmp);
			bmp.DeleteObject();
			// 文字
			//pDC->TextOut(rectText.left+2, rectText.top,pItem->m_szItemName);		
			rect = rectText;
			rect.DeflateRect(0,5,0,0);
			pDC->SetTextColor(RGB(0,0,0));
			rect.OffsetRect(2,0);
			if (pItem->m_szItemName != NULL)
			{
				pDC->DrawText(pItem->m_szItemName, lstrlen(pItem->m_szItemName),rect, DT_LEFT | DT_SINGLELINE);
			}
		}
	}

	// Item被选中
	if ( (state & ODS_SELECTED) && (action & (ODA_SELECT | ODA_DRAWENTIRE)))
	{
		CRect rect(rectItem);
		// 背景
		CPen Pen(PS_SOLID, 1, RGB(0, 0, 0));
		CPen* pOldPen = pDC->SelectObject(&Pen);
		pDC->Rectangle(rect);
		pDC->SelectObject(pOldPen);

		rect.DeflateRect(0,1,0,0);
		pDC->FillRect(rect, &CBrush(m_clrSel));

		// 边框
		if (m_bIsEdge)
		{
			pDC->DrawEdge(rect, EDGE_SUNKEN, BF_BOTTOM);
		}

		// 图标
		rect = rectIcon;
		rect.DeflateRect(0,5,0,0);
		CDC dcMem;
		dcMem.CreateCompatibleDC(pDC);

		CBitmap bmp;
		bmp.LoadBitmap(pItem->m_nBitMap);
		CBitmap* pOldBmp = dcMem.SelectObject(&bmp);
		pDC->BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);
		dcMem.SelectObject(pOldBmp);
		bmp.DeleteObject();

		// 文字
		rect.CopyRect(rectText);
		rect.DeflateRect(0,5,0,0);
		pDC->SetTextColor(m_clrText);   // 被选中时的颜色
		rect.OffsetRect(10,0);
		if (pItem->m_szItemName != NULL)
		{
			pDC->DrawText(pItem->m_szItemName, lstrlen(pItem->m_szItemName),rect, DT_LEFT | DT_SINGLELINE);
		}
	}
}

int CListBoxEx::VKeyToItem(UINT nKey, UINT nIndex)
{

	// TODO:  添加处理特定虚拟键的代码
	// 返回 -1 = 默认操作
	// 返回 -2 = 没有进一步的操作
	// 返回索引 = 执行以下项上的键击的默认操作
	//               索引指定的项
	if ((nKey == VK_UP) && (nIndex > 0))
		SetCurSel(nIndex);

	else if ((nKey == VK_DOWN) && (nIndex < (UINT)GetCount()))
		SetCurSel(nIndex);

	return -1;
}

void CListBoxEx::PreSubclassWindow()
{
	// TODO: 在此添加专用代码和/或调用基类
    m_nCurSel = -1;
	//CListBox::PreSubclassWindow();
}

void CListBoxEx::AddItem( CString lpszItemName, UINT nBitMap )
{
	CListItem* pItem = new CListItem(lpszItemName, nBitMap);
	m_pItemList.AddTail(pItem);
	int nIndex = AddString(_T(""));   // 返回值为Item在ListBox中的序号based zero
	SetItemDataPtr(nIndex, pItem);
}

void CListBoxEx::SetSelClr( COLORREF clr )
{
	m_clrSel = clr;
}

void CListBoxEx::SetBackClr( COLORREF clr )
{
	m_clrBack = clr;
}

void CListBoxEx::SetTextClr( COLORREF clr )
{
	m_clrText = clr;
}

void CListBoxEx::SetCurSel( int nSel )
{
	m_nCurSel = nSel;
	CListBox::SetCurSel(m_nCurSel);
	OnLbnSelchange();
}

int CListBoxEx::GetCurSel()
{
	return m_nCurSel;
}

void CListBoxEx::EnableEdge( BOOL bEnable )
{
	m_bIsEdge = bEnable;
}

void CListBoxEx::SetIconSize( int nWidth, int nHeight )
{
	m_nWidth = nWidth;
	m_nHeight = nHeight;
}
