代码简写
MyListBox.h
class CUseListBox : public CListBox
{
typedef struct _ListBox_Data
{
CString strAppend; //第二排附加数据
LPTSTR hIcon; //图标
_ListBox_Data()
{
strAppend = _T("");
hIcon = NULL;
}
} List_AppendData;
public: virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/); virtual void MeasureItem(LPMEASUREITEMSTRUCT /*lpMeasureItemStruct*/);
void InsertStr(CString str,int iIndex,CString strAppend,LPTSTR icon);
afx_msg void OnDestroy(); afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
}
MyListBox.cpp
BEGIN_MESSAGE_MAP(CUseListBox, CListBox)
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
void CUseListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (GetCount() == )
{
return;
}
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
List_AppendData * pData =(List_AppendData *) GetItemDataPtr(lpDrawItemStruct->itemID);
//获取列表数据
CString str;
GetText(lpDrawItemStruct->itemID,str);
if (lpDrawItemStruct->itemAction | ODA\_SELECT && lpDrawItemStruct->itemState & ODS\_SELECTED)
{
CBrush t\_brush1;
t\_brush1.CreateSolidBrush(RGB(,,));
dc.FillRect(&lpDrawItemStruct->rcItem,&t\_brush1);
}
else
{
CBrush t\_brush1;
t\_brush1.CreateSolidBrush(RGB(,,));
dc.FillRect(&lpDrawItemStruct->rcItem,&t\_brush1);
}
//画图标
HICON t\_hIcon;
t\_hIcon = LoadIcon(AfxGetInstanceHandle(),pData->hIcon);
DrawIcon(dc.m\_hDC,,lpDrawItemStruct->rcItem.top+,t\_hIcon);
//第一排字体
HFONT t\_hFont= CreateFont(,,,,,,,,,,,,FF\_MODERN,\_T("宋体"));
dc.SelectObject(t\_hFont);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(,,));
dc.TextOut(,lpDrawItemStruct->rcItem.top+,str,str.GetLength());
//第二排字体
HFONT t\_hFont1= CreateFont(,,,,,,,,,,,,FF\_MODERN,\_T("宋体"));
dc.SelectObject(t\_hFont1);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(,,));
str = pData->strAppend;
dc.TextOut(,lpDrawItemStruct->rcItem.top+,str,str.GetLength());
//底部边线
CPen t\_pen(PS\_SOLID,,RGB(,,));
dc.SelectObject(t\_pen);
dc.MoveTo(,lpDrawItemStruct->rcItem.bottom-);
dc.LineTo(lpDrawItemStruct->rcItem.right,lpDrawItemStruct->rcItem.bottom-);
dc.Detach();
}
void CUseListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: 添加您的代码以确定指定项的大小
lpMeasureItemStruct->itemHeight = ;
}
void CUseListBox::InsertStr(CString str,int iIndex,CString strAppend,LPTSTR icon)
{
List_AppendData * pData = new List_AppendData;
pData->hIcon = icon;
pData->strAppend = strAppend;
int i = SetItemDataPtr(InsertString(iIndex,str),pData);
}
void CUseListBox::OnDestroy()
{
CListBox::OnDestroy();
// TODO: 在此处添加消息处理程序代码
int iCount = GetCount();
for (int i=; i<iCount; i++)
{
List\_AppendData \* pData = (List\_AppendData \*)GetItemDataPtr(i);
delete pData;
pData = NULL;
}
}
BOOL CUseListBox::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
return CListBox::OnEraseBkgnd(pDC);
}
void CUseListBox::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CListBox::OnMouseMove(nFlags, point);
}
调用直接InsertStr就行
我写的加载图片
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CBitmap bmp, \*pOldBmp;
HBITMAP bitmap;
CDC memDC;
memDC.CreateCompatibleDC(pDC);
if (pCell)
{
CRect rect;
GetCellRect(pCell->dwRow, pCell->dwCol, rect);
if (!pCell->FilePathName ==NULL )
{
BITMAP bm;
HBITMAP hBitmap = (HBITMAP)::LoadImage(
AfxGetInstanceHandle(),
pCell->FilePathName,
IMAGE\_BITMAP,
, ,
LR\_DEFAULTCOLOR | LR\_CREATEDIBSECTION | LR\_LOADFROMFILE);
bmp.DeleteObject();
bmp.Attach(hBitmap);
bmp.GetObject(sizeof(BITMAP), &bm);
memDC.CreateCompatibleDC(pDC);
pOldBmp =memDC.SelectObject(&bmp);
int offH = rect.Height() - bm.bmHeight;
int offW = rect.Width() - bm.bmWidth;
if (offH>)
{
rect.top += offH / ;
rect.bottom -= (offH - offH / );
}
if (offW>)
{
rect.left += offW / ;
rect.right -= (offW - offW / );
}
//显示
pDC->StretchBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, , , \\
bm.bmWidth, bm.bmHeight, SRCCOPY);
//Clean
memDC.SelectObject(pOldBmp);
bmp.DeleteObject();
}
}
//Clean
memDC.DeleteDC();
做了一下位置调整一般般吧
手机扫一扫
移动阅读更方便
你可能感兴趣的文章