List Control Extended for Progress Control






4.48/5 (20 votes)
Apr 22, 2004

137289

10375
Showing progress bar in any column in a list control.
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.