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

List Control Extended for Progress Control

Rate me:
Please Sign up or sign in to vote.
4.48/5 (22 votes)
21 Apr 2004 135.2K   10.3K   101   12
Showing progress bar in any column in a list control.

Sample Image - ProgressListControl.jpg

Introduction

I don’t now how many times I wanted to be able to show a progress bar in the list control. The list control is a versatile control and it is really neat when anybody wants to display some data in tabular form. But it does not come with a built in progress bar.

So I tried to use the owner drawn and custom drawn controls. But they could not match the look and feel of the Windows default progress bar.

So I decided to make the progress control a child of the control and set the progress position to the numeric text of the respective box.

Code

The trick was to capture the WM_PAINT message and then to create a series of progress controls in the right place.

void CListCtrlEx::OnPaint()
{
 // TODO: Add your message handler code here
 // Do not call CListCtrl::OnPaint() for painting messages
 int Top=GetTopIndex();
 int Total=GetItemCount();
 int PerPage=GetCountPerPage();
 int LastItem=((Top+PerPage)>Total)?Total:Top+PerPage;
 // if the count in the list os nut zero
 // delete all the progress controls and them procede
 {
  int Count=(int)m_ProgressList.GetCount();
  for(int i=0;i<Count;i++)
  {
   CProgressCtrl* pControl=m_ProgressList.GetAt(0);
   pControl->DestroyWindow();
   m_ProgressList.RemoveAt(0);
  }
 }
 CHeaderCtrl* pHeader=GetHeaderCtrl();
 for(int i=Top;i<LastItem;i++)
 {
  CRect ColRt;
  pHeader->GetItemRect(m_ProgressColumn,&ColRt);
  // get the rect
  CRect rt;
  GetItemRect(i,&rt,LVIR_LABEL);
  rt.top+=1;
  rt.bottom-=1;
  rt.left+=ColRt.left;
  int Width=ColRt.Width();
  rt.right=rt.left+Width-4;
 // create the progress control and set their position
  CProgressCtrl* pControl=new CProgressCtrl();
  pControl->Create(NULL,rt,this,IDC_PROGRESS_LIST+i);
  CString Data=GetItemText(i,m_ProgressColumn);
  int Percent=atoi(Data);
  // set the position on the control
  pControl->SetPos(Percent);
  pControl->ShowWindow(SW_SHOWNORMAL);
  // add them to the list
  m_ProgressList.Add(pControl);
 }
 CListCtrl::OnPaint();
}

So the list control paints the data in the correct position, and the progress controls being the child windows, stay on the top of the ListCtrl thus making an illusion of being in the place of normal text.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
India India
This is just a beginning. I’m yet to decide the distance I have to travel.
I’m a 2nd year B. Tech. Student and I’ve started Visual programming about a year ago. Lets hope I can continue that for the rest of 2 and a half more years.

Comments and Discussions

 
QuestionCode License Pin
Member 1063802211-Mar-14 0:04
Member 1063802211-Mar-14 0:04 
Questionbuilding on vs2010 [modified] Pin
steveh21126-Aug-11 3:48
steveh21126-Aug-11 3:48 
Generalmemory leak Pin
haisan17-Jun-11 4:50
haisan17-Jun-11 4:50 
Generalthank you Pin
shint2-May-15 21:17
shint2-May-15 21:17 
Generalthere is some flickering Pin
yinglai22-May-10 16:35
yinglai22-May-10 16:35 
GeneralPort to VC6! Pin
cristitomi6-Apr-07 3:37
cristitomi6-Apr-07 3:37 
Generali believe it's a problem Pin
levelboy8-Jan-07 12:15
levelboy8-Jan-07 12:15 
GeneralThis is great. Pin
KirinBeer1-Feb-05 15:06
KirinBeer1-Feb-05 15:06 
GeneralGreat idea (better with a few modifications) Pin
Jon Woolman21-May-04 21:36
Jon Woolman21-May-04 21:36 
GeneralRe: Great idea (better with a few modifications) Pin
Lars [Large] Werner3-Jun-04 3:43
professionalLars [Large] Werner3-Jun-04 3:43 
GeneralRe: Great idea (better with a few modifications) Pin
Yin Gang9-May-05 16:30
Yin Gang9-May-05 16:30 
GeneralRe: Great idea (better with a few modifications) Pin
fjolnir29-Aug-05 12:38
fjolnir29-Aug-05 12:38 
Actually, the flicker can be eliminated even more with Jon Woolman’s 3rd recommendation. If you are not updating the control text, there will be almost no flicker.

So, just get the percent with GetItemData. In OnPaint replace:

CString strPercent = GetItemText(i, m_ProgressColumn);<br />
int nPercent = atoi(strPercent);


with:

int nPercent = GetItemData(i);

And in OnTimer replace:

CString text;<br />
text.Format("%d",((count+i)%10)*10);<br />
m_ProgressList.SetItemText(i,0,text);


with:

m_ProgressList.SetItemData(i,(DWORD)(((count+i)%10)*10));

Enjoy! Smile | :)

--Mark.

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.