ListCtrl中嵌入进度条

VC中在listctrl中嵌入进度条,截图如下:

其实要实现这个非常容易,以下是自绘ListCtrl的代码,首先继承CListCtrl
然后增加函数OnCustomDraw
void CProcessList::OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult)
{
 //draw each item.set txt color,bkcolor....
 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
 
 // Take the default processing unless we set this to something else below.
 *pResult = CDRF_DODEFAULT;
 
 // First thing - check the draw stage. If it’s the control’s prepaint
 // stage, then tell Windows we want messages for every item.
 
 if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
 {
  *pResult = CDRF_NOTIFYITEMDRAW;
 }
 else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
 {
  // This is the notification message for an item.  We’ll request
  // notifications before each subitem’s prepaint stage.
  
  *pResult = CDRF_NOTIFYSUBITEMDRAW;
 }
 else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
 {
  // This is the prepaint stage for a subitem. Here’s where we set the
  // item’s text and background colors. Our return value will tell
  // Windows to draw the subitem itself, but it will use the new colors
  // we set here.
  
  int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
  int nSubItem = pLVCD->iSubItem;
  
  if(nSubItem != 2)//
这里我只重绘第二列
   return;

  COLORREF crText  = ::GetSysColor(COLOR_WINDOWFRAME);
  COLORREF crBkgnd = ::GetSysColor(COLOR_WINDOW);
  
  CDC* pDC = CDC::FromHandle(pLVCD->nmcd.hdc);
  CRect rect;
  GetSubItemRect(nItem, nSubItem, LVIR_BOUNDS, rect);
  if (GetItemState(nItem, LVIS_SELECTED))
   DrawText(nItem, nSubItem, pDC, ::GetSysColor(COLOR_HIGHLIGHT),
   ::GetSysColor(COLOR_HIGHLIGHT), rect);
  else
   DrawText(nItem, nSubItem, pDC, crText, crBkgnd, rect);

  *pResult = CDRF_SKIPDEFAULT; // We’ve painted everything.
 }
}

然后为该函数增加消息映射(#add其实是消息反射)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)

最后我们为画进度条而努力,这里程序中把进度存在ItemData中。
void CProcessList::DrawText(int nItem,
        int nSubItem,
        CDC *pDC,
        COLORREF crText,
        COLORREF crBkgnd,
        CRect &rect)
{
 ASSERT(pDC);
 pDC->FillSolidRect(&rect, crBkgnd);
 
 int nProcess = GetItemData(nItem);
 CRect procRect = rect;
 pDC->Rectangle(procRect);

 procRect.left += 1;
 procRect.bottom -= 1;
 procRect.top += 1;
 procRect.right = procRect.left + rect.Width() * nProcess / 100;
 CBrush brush(RGB(255,0,0));
 pDC->FillRect(&procRect, &brush);
 
 CString str;
 str.Format("%d%%", nProcess);
 
 if (!str.IsEmpty())
 {
  UINT nFormat = DT_VCENTER | DT_SINGLELINE | DT_CENTER;
  
  pDC->SetBkMode(TRANSPARENT);
  pDC->SetTextColor(crText);
  pDC->SetBkColor(crBkgnd);
  pDC->DrawText(str, &rect, nFormat);
 }
}

 

 

上面用到一个结构,是存放于ItemData中用来表示进度和步长的:
typedef struct _ProcessLCI_Data_
{
            int
 nProportion;
          
int nStep;
} PLCIDATA, *LPPLCIDATA;

接下来就是在容器中使用继承的ListCtrl了,这里使用对话框,在一个定时器中模拟进度的变化(其它的初始化和清除就不罗嗦了):

void CListCtrlProcessDlg::OnTimer(UINT nIDEvent)
{
      if(nIDEvent == TIMER_PROCESS)
      {
                    BOOL bPaint = FALSE;
         for(int i = 0; i < m_listProcess.GetItemCount(); ++i)
          {
                     LPPLCIDATA pIData = (LPPLCIDATA)m_listProcess.GetItemData(i);
                     if(pIData->nProportion >= 100)
                              continue;
                     bPaint = TRUE;
                     if(pIData->nProportion + pIData->nStep > 100)
                     {
                               pIData->nProportion = 100;
                     }
                     else
                     {
                               pIData->nProportion += pIData->nStep;
                     }
           }
           if(bPaint)
                     
m_listProcess.Invalidate(FALSE);
     }
     CDialog::OnTimer(nIDEvent);
}

   这里有个奇怪的问题,在VC6中不调上面的Invalidate(),只要TIMMER发生,进度条都会自动刷新。但在VC71上编译,如果不调Invalidate(),根本不刷新???不解!!!
  
从运行的结果看,效果还不错。VC6编译的就是满100%后进度条还是不停的闪烁;而VC71就不会了,因为没有调Invalidate()???


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
VC ListCtrl是MFC的一种控件,用于显示数据列表。如果需要在ListCtrl嵌入进度条,可以通过以下步骤实现: 1. 首先,在ListCtrl的列头添加一个进度条列。可以使用InsertColumn函数指定插入的列的标题和宽度。 2. 然后,通过SetItem函数为每一行数据的进度条插入一条数据。可以使用下述代码示例: ```cpp int nItem = pListCtrl->GetItemCount(); pListCtrl->InsertItem(nItem, _T("")); CProgressCtrl* pProgress = new CProgressCtrl; pProgress->Create(WS_CHILD | WS_VISIBLE, CRect(0, 0, 100, 20), pListCtrl, 0); pListCtrl->SetItemData(nItem, (DWORD_PTR)pProgress); pListCtrl->SetItem(nItem, nColumnIndex, LVIF_PARAM, NULL, 0, 0, (LPARAM)pProgress); ``` 3. 接下来,需要在绘制ListCtrl的每一行时,将进度条绘制出来。可以使用下述代码示例: ```cpp void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); CRect rcItem(lpDrawItemStruct->rcItem); int nColumnCount = GetHeaderCtrl()->GetItemCount(); // 绘制进度条 if (lpDrawItemStruct->itemData) { CRect rcProgress(rcItem.left, rcItem.top, rcItem.right, rcItem.bottom); CProgressCtrl* pProgress = (CProgressCtrl*)lpDrawItemStruct->itemData; pProgress->MoveWindow(rcProgress); pProgress->SetWindowPos(NULL, rcProgress.left, rcProgress.top, rcProgress.Width(), rcProgress.Height(), SWP_NOZORDER); } // 绘制其他单元格内容 // 绘制选择框 // 绘制焦点框 } ``` 4. 最后,在ListCtrl更新进度条的值。可以使用下述代码示例: ```cpp CProgressCtrl* pProgress = (CProgressCtrl*)pListCtrl->GetItemData(nItem); pProgress->SetRange(0, 100); pProgress->SetPos(nProgressValue); ``` 通过以上步骤,就可以在VC ListCtrl嵌入进度条,并更新和显示相应的进度值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值