对于LVS_REPORT风格的列表框,重载DrawItem显然很容易解决.

解决方案 »

  1.   

    这是我的一个程序:
    头文件:
    class CGSListBox : public CListBox
    {
    // Construction
    public:
    CGSListBox();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CGSListBox)
    public:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
    //}}AFX_VIRTUAL// Implementation
    public:
    void DrawGSItem(int nIndex, CDC* pDC, CRect& rect, int nType = 0);
    int AddGSData(GSObjData* pGSData);
    virtual ~CGSListBox(); // Generated message map functions
    protected:
    //{{AFX_MSG(CGSListBox)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };实现:
    CGSListBox::CGSListBox()
    {
    }CGSListBox::~CGSListBox()
    {
    }
    BEGIN_MESSAGE_MAP(CGSListBox, CListBox)
    //{{AFX_MSG_MAP(CGSListBox)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()void CGSListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rect = lpDrawItemStruct->rcItem;
    int nIndex = lpDrawItemStruct->itemID;
    // Set the background mode to TRANSPARENT.
    int nOldMode = pDC->SetBkMode(TRANSPARENT);
    BOOL bSel = FALSE;
    if ((lpDrawItemStruct->itemState & ODS_SELECTED))
    // Item has been selected - hilite frame.
    bSel = TRUE;
    if (!(lpDrawItemStruct->itemState & ODS_SELECTED))
    // Item has been unselected.
    bSel = FALSE;
    CBrush br(RGB(50, 50, 220));
    pDC->FillRect(rect, &br);
    DrawGSItem(nIndex, pDC, rect);
    if((lpDrawItemStruct->itemState & ODS_SELECTED))
    pDC->InvertRect(rect);
    // if ((lpDrawItemStruct->itemState & ODS_FOCUS))
    // pDC->DrawFocusRect(rect);
    // Set the background to old mode.
    pDC->SetBkMode(nOldMode);
    }void CGSListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
    {
    lpMeasureItemStruct->itemHeight = 44;
    }int CGSListBox::AddGSData(GSObjData *pGSObjData)
    {
    int nItem = AddString(_T("无"));
    if(nItem != LB_ERR)
    {
    SetItemData(nItem, (DWORD)pGSObjData);
    return nItem;
    }
    return 0;
    }void CGSListBox::DrawGSItem(int nIndex, CDC *pDC, CRect &rect, int nType)
    {
    if(nIndex < 0 || nIndex >= GetCount())
    return ;
    GSObjData* pGSObjData = (GSObjData*)GetItemData(nIndex);
    ASSERT(pGSObjData);
    int nLeft = 4;
    int nLength = 46;

    CString strIndex, strCode, strName, strStart, strEnd, strPer, strTotal; strIndex.Format("%03d", nIndex + 1);
    strCode = pGSObjData->m_strGSCode;
    strName = pGSObjData->m_strGSName;
    strStart = pGSObjData->m_strGSStartData;
    strEnd = pGSObjData->m_strGSEndData;
    strPer = pGSObjData->m_strGSPercentData;
    strTotal = pGSObjData->m_strGSTotalData; CFont newFont;
    newFont.CreateFont(22, 0, 0, 0, 700 , 0, 0, 0, 
    ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
    PROOF_QUALITY, VARIABLE_PITCH | FF_DONTCARE, "Fixedsys");
    CFont* oldFont = pDC->SelectObject(&newFont); pDC->SetTextColor(RGB(255, 255, 255));
    pDC->DrawText(strIndex, 
    CRect(rect.left + nLeft, rect.top, rect.left + nLength, rect.bottom), 
    DT_LEFT | DT_VCENTER | DT_SINGLELINE);
    nLeft = 50;
    nLength = 80;
    pDC->SetTextColor(RGB(255, 255, 0));
    pDC->DrawText(strCode, 
    CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), 
    DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    nLeft = 130;
    nLength = 90;
    pDC->SetTextColor(RGB(255, 255, 180));
    pDC->DrawText(strName, 
    CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), 
    DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    nLeft = 220;
    nLength = 80;
    if(pGSObjData->m_bIsStop)
    {
    pDC->SetTextColor(RGB(255, 255, 255));
    }
    else
    {
    if(GSObjData::StringToDouble(strEnd) < GSObjData::StringToDouble(strStart))
    pDC->SetTextColor(RGB(255, 0, 0));
    else if(GSObjData::StringToDouble(strEnd) > GSObjData::StringToDouble(strStart))
    pDC->SetTextColor(RGB(0, 255, 0));
    else
    pDC->SetTextColor(RGB(255, 255, 255));
    }
    pDC->DrawText(strEnd, 
    CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), 
    DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
    nLeft = 300;
    nLength = 80;
    pDC->DrawText(strStart, 
    CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), 
    DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
    nLeft = 380;
    nLength = 80;
    pDC->DrawText(strPer, 
    CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), 
    DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
    nLeft = 460;
    nLength = 120;
    pDC->DrawText(strTotal, 
    CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom), 
    DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
    rect.bottom -= 2;
    CPen newPen1(PS_SOLID, 1, RGB(128, 128, 128));
    CPen newPen2(PS_SOLID, 1, RGB(192, 192, 192));
    pDC->SelectObject(&newPen2);
    pDC->MoveTo(rect.left, rect.bottom);
    pDC->LineTo(rect.right, rect.bottom);
    pDC->SelectObject(&newPen1);
    rect.bottom += 1;
    pDC->MoveTo(rect.left, rect.bottom);
    pDC->LineTo(rect.right, rect.bottom);
    }CListCtrl要有owner draw风格:Variable可以