我有以下函数可以将加载的位图绘制到窗口。
void OnPaint(HWND hwnd) {
PAINTSTRUCT ps;
HDC hdc;
BITMAP bitmap;
HDC hdcMem;
HGDIOBJ oldBitmap;
hdc = BeginPaint(hwnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
HBITMAP bmp = mainBitmap;
oldBitmap = SelectObject(hdcMem, mainBitmap);
GetObject(bmp, sizeof(bitmap), &bitmap);
x += 64;
RECT rect;
rect.left = x;
rect.top = 0;
rect.right = x+64;
rect.bottom = 64;
HBITMAP hBmp = CreateCompatibleBitmap(
hdc, // Handle to a device context
rect.right - rect.left, // Bitmap width
rect.bottom - rect.top // Bitmap height
);
BitBlt(
hdc, // Destination rectangle context handle
0, // Destination rectangle x-coordinate
0, // Destination rectangle y-coordinate
rect.right - rect.left, // Destination rectangle width
rect.bottom - rect.top, // Destination rectangle height
hdcMem, // A handle to the source device context
rect.left, // Source rectangle x-coordinate
rect.top, // Source rectangle y-coordinate
SRCCOPY // Raster-operation code
);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
}
我将以下图像加载到 HBITMAP mainBitmap
中:
图像在窗口成功打开时绘制,我在 Sprite 位图中看到第一个图标(黄色抓钩),但我的问题是,当我按 'C'
重新 -绘制窗口,图像不会更改为 Sprite 图像中的下一个图标。
我所知道的事情
初始化时,
x = 64;
每次我按下
'C'
时,都会调用绘图。 (视觉确认 Studio 调试器)每次调用
OnPaint
时,x 都会增加 64。
为什么图形没有变化?
这是我的 WindowsProc
函数,用于处理 WM_PAINT
消息:
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
HANDLE_MSG(hwnd, WM_PAINT, OnPaint);
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
最佳答案
尝试调用function InvalidateRect()更新区域。
关于c++ - BitBlt 不更新 Windows C++ API 中的 WM_PAINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46217688/