【发布时间】:2012-02-11 02:55:22
【问题描述】:
用 c++ 在 win32 中做一个项目,试图双缓冲正在绘制的图像,但我得到一个黑屏,上面绘制了正确的位图。这也导致了我的 WM_MOUSEMOVE 条件,它将位图与光标一起拖动以不绘制位图。绘制代码如下:在WM_PAINT下的wndproc中调用paint(),scroll是滚动条的位置,目前未使用。
int paint(HWND hWnd, HINSTANCE hInst, RECT clientRect, std::vector<Measure> *measures, int scroll)
{
int x = 90;
hdc = BeginPaint(hWnd, &ps);
hdcmem = CreateCompatibleDC(hdc);
HBITMAP hbmScreen = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom);
SelectObject(hdcmem,hbmScreen);
/*these functions just create the bitmaps into hdcmem*/
drawStaff(hWnd, hInst, clientRect, x, 0);
drawKey(hWnd, hInst, clientRect, x, (*measures)[0], 0);
drawTime(hWnd, hInst, clientRect, x, (*measures)[0], 0);
drawNotes(hWnd, hInst, clientRect, measures, x);
BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, hdcmem, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdcmem);
return 0;
}
【问题讨论】:
-
您不应该在每次绘制时重新制作后缓冲区,只需在
WM_CREATE(和WM_SIZE)上制作一次,然后使用SetWindowLongPtr(GWLP_USERDATA)将其附加到HWND。 -
您有资源泄漏 - 您忘记在从该函数返回之前调用 hdcmem 上的 DeleteObject。
-
您也未能调用 EndPaint。
标签: c++ winapi bitblt double-buffering