C ++双缓冲区和内存

时间:2010-08-17 20:28:10

标签: c++ visual-c++ winapi gdi+

好的,所以我的双缓冲区工作正常,但似乎它使用了大量的内存。

我知道双缓冲区应该存储我正在绘制的椭圆的副本,而不是在屏幕上绘制它,但之后它会删除副本并制作新的副本,但它似乎不会删除它 这是我的代码

hdc=GetDC(hWnd);
HDC memDC=CreateCompatibleDC(hdc);
HBITMAP hMemMap=CreateCompatibleBitmap(hdc, 300, 300);
HBITMAP hOldMap=(HBITMAP)SelectObject(memDC, hMemMap);

// Drawing
Graphics draw(memDC);
draw.FillRectangle(&green, 0, 0, 300, 25);
Font font(&fontfamily, 14, 0, UnitPixel);
PointF p1(180.0f, 4.0f);
PointF p2(30.0f, 4.0f);
draw.DrawString(level, -1, &font, p2, &blue);
draw.DrawString(str, -1, &font, p1, &blue);
draw.FillEllipse(&red, ball1.Ex, ball1.Ey, 25, 25);
draw.FillEllipse(&red, ball2.Ex, ball2.Ey, 25, 25);
draw.FillEllipse(&red, ball3.Ex, ball3.Ey, 25, 25);
draw.FillEllipse(&red, ball4.Ex, ball4.Ey, 25, 25);
draw.FillEllipse(&red, ball5.Ex, ball5.Ey, 25, 25);
draw.FillEllipse(&red, ball6.Ex, ball6.Ey, 25, 25);
draw.FillEllipse(&red, ball7.Ex, ball7.Ey, 25, 25);
draw.FillEllipse(&red, ball8.Ex, ball8.Ey, 25, 25);
draw.FillEllipse(&red, ball9.Ex, ball9.Ey, 25, 25);
draw.FillEllipse(&red, ball10.Ex, ball10.Ey, 25, 25);
BitBlt(hdc, 0, 0, 300, 300, memDC, 0, 0, SRCCOPY);
SelectObject(memDC, hOldMap);
DeleteObject(hMemMap);
DeleteDC(memDC);

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你正在摧毁所有物体,但不是DC。您必须在绘图后调用ReleaseDC。

请参阅MSDN

  

使用通用设备上下文绘制后,必须调用ReleaseDC函数以释放设备上下文。