一个vc ListCtrl控件中插入进度条类CProcessList

来源:互联网 发布:安川机器人编程软件 编辑:程序博客网 时间:2024/05/09 22:22


类文件下载地址:http://download.csdn.net/detail/u010648606/7332797


头文件:


#if !defined(AFX_PROCESSLIST_H__2BCE79FD_670F_445B_A032_AA8569C8C246__INCLUDED_)#define AFX_PROCESSLIST_H__2BCE79FD_670F_445B_A032_AA8569C8C246__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// ProcessList.h : header file///////////////////////////////////////////////////////////////////////////////// CProcessList windowclass CProcessList : public CListCtrl{// Constructionpublic:CProcessList();void OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult);void DrawText(int nItem, int nSubItem, CDC *pDC, COLORREF crText, COLORREF crBkgnd, CRect &rect);// Attributespublic:// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CProcessList)//}}AFX_VIRTUAL// Implementationpublic:virtual ~CProcessList();// Generated message map functionsprotected://{{AFX_MSG(CProcessList)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSGDECLARE_MESSAGE_MAP()};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_PROCESSLIST_H__2BCE79FD_670F_445B_A032_AA8569C8C246__INCLUDED_)



CPP文件:

// ProcessList.cpp : implementation file//#include "stdafx.h"#include "ProcessList.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CProcessListCProcessList::CProcessList(){}CProcessList::~CProcessList(){}BEGIN_MESSAGE_MAP(CProcessList, CListCtrl)//{{AFX_MSG_MAP(CProcessList)// NOTE - the ClassWizard will add and remove mapping macros here.//}}AFX_MSG_MAPON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CProcessList message handlersvoid CProcessList::DrawText(int nItem, int nSubItem, CDC *pDC, COLORREF crText, COLORREF crBkgnd, CRect &rect){ASSERT(pDC);pDC->FillSolidRect(&rect, crBkgnd);//下面是将进度条中xx%转为数字:比如50%,先将%号去掉,在用atoi转为整数CString strProcess = GetItemText(nItem,nSubItem);strProcess.Replace("%","");int nProcess = atoi(strProcess.GetBuffer(0));CRect procRect = rect;pDC->Rectangle(procRect);procRect.left += 0;procRect.bottom -= 0;procRect.top += 0;procRect.right = procRect.left + rect.Width() * nProcess / 100;CBrush brush(RGB(86,149,225));     //----------------------------------------------->这里是进度条的RGBpDC->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);}}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 != 3)//这里我只重绘第三列        ------------------------------------------------>这里是你需要显示进度条的列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);elseDrawText(nItem, nSubItem, pDC, crText, crBkgnd, rect);*pResult = CDRF_SKIPDEFAULT; // We've painted everything.}}


0 0
原创粉丝点击