问题遇到的现象和发生背景
我要设计一个游戏, 包含背景, 玩家和怪物, 所以需要多个贴图, 用vs创建的Windows窗口应用程序代码, 如果对同一个hdc绘制多次, 看起来会闪, 我希望解决掉闪烁问题的同时, 能支持贴多个图.
问题相关代码,请勿粘贴截图
void MyPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, HDC hdc, PAINTSTRUCT* ps)
{
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
RECT client_rect;
GetClientRect(hWnd, &client_rect);
HDC hdc1 = CreateCompatibleDC(hdc);
HBITMAP hcbmp1 = CreateCompatibleBitmap(hdc1, cx, cy);
HBITMAP old_bmp1 = (HBITMAP)SelectObject(hdc1, hcbmp1);
Rectangle(hdc1, -1, -1, cx, cy);
HDC hdc2 = CreateCompatibleDC(hdc);
HBITMAP hcbmp2 = CreateCompatibleBitmap(hdc2, cx, cy);
HBITMAP old_bmp2 = (HBITMAP)SelectObject(hdc2, hcbmp2);
HBITMAP hbmp1 = (HBITMAP)LoadImage(NULL, L"c:\\temp\\picture.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
SelectObject(hdc2, hbmp1);
BitBlt(hdc1, 0, 0, cx / 2, cy / 2, hdc2, 0, 0, SRCCOPY);
HBITMAP hbmp2 = (HBITMAP)LoadImage(NULL, L"c:\\temp\\aa.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
SelectObject(hdc2, hbmp2);
BitBlt(hdc1, cx / 2, cy / 2, cx, cy, hdc2, 0, 0, SRCCOPY);
BitBlt(hdc, 0, 0, cx, cy, hdc1, 0, 0, SRCCOPY);
DeleteObject(hbmp2);
SelectObject(hdc2, old_bmp2);
DeleteObject(hcbmp2);
DeleteObject(hdc2);
DeleteObject(hbmp1);
SelectObject(hdc1, old_bmp1);
DeleteObject(hcbmp1);
DeleteObject(hdc1);
}
运行结果及报错内容
我的解答思路和尝试过的方法
我创建了2个兼容dc, dc2负责加载位图, dc1负责临时保存我贴好的图, 最后把完整的dc1的内容拷贝到hdc里面, 但是拷贝之后显示的是黑色的