还没有写过什么,第一次了解这些,见笑了,其实也就是贴下代码:
//============================#include "CMyListBox.h"======================================
#pragma once
// CMyListBox
class CMyListBox : public CListBox
{
DECLARE_DYNAMIC(CMyListBox)
public:
CMyListBox();
virtual ~CMyListBox();
protected:
DECLARE_MESSAGE_MAP()
int m_itemHeight;
CRect m_itemBound;
int m_leftOffset;
int m_topOffset;
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
void AddItem(TCHAR* pItem);
void SetEveryItemHeight(int hight);
void SetLeftTop(int nleft,int ntop);
void GetMySelText(CString& strTxt);
};
//========================================#include "CMyListBox.cpp"=========================
// CMyListBox.cpp : 实现文件
//
#include "stdafx.h"
#include "CListBoxReDraw.h"
#include "CMyListBox.h"
// CMyListBox
IMPLEMENT_DYNAMIC(CMyListBox, CListBox)
CMyListBox::CMyListBox()
{
m_itemHeight = 20;
m_leftOffset = 0;
m_topOffset = 0;
}
CMyListBox::~CMyListBox()
{
}
BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
END_MESSAGE_MAP()
// CMyListBox 消息处理程序
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
//文字范围
CRect txtRt;
m_itemBound = lpDrawItemStruct->rcItem;
txtRt = lpDrawItemStruct->rcItem;
txtRt.left = m_itemBound.left + m_leftOffset;
txtRt.top = m_itemBound.top + m_topOffset;
//颜色设置
COLORREF crOldBkColor = pDC->GetBkColor();
COLORREF crOldTxtColor = pDC->GetTextColor();
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
// 选中状态
pDC->SetTextColor(RGB(222,55,55));
pDC->FillSolidRect(&m_itemBound,RGB(182,218,243));
}
else
{
// 未选中状态
pDC->SetTextColor(crOldTxtColor);
pDC->FillSolidRect(&m_itemBound, crOldBkColor);
}
//显示文字
const TCHAR* str = ((TCHAR*)GetItemDataPtr(lpDrawItemStruct->itemID));
pDC->DrawText(str,strlen(str),&txtRt,DT_VCENTER|DT_SINGLELINE);
//pDC->SetTextColor(crOldTxtColor);
// TODO: 添加您的代码以绘制指定项
}
void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = m_itemHeight;
// TODO: 添加您的代码以确定指定项的大小
}
void CMyListBox::AddItem(TCHAR* pItem)
{
if(*pItem == '\0') return;
int index = AddString(pItem);
SetItemDataPtr(index,pItem);
}
void CMyListBox::SetEveryItemHeight(int hight)
{
if(hight < 0 || hight > 1000) return;
m_itemHeight = hight;
}
void CMyListBox::SetLeftTop(int nleft,int ntop)
{
m_leftOffset = nleft;
m_topOffset = ntop;
}
void CMyListBox::GetMySelText(CString& strTxt)
{
GetText(GetCurSel(),strTxt);
}
#pragma once
// CMyListBox
class CMyListBox : public CListBox
{
DECLARE_DYNAMIC(CMyListBox)
public:
CMyListBox();
virtual ~CMyListBox();
protected:
DECLARE_MESSAGE_MAP()
int m_itemHeight;
CRect m_itemBound;
int m_leftOffset;
int m_topOffset;
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
void AddItem(TCHAR* pItem);
void SetEveryItemHeight(int hight);
void SetLeftTop(int nleft,int ntop);
void GetMySelText(CString& strTxt);
};
//========================================#include "CMyListBox.cpp"=========================
// CMyListBox.cpp : 实现文件
//
#include "stdafx.h"
#include "CListBoxReDraw.h"
#include "CMyListBox.h"
// CMyListBox
IMPLEMENT_DYNAMIC(CMyListBox, CListBox)
CMyListBox::CMyListBox()
{
m_itemHeight = 20;
m_leftOffset = 0;
m_topOffset = 0;
}
CMyListBox::~CMyListBox()
{
}
BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
END_MESSAGE_MAP()
// CMyListBox 消息处理程序
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
//文字范围
CRect txtRt;
m_itemBound = lpDrawItemStruct->rcItem;
txtRt = lpDrawItemStruct->rcItem;
txtRt.left = m_itemBound.left + m_leftOffset;
txtRt.top = m_itemBound.top + m_topOffset;
//颜色设置
COLORREF crOldBkColor = pDC->GetBkColor();
COLORREF crOldTxtColor = pDC->GetTextColor();
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
// 选中状态
pDC->SetTextColor(RGB(222,55,55));
pDC->FillSolidRect(&m_itemBound,RGB(182,218,243));
}
else
{
// 未选中状态
pDC->SetTextColor(crOldTxtColor);
pDC->FillSolidRect(&m_itemBound, crOldBkColor);
}
//显示文字
const TCHAR* str = ((TCHAR*)GetItemDataPtr(lpDrawItemStruct->itemID));
pDC->DrawText(str,strlen(str),&txtRt,DT_VCENTER|DT_SINGLELINE);
//pDC->SetTextColor(crOldTxtColor);
// TODO: 添加您的代码以绘制指定项
}
void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = m_itemHeight;
// TODO: 添加您的代码以确定指定项的大小
}
void CMyListBox::AddItem(TCHAR* pItem)
{
if(*pItem == '\0') return;
int index = AddString(pItem);
SetItemDataPtr(index,pItem);
}
void CMyListBox::SetEveryItemHeight(int hight)
{
if(hight < 0 || hight > 1000) return;
m_itemHeight = hight;
}
void CMyListBox::SetLeftTop(int nleft,int ntop)
{
m_leftOffset = nleft;
m_topOffset = ntop;
}
void CMyListBox::GetMySelText(CString& strTxt)
{
GetText(GetCurSel(),strTxt);
}
大部分转载 小部分自写