从像素缓冲区创建 HBITMAP 然后渲染

Spi*_*ice 2 c winapi bitmap

好的,所以我刚开始使用 WinAPI 来绘制位图图像。因此,如果我的代码绝对是垃圾,我深表歉意。无论如何,我该怎么做?本质上,我想使用这样HBITMAP的简单unsigned char像素缓冲区创建一个对象CreateBitmap()......

HBITMAP hbm = CreateBitmap(width, height, 1, 24, buffer);
Run Code Online (Sandbox Code Playgroud)

(显然这是每像素 3 字节)

在我尝试渲染它之前,这似乎工作正常。现在,我只想保持简单并在屏幕上绘制一个 100 x 100 的黑色像素方块,但什么也没显示。如果有人能指出我的残暴代码中的错误,我将不胜感激。

这是我的设置(我知道很多事情都可以改进,我现在只想尽可能地简约,实际上屏幕上会出现一些东西):

HBITMAP hbm = NULL;

void window_init(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    static unsigned char buffer[30000] = {0};
    hbm = CreateBitmap(100, 100, 1, 24, buffer);

}
void window_draw(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    HDC hdc = BeginPaint(hwnd, &ps);
    HDC hdcMem = CreateCompatibleDC(hdc);

    HBITMAP hbmOld = SelectObject(hdcMem, hbm);
    BitBlt(hdc, 100, 100, 100, 100, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);

    EndPaint(hwnd, &ps);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {

        case WM_CREATE: window_init(hwnd, msg, wParam, lParam); break;
        case WM_PAINT: window_draw(hwnd, msg, wParam, lParam); break;
        case WM_CLOSE: DestroyWindow(hwnd); break;
        case WM_DESTROY: PostQuitMessage(0); break;
        default: return DefWindowProc(hwnd, msg, wParam, lParam);

    }

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

这是我的整个代码,以防您想复制它...

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct {

    const char *wc_name;
    const char *title;

    unsigned int width;
    unsigned int height;

} Window;

static HBITMAP hbmp = NULL;
static PAINTSTRUCT ps;

void window_create(Window *window, const char *wc_name, const char *title, unsigned int width, unsigned int height) {

    window->wc_name = wc_name;
    window->title = title;

    window->width = width;
    window->height = height;

}
void window_init(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    static unsigned char buffer[30000] = {0};
    hbmp = CreateBitmap(100, 100, 1, 24, buffer);

}
void window_draw(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    HDC hdc = BeginPaint(hwnd, &ps);
    HDC hdcMem = CreateCompatibleDC(hdc);

    HBITMAP hbmOld = SelectObject(hdcMem, hbmp);
    BitBlt(hdc, 100, 100, 100, 100, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);;

    EndPaint(hwnd, &ps);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {

        case WM_CREATE: window_init(hwnd, msg, wParam, lParam); break;
        case WM_PAINT: window_draw(hwnd, msg, wParam, lParam); break;
        case WM_CLOSE: DestroyWindow(hwnd); break;
        case WM_DESTROY: PostQuitMessage(0); break;
        default: return DefWindowProc(hwnd, msg, wParam, lParam);

    }

    return 0;

}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

    Window window;
    window_create(&window, "Parent Window", "My Window", 1000, 1000 / 16 * 9);

    WNDCLASSEX wc = {0};
    MSG msg = {0};
    HWND hwnd = NULL;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszClassName = window.wc_name;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&wc)) {

        MessageBox(NULL, "Failed to register application window!", "WC Registration Error!", MB_ICONEXCLAMATION | MB_OK);
        return -1;

    }

    hwnd = CreateWindowEx(

        0, window.wc_name, window.title,
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        window.width, window.height, NULL, NULL, hInstance, NULL

    );

    if (!hwnd) {

        MessageBox(NULL, "Failed to launch application window!", "HWND Creation Error!", MB_ICONEXCLAMATION | MB_OK);
        return -1;

    }

    ShowWindow(hwnd, SW_MAXIMIZE);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0) > 0) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);

    }

    return msg.wParam;

}
Run Code Online (Sandbox Code Playgroud)

Gov*_*mar 5

CreateBitmap生成设备相关位图的句柄,CreateDIBSection这与CreateCompatibleBitmap.

如何使用CreateDIBSection

HDC hdc = GetDC(hwnd);
BITMAPINFO bi = { 0 };
static BYTE *bits = NULL;
static unsigned char buffer[30000] = { 0 };

bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = 100;
bi.bmiHeader.biHeight = 100;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;

hbmp = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
CopyMemory(bits, buffer, 30000); // Can be replaced with arbitrary RGB value population
ReleaseDC(hwnd, hdc);
Run Code Online (Sandbox Code Playgroud)

  • 你认为 `bm.bmBits = buffer;` 是什么? (2认同)