16,548
社区成员




class CWLWListBox : public CListBox
{
DECLARE_DYNAMIC(CWLWListBox)
public:
CWLWListBox();
virtual ~CWLWListBox();
int AddString(LPCTSTR lpszItem);
int AddString(LPCTSTR lpszItem, COLORREF rgb);
int InsertString(int nIndex, LPCTSTR lpszItem);
int InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb);
void SetItemColor(int nIndex, COLORREF rgb);
BOOL m_nSelectChange;
COLORREF m_clrFore; // 前景颜色
COLORREF m_clrBack; // 背景颜色
CBrush m_brush;
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnLbnSelchange();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnPaint();
virtual void MeasureItem(LPMEASUREITEMSTRUCT /*lpMeasureItemStruct*/);
};
// WLWListBox.cpp : 实现文件
//
#include "stdafx.h"
#include "WLWListBox.h"
// CWLWListBox
IMPLEMENT_DYNAMIC(CWLWListBox, CListBox)
CWLWListBox::CWLWListBox()
{
m_nSelectChange = FALSE;
}
CWLWListBox::~CWLWListBox()
{
}
int CWLWListBox::AddString(LPCTSTR lpszItem)
{
return ((CListBox*)this)->AddString(lpszItem);
}
int CWLWListBox::AddString(LPCTSTR lpszItem, COLORREF rgb)
{
int nIndex = CListBox::AddString(lpszItem);
if (nIndex >= 0)
SetItemData(nIndex, rgb);
return nIndex;
}
int CWLWListBox::InsertString(int nIndex, LPCTSTR lpszItem)
{
return ((CListBox*)this)->InsertString(nIndex, lpszItem);
}
int CWLWListBox::InsertString(int nIndex, LPCTSTR lpszItem, COLORREF rgb)
{
int nItemIndex = ((CListBox*)this)->InsertString(nIndex, lpszItem);
if (nItemIndex >= 0)
SetItemData(nItemIndex, rgb);
return nItemIndex;
}
void CWLWListBox::SetItemColor(int nIndex, COLORREF rgb)
{
SetItemData(nIndex, rgb);
RedrawWindow();
}
BEGIN_MESSAGE_MAP(CWLWListBox, CListBox)
ON_WM_ERASEBKGND()
ON_CONTROL_REFLECT(LBN_SELCHANGE, &CWLWListBox::OnLbnSelchange)
ON_WM_LBUTTONDOWN()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_PAINT()
END_MESSAGE_MAP()
// CWLWListBox 消息处理程序
BOOL CWLWListBox::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//pDC->SetBkMode(TRANSPARENT);
//CBitmap bitmap;
//bitmap.LoadBitmap(IDB_BITMAP2);
//CDC dcCompatible;
//dcCompatible.CreateCompatibleDC(pDC);
//CBitmap* pOldbitmap = dcCompatible.SelectObject(&bitmap);
//CRect rect;
//GetClientRect(&rect); // 获取控件客户区的矩形区域
//BITMAP bm;
//bitmap.GetBitmap(&bm);
//int nBmWidth = bm.bmWidth;
//int nbmHeight = bm.bmHeight;
//pDC->StretchBlt(0, 0, rect.Width(), rect.Height(), &dcCompatible, 0, 0, nBmWidth, nbmHeight, SRCCOPY);
////pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcCompatible, 0, 0, SRCCOPY);
//dcCompatible.SelectObject(pOldbitmap);
//return TRUE;
return CListBox::OnEraseBkgnd(pDC);
}
void CWLWListBox::OnLbnSelchange()
{
// TODO: 在此添加控件通知处理程序代码
// m_nSelectChange = TRUE;
}
void CWLWListBox::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_nSelectChange)
{
m_nSelectChange = FALSE;
Invalidate();
}
CListBox::OnLButtonDown(nFlags, point);
}
void CWLWListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (lpDrawItemStruct->itemID == -1)
{
DrawFocusRect(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem);
return;
}
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
COLORREF clrOld;
CString sText;
COLORREF clrNew = (COLORREF)(lpDrawItemStruct->itemData);
// item处于被选中的状态,设置高亮
if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
CBrush brush(::GetSysColor(COLOR_HIGHLIGHT));
pDC->FillRect(&lpDrawItemStruct->rcItem, &brush);
}
// item丢失选中状态
if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & ODA_SELECT))
{
CBrush brush(::GetSysColor(COLOR_WINDOW));
pDC->FillRect(&lpDrawItemStruct->rcItem, &brush);
}
// item获得焦点
if ((lpDrawItemStruct->itemAction & ODA_FOCUS) &&
(lpDrawItemStruct->itemState & ODS_FOCUS))
{
pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
}
// item丢失焦点
if ((lpDrawItemStruct->itemAction & ODA_FOCUS) &&
!(lpDrawItemStruct->itemState & ODS_FOCUS))
{
pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
}
// 设置背景模式为透明,并保存原有背景模式
int nBkMode = pDC->SetBkMode(TRANSPARENT);
if (lpDrawItemStruct->itemState & ODS_SELECTED)
clrOld = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
else
if (lpDrawItemStruct->itemState & ODS_DISABLED)
clrOld = pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
else
clrOld = pDC->SetTextColor(clrNew);
// 获取item的text
GetText(lpDrawItemStruct->itemID, sText);
CRect rect = lpDrawItemStruct->rcItem;
// 设置文本显示风格,一定要加上DT_EXPANDTABS
UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
if (GetStyle() & LBS_USETABSTOPS)
nFormat |= DT_EXPANDTABS;
// 绘制文本
pDC->DrawText(sText, -1, &rect, nFormat);
::OutputDebugString(sText + _T("\n"));
// 恢复原有的字体颜色和背景模式
pDC->SetTextColor(clrOld);
pDC->SetBkMode(nBkMode);
}
void CWLWListBox::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
Invalidate();
CListBox::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CWLWListBox::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
Invalidate();
CListBox::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CWLWListBox::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CListBox::OnPaint()
}
void CWLWListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
// TODO: 添加您的代码以确定指定项的大小
}
m_listbox.AddString(_T("空山新雨后"), RGB(255, 0, 0));
m_listbox.AddString(_T("天气晚来秋"), RGB(0, 255, 0));
m_listbox.AddString(_T("明月松间照"), RGB(0, 0, 255));
m_listbox.AddString(_T("清泉石上流"), RGB(255, 0, 255));