c++ - BitBlt 不更新 Windows C++ API 中的 WM_PAINT

标签 c++ winapi bitmap sprite-sheet

我有以下函数可以将加载的位图绘制到窗口。

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

图像在窗口成功打开时绘制,我在 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/

相关文章:

c++ - 在模板参数 : Some usage examples and. 中使用 auto .. 如何使其与恒定大小的 C 数组一起使用?

c++ - 如何制作过滤掉非整数的 C++ 程序?

C++ 三字母语言名称到 LCID

c# - C++ 架构 : how is it similar to machine architecture

c++ - 带有 O1、O2 或 O3 的 gcc 4.7.3 创建错误代码

wpf - 打印窗口 WPF/DirectX

带有 bInitialOwner=true 的 CreateMutex 似乎表现得很奇怪

android - 将图像加载到位图对象时出现奇怪的 OutOfMemory 问题

Android 位图大于 heapSize

android - 在 Android 应用程序中更改位图分辨率