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

时间:2018-09-01 01:48:01

标签: c winapi bitmap

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

HBITMAP hbm = CreateBitmap(width, height, 1, 24, buffer);

(显然,这是每个像素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;

}

这是我的完整代码,以防万一您要复制它...

#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;

}

1 个答案:

答案 0 :(得分:4)

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);

['Start', 'Bottom right', 1]['South', 1, 1], ['North', 3, 4], ['West', 4, 0], ['West', 2, 0], ['South', 3, 4]