|
贴代码如下. CProgressCtrlEx.cpp类的功能是做一个进度条,替代系统自带的进度条. 在使用的过程中,发现存在内存泄露. 经过测试发现,泄露发生在当OnTimer()定时器进入时, 调用SetPos()函数,重新确定进度条的位置的时候, 需要刷新进度条, 调用Invalidate()函数. 查看系统使用内存时,发现内存不断增加, 频率是5-6秒钟,增加4KB.
求助答案,站内回信,或Email lovefornothing@live.cn
#include "stdafx.h"
#include "resource.h"
#include "ProgressCtrlEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CProgressCtrlEx
CProgressCtrlEx::CProgressCtrlEx()
{
m_nProgressPos=0;
m_penframe.CreatePen(PS_SOLID,1,RGB(0,0,0));
}
CProgressCtrlEx::~CProgressCtrlEx()
{
m_penframe.DeleteObject();
}
BEGIN_MESSAGE_MAP(CProgressCtrlEx, CWnd)
//{{AFX_MSG_MAP(CProgressCtrlEx)
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProgressCtrlEx message handlers
bool CProgressCtrlEx::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInst, ProgressCtrlEx_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = NULL;
wndcls.hbrBackground = NULL;
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = ProgressCtrlEx_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
bool CProgressCtrlEx::CreateCtrl(CWnd *pParent, CRect rcClient, UINT nID)
{
if (!RegisterWindowClass())
return FALSE;
if (!Create(ProgressCtrlEx_CLASSNAME,NULL, WS_CHILD|WS_VISIBLE,CRect(rcClient.left,rcClient.top,rcClient.right,rcClient.bottom), pParent,nID,NULL))
return FALSE;
return TRUE;
}
BOOL CProgressCtrlEx::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CWnd::OnEraseBkgnd(pDC);
CRect RectClient;
GetClientRect(&RectClient);
pDC->FillSolidRect(RectClient,RGB(255,255,255));
CRect rectframe;
rectframe.left=RectClient.left+1;
rectframe.top=RectClient.top+1;
rectframe.right=RectClient.right-1;
rectframe.bottom=RectClient.bottom-1;
CPen* poldpen;
poldpen=pDC->SelectObject(&m_penframe);
pDC->MoveTo(rectframe.TopLeft());
pDC->LineTo(CPoint(rectframe.right,rectframe.top));
pDC->LineTo(CPoint(rectframe.right,rectframe.bottom));
pDC->LineTo(CPoint(rectframe.left,rectframe.bottom));
pDC->LineTo(CPoint(rectframe.left,rectframe.top));
pDC->SelectObject(poldpen);
CRect rectfill;
rectfill.left=rectframe.left+1;
rectfill.top=rectframe.top+1;
rectfill.right=rectframe.right;
rectfill.bottom=rectframe.bottom;
if(m_nProgressPos!=100)
{
double temp1=m_nProgressPos*1.0;
double temp2=temp1/100.0;
int width=int(temp2*rectfill.Width());
rectfill.right=rectfill.left+width;
}
else
{
rectfill.right=rectframe.right;
}
pDC->FillSolidRect(rectfill,RGB(220,40,30));
// penframe.DeleteObject();
return true;
}
void CProgressCtrlEx::SetPos(int nPos)
{
m_nProgressPos=nPos;
Invalidate();
}
|
|