Click here to Skip to main content
15,868,164 members
Articles / Desktop Programming / MFC

Extend List Control with Progress Control

Rate me:
Please Sign up or sign in to vote.
4.70/5 (12 votes)
27 Mar 2006CPOL1 min read 67.4K   3.5K   62   2
A better method to embed the progress control in list control using custom draw
Sample Image - ProgressLVDemo.jpg

Introduction

We want to add a progress control or other controls in the list control sometimes. Traditionally, we could draw these controls using CDC and deal with all messages related to the controls, but it's fairly complex.

This article discusses a method which embeds controls to the list control directly, so we need not draw it, and can manipulate it using predefined MFC classes.

Create Progress Controls Using Custom Draw

If you are not familiar with custom draw, you could read this article. It's a great article about custom draw.

  1. We create a class named CProListCtrl derived from CListCtrl.
  2. Respond to the NM_CUSTOMDRAW in CProListCtrl using notify reflect.
  3. We create and display the controls in the post paint stage of the sub item.
C++
void CProListCtrl::OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult )
{
    NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)pNMHDR;

    *pResult = CDRF_DODEFAULT;

    if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
    {
        *pResult = CDRF_NOTIFYITEMDRAW;
        return;
    }else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
    {
        *pResult = CDRF_NOTIFYSUBITEMDRAW;
        return;
    }else if ( (CDDS_SUBITEM | CDDS_ITEMPREPAINT) == pLVCD->nmcd.dwDrawStage )
    {
        *pResult = CDRF_NOTIFYPOSTPAINT;
        return;
    }else if ( (CDDS_SUBITEM | CDDS_ITEMPOSTPAINT) == pLVCD->nmcd.dwDrawStage )
    {
        int nItem = pLVCD->nmcd.dwItemSpec;
        int nSubItem = pLVCD->iSubItem;
        if (1 != nSubItem)
            return;
        
        CRect rcSubItem;
        this->GetSubItemRect(nItem, nSubItem, LVIR_BOUNDS, rcSubItem);
        
        CProgressCtrl* pCtrl = (CProgressCtrl*)this->GetItemData(nItem);
        if (NULL == pCtrl)
        {
            pCtrl = new CProgressCtrl;
            if (rcSubItem.Width() > 100)
                rcSubItem.right = rcSubItem.left + 100;

            pCtrl->Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH, rcSubItem, 
                               this, 0x1000 + nItem);
            ASSERT(pCtrl->GetSafeHwnd());
            pCtrl->SetPos( nItem*10 % 100 );
            this->SetItemData(nItem, (DWORD)pCtrl);
        }
            
        if (rcSubItem.Width() > 100)
            rcSubItem.right = rcSubItem.left + 100;
        pCtrl->MoveWindow(rcSubItem);
        pCtrl->ShowWindow(SW_SHOW);
        *pResult = CDRF_SKIPDEFAULT;
        return;
    }
}

Destroy Progress Controls

We should destroy the controls we had created to avoid the memory leak and resource leak. We could do it when list control is destroying.

C++
void CProListCtrl::OnDestroy()
{
    int nCount = this->GetItemCount();
    CProgressCtrl* pCtrl;
    for(int i = 0; i < nCount; i++)
    {
        pCtrl = (CProgressCtrl*)this->GetItemData(i);
        if (NULL != pCtrl)
            delete pCtrl;
        this->SetItemData(i, 0);
    }
}

Redraw and Move the Progress Controls

When the list control is scrolled, or the head item is changed, we must redraw and move the progress controls.

C++
void CProListCtrl::InvalidateProgressCtrls()
{
    int nFirst = GetTopIndex();
    int nLast = nFirst + GetCountPerPage();

    //Hide the other items.
    int nCount = this->GetItemCount();
    CProgressCtrl* pCtrl;
    for(int i = 0; i < nFirst; i++)
    {
        pCtrl = (CProgressCtrl*)this->GetItemData(i);
        if (NULL != pCtrl)
            pCtrl->ShowWindow(SW_HIDE);        
    }
    for(i = nLast; i < nCount; i++)
    {
        pCtrl = (CProgressCtrl*)this->GetItemData(i);
        if (NULL != pCtrl)
            pCtrl->ShowWindow(SW_HIDE);        
    }

    //Invalidate
    CRect rc(0,0,0,0);
    CRect rcSubItem;
    for(; nFirst < nLast; nFirst++)
    {
        GetSubItemRect(nFirst, 1, LVIR_BOUNDS, rcSubItem);
        VERIFY( rc.UnionRect(rc, rcSubItem) );
    }

    InvalidateRect(rc);
}

Demo Project

It's a dialog based application that covered all I mentioned in this article. You could inspect it in your environment. Any suggestions are welcome.

History

  • 27th March, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldon't forget on Mouse Wheel Pin
as_sound_as23-Aug-07 2:17
as_sound_as23-Aug-07 2:17 
Questionadding more controls? Pin
SnowMiser123413-Jun-06 15:27
SnowMiser123413-Jun-06 15:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.