| 发表于:2007-06-17 16:29:04 楼主 |
下面是一个钟表程序的一部分,模拟秒针旋转所以要旋转位图。在ontimer里有内存泄露,ontimer每秒执行一次,能看到内存明显增长。可能是旋转位图引起的,请各位大虾看看如何改正。thanks。 //几个成员变量: cdc m_dcsecpointer;//the second pointer cpoint m_clockcenter;//the clock pointer center cdc m_dcbkground;//the back ground //ontimer函数,里面调用了rotatebitmap()函数 void cclockdlg::ontimer(uint nidevent) { ctime time=ctime::getcurrenttime(); int nsec=time.getsecond(); float fangle; cpoint points; hdc dcs; int nws=sec_width; int nhs=sec_height; switch(nidevent) { case main_timer: fangle=nsec*6*pi/180; points.y=-sec_height*cos(fangle)/2; points.x=sec_height*sin(fangle)/2; points=points+m_clockcenter; dcs=rotatebitmap(m_dcsecpointer,nws,nhs,points,fangle); getdc()-> stretchblt(0,0,m_clockcenter.x*2,2*m_clockcenter.y,&m_dcbkground,0,0,m_clockcenter.x*2,2*m_clockcenter.y,srccopy); transparentblt(getdc()-> m_hdc,points.x,points.y,nws,nhs,dcs,0,0,nws,nhs,rgb(0,0,0)); deletedc(dcs); break; } cdialog::ontimer(nidevent); } //旋转位图的函数 hdc cclockdlg::rotatebitmap( hdc dcsrc,//source bitmap int &srcwidth, //[in]width of source bitmap //[out]width of the rotated bitmap int &srcheight, //[in]height of source bitmap //[out]height of the rotated bitmap cpoint &cpcenter, //[in]the rotated bitmap 's center wanted be shown //[out]the rotated bitmap 's left top corner double dangle//angle of rotation and y axis ) {//return value:rotated bitmap is temporarily stored here double x1,x2,x3,y1,y2,y3; double maxwidth,maxheight,minwidth,minheight; double srcx,srcy; double sina,cosa; double dstwidth,dstheight; hdc tmpdcdst;//temp dc for rotate hbitmap newbitmap; sina = sin(dangle); cosa = cos(dangle); x1 = -srcheight * sina; y1 = srcheight * cosa; x2 = srcwidth * cosa - srcheight * sina; y2 = srcheight * cosa + srcwidth * sina; x3 = srcwidth * cosa; y3 = srcwidth * sina; minwidth = x3> (x1> x2?x2:x1)?(x1> x2?x2:x1):x3; minwidth = minwidth> 0?0:minwidth; minheight = y3> (y1> y2?y2:y1)?(y1> y2?y2:y1):y3; minheight = minheight> 0?0:minheight; maxwidth = x3> (x1> x2?x1:x2)?x3:(x1> x2?x1:x2); maxwidth = maxwidth> 0?maxwidth:0; maxheight = y3> (y1> y2?y1:y2)?y3:(y1> y2?y1:y2); maxheight = maxheight> 0?maxheight:0; dstwidth = maxwidth - minwidth; dstheight = maxheight - minheight; //prepare a temp empty bitmap for drawing tmpdcdst = createcompatibledc(dcsrc); newbitmap = createcompatiblebitmap(dcsrc,(int)dstwidth,(int)dstheight); selectobject(tmpdcdst,newbitmap); //copy to the temp bitmap from source bitmap for( int i = 0 ;i <dstheight;i++){ for(int j = 0 ;j <dstwidth;j++){ srcx = (j + minwidth) * cosa + (i + minheight) * sina; srcy = (i + minheight) * cosa - (j + minwidth) * sina; if( (srcx > = 0) && (srcx <= srcwidth) &&(srcy > = 0) && (srcy <= srcheight)) { bitblt(tmpdcdst, j, i, 1, 1, dcsrc,(int)srcx, (int)srcy, srcinvert); } } } deleteobject(newbitmap); //return values srcwidth=dstwidth; srcheight=dstheight; cpcenter.x=cpcenter.x-dstwidth/2; cpcenter.y=cpcenter.y-dstheight/2; return tmpdcdst; } |
|
|
|
|