松鼠的博客

导航

统计

Extend List Control with progress control

Introduction

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

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

Create Progress controls using custom draw

If you are not familar with custom draw, you could read this article-http://www.codeproject.com/listctrl/lvcustomdraw.asp, it's a great article about custom draw.

  1. We create a class named CProListCtrl dervied from CListCtrl.
  2. Respond the NM_CUSTOMDRAW in CProListCtrl using notify reflect.
  3. We create and display the controls in the post paint stage of the sub item.
Collapse
void CProListCtrl::OnCustomDraw( NMHDR* pNMHDR, LRESULT* pResult )
{
NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)pNMHDR;
*pResult = CDRF_DODEFAULT;
if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYITEMDRAW;
return;
{
*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;
}
}

Destory Progress controls

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

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.

Collapse
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 dialog based application that convered all I mentioned in this article. You could inspect it in your enviorment.Any suggestions are welcome :)

posted on   Xproer-松鼠  阅读(593)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示