VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
VC ListCtrl中嵌入进度条

作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站

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

其实要实现这个非常容易,以下是自绘ListCtrl的代码,首先继承CListCtrl,
然后增加函数:
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.
 }
}

然后为该函数增加消息映射:
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);
 }
}

怎么样?很简单吧,其实使用VC开发界面也非常迅速的,虽然还是觉得MFC的封装有JAVA界面库封装得那么好该多好啊……




相关文章

相关软件