我正在使用 win32 制作 2D 动画。到目前为止,我的程序加载了从资源创建的 HBITMAP 对象数组。在下面的代码中从“OnUpdate()”调用 CreateCompatibleDC() 时,动画期间会出现问题。在多次调用 OnUpdate 函数后,未创建 HDC 对象(可能未在内存中分配)。当调用 DeleteDC() 删除 HDC 对象时,这会导致意外结果。以下是 main.cpp 中的更新函数代码:
void OnUpdate(
HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd,&ps);
if(!hdc)
{
MessageBox(NULL, L"Failed to Create Compatible DC - 'hdc' in OnUpdate()", L"ALERT", MB_OK);
PostMessage(hwnd, WM_DESTROY, NULL, NULL);
}
HPALETTE hpalT = SelectPalette(hdc,hpal,FALSE);
BITMAP bm;
HDC hdcMem = CreateCompatibleDC(hdc);
if(!hdcMem)
{
MessageBox(NULL, L"Failed to CreateCompatibleDC - 'hdcMem' in OnUpdate()", L"ALERT", MB_OK);
PostMessage(hwnd, WM_DESTROY, NULL, NULL);
}
SelectBitmap(hdcMem, bkgMain);
GetObject(bkgMain, sizeof(bm), &bm);
BitBlt(backDC, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
// Clean up.
if(!DeleteDC(hdcMem))
{
MessageBox(NULL, L"Failed to DeleteDC - 'hdcMem' in OnUpdate()", L"ALERT", MB_OK);
PostMessage(hwnd, WM_DESTROY, NULL, NULL);
}
SelectPalette(hdc,hpalT,FALSE);
EndPaint(hwnd,&ps);
}