我写了一个OCX,可在98下使用时,资源会随操作慢慢地减少,为何?问题出在什么地方???
附原码如下:
//控件画图主函数
void CChartCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
CDC MemDC;
CBitmap MemBit;
MemDC.CreateCompatibleDC(pdc);
MemBit.CreateCompatibleBitmap(pdc,rcBounds.Width(),rcBounds.Height());
CBitmap *pOldBit;
pOldBit = (CBitmap*)MemDC.SelectObject(&MemBit); MemDC.FillSolidRect(0,0,rcBounds.Width(),rcBounds.Height(),TranslateColor(GetBackColor())); clsChart.oldx=-10000;
iCurrentShow=clsChart.DrawTickCurrent(&MemDC,rcBounds,iDrawMode,iCurrentShow);

pdc->BitBlt(0,0,rcBounds.Width(),rcBounds.Height(),&MemDC,0,0,SRCCOPY);//绘图完成后的清理
(CBitmap*)MemDC.SelectObject(pOldBit);
MemBit.DeleteObject();
//DeleteObject(&MemBit);  MemDC.DeleteDC();
//ReleaseDC(&MemDC);
}void CChartCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
InvalidateControl();  //用于测试,即每一次使控件刷新时,系统资源就会减少
}其中:
clsChart.DrawTickCurrent(&MemDC,rcBounds,iDrawMode,iCurrentShow);函数如下:int CChart::DrawTickCurrent(CDC * pdc,CRect rect,int type,int iCurrentShow)
{
if(pTickFirst==NULL)
return 0;
pTickCurrent=pTickShowFirst;
int iStep=(iChart3Down-iChart3Top)/20;
int iStepX=(iChartRight-iChartLeft)/(iTickShowNumber+1);//每一格的大小
if (iStepX<=0)
iStepX=1;
int iShowMax=20;
int iChartWidth=iChartRight-iChartLeft;
while((iShowMax*4)/3*iTickShowNumber>iChartWidth)
{
iShowMax=iShowMax-1;
if (iShowMax<=0)
{
iShowMax=1;
break;
}
}
int iSpace=iChartWidth-iStepX*(iShowNumber+1); int tmp1,tmp2,tmp3=0;
for (int i=1;i<=iTickShowNumber && (i*iStepX+iSpace)<iChartWidth&& i<iChartWidth;i++)
{
if(iSpace>iShowMax && iSpace<iTickShowNumber)
{
tmp1=1;tmp2=0;
iSpace--;
tmp3=i;
}
else
{
tmp1=0;tmp2=tmp3;
}//平均分配空白空间,直到无空白为止 if(i==iCurrentShow)
{
CPen pen3;
pen3.CreatePen(PS_SOLID,1,clrLine3);
CPen *oldPen=(CPen*)pdc->SelectObject(&pen3); pdc->SetROP2(R2_NOT);
pdc->MoveTo (oldx,iChart1Top);
pdc->LineTo(oldx,iChart3Down); oldx=iChartLeft+i*iStepX+tmp1*i+tmp2;
pdc->MoveTo(oldx,iChart1Top);
pdc->LineTo(oldx,iChart3Down);
(CPen*)pdc->SelectObject(oldPen);
pen3.DeleteObject();
DeleteObject(&pen3); CString str,str1;
str1.Format("%0.4f",pTickCurrent->fLatestPrice);
str="Tick: "+str1+" 时间: " +pTickCurrent->chTime+"  " ; CFont font;
font.CreateFont((rect.bottom - rect.top)/(PERCENT_Y+1),(rect.right-rect.left)/(PERCENT_X+20),
0,0,400,FALSE,FALSE,0,ANSI_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"宋体");
CFont *oldFont=(CFont*)pdc->SelectObject(&font);
pdc->SetTextColor(clrLine1);
pdc->SetBkColor(clrBk);
pdc->TextOut(iChartLeft+5,rect.top+rect.Height()/PERCENT_Y,str);
(CFont*)pdc->SelectObject(oldFont);
font.DeleteObject();
DeleteObject(&font); return iCurrentShow;
}
else
{
pTickCurrent=pTickCurrent->pNext;
}
}
if (iCurrentShow<1)
return 0;
if (iCurrentShow>iTickShowNumber)
return iTickShowNumber+1; return 0;
}我自己以为问题出在OnDraw()函数里,但不知是哪里不对,请指教!!!

解决方案 »

  1.   

    pen3.DeleteObject();
    DeleteObject(&pen3); //??
    font.DeleteObject();
    DeleteObject(&font); //??
    _____________
    你把DrawTickCurrent这段注释掉,看看还有没有问题.慢慢找.
      

  2.   

    to luxm我以为是GDI设备的问题,可能是我没处理好。在98下,控件工程怎么调试呢?
      

  3.   

    GetDC取得的HDC用ReleaseDC清除
    CreateCompatibleDC取得的HDC用DeleteObject清除
    非常非常重要!!!否则会产生不可预料的错误
    典型表现:有时正常,有时错误,内存不断减少
    你在这方面仔细检查,肯定能发现问题
      

  4.   

    void CChartCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
         #ifdef _DEBUG
             CMemoryState oldMemState, newMemState, diffMemState;
             oldMemState.Checkpoint();
         #endif     CDC MemDC;
         CBitmap MemBit;     .......      #ifdef _DEBUG
    newMemState.Checkpoint();
    if( diffMemState.Difference( oldMemState, newMemState ) )
    {
        AfxMessageBox( "问题的确出在这里! " );
    }
         #endif
    }
      

  5.   

    to everandforever
    那说的那两行可以注释掉,因为我怕前面那种方法释放不干净,无影响。
      

  6.   

    to mjk
    你说的“CreateCompatibleDC取得的HDC用DeleteObject清除”的这句,我不是很清楚,DeleteObject是用来清除HGDIOBJ的,而DeleteDC或者ReleaseDC是用来清除HDC的,而CreateCompatibleDC是用来生成HDC的,怎么用DeleteObject来清除呢?不知道我说的对不对,请指教!!!