今天发现里一个非常好用的Listbox自绘类,带不同文字字体和图片,觉得很有必要记下来

代码简写

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() == 0)
    {
        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(229,239,244));
        dc.FillRect(&lpDrawItemStruct->rcItem,&t_brush1);

    }
    else
    {
          CBrush t_brush1;
         t_brush1.CreateSolidBrush(RGB(255,255,255));
         dc.FillRect(&lpDrawItemStruct->rcItem,&t_brush1);
    }
    //画图标
    HICON t_hIcon;
    t_hIcon = LoadIcon(AfxGetInstanceHandle(),pData->hIcon);
    DrawIcon(dc.m_hDC,10,lpDrawItemStruct->rcItem.top+15,t_hIcon);
    //第一排字体
    HFONT t_hFont= CreateFont(12,0,0,0,600,0,0,0,1,2,1,0,FF_MODERN,_T("宋体"));
    dc.SelectObject(t_hFont);
    dc.SetBkMode(TRANSPARENT);
    dc.SetTextColor(RGB(46,134,208));
    dc.TextOut(60,lpDrawItemStruct->rcItem.top+18,str,str.GetLength());
    //第二排字体
    HFONT t_hFont1= CreateFont(12,0,0,0,400,0,0,0,1,2,1,0,FF_MODERN,_T("宋体"));
    dc.SelectObject(t_hFont1);
    dc.SetBkMode(TRANSPARENT);
    dc.SetTextColor(RGB(142,142,142));
    str = pData->strAppend;
    dc.TextOut(60,lpDrawItemStruct->rcItem.top+37,str,str.GetLength());
    //底部边线
    CPen t_pen(PS_SOLID,1,RGB(211,218,223));
    dc.SelectObject(t_pen);
    dc.MoveTo(0,lpDrawItemStruct->rcItem.bottom-1);
    dc.LineTo(lpDrawItemStruct->rcItem.right,lpDrawItemStruct->rcItem.bottom-1);



    dc.Detach();
}

void CUseListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{

    // TODO:  添加您的代码以确定指定项的大小
    lpMeasureItemStruct->itemHeight = 63;
}
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=0; 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,
                    160, 90,
                    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>0)
                {
                    rect.top += offH / 2;
                    rect.bottom -= (offH - offH / 2);
                }
                if (offW>0)
                {
                    rect.left += offW / 2;
                    rect.right -= (offW - offW / 2);
                }
                //显示
                pDC->StretchBlt(rect.left, rect.top, rect.Width(), rect.Height(), &memDC, 0, 0, 
                    bm.bmWidth, bm.bmHeight, SRCCOPY);
            
                //Clean
                memDC.SelectObject(pOldBmp);
                bmp.DeleteObject();

            }
        }
        //Clean
        memDC.DeleteDC();


做了一下位置调整一般般吧

代码

赌上我的人生为梦想,即使是臭名远扬,也要我的名字响彻天堂
原文地址:https://www.cnblogs.com/ye-ming/p/7107570.html