How to draw pixels to a Window with GDI with C++ Win32 API

1 Answer

0 votes
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX

#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define Rand32() ((rand() << 16) + (rand() << 1) + (rand() & 1))

static bool quit = false;

struct {
    int width;
    int height;
    uint32_t* pixels;
} pixelposinfo = { 0 };

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);


static BITMAPINFO gBITMAPINFO;
static HDC gHDC = 0;

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    const wchar_t window_class_name[] = L"Window Class Name";
    static WNDCLASS window_class = { 0 };

    window_class.lpfnWndProc = WndProc;
    window_class.hInstance = hInstance;
    window_class.lpszClassName = window_class_name;
    RegisterClass(&window_class);

    gBITMAPINFO.bmiHeader.biSize = sizeof(gBITMAPINFO.bmiHeader);
    gBITMAPINFO.bmiHeader.biPlanes = 1;
    gBITMAPINFO.bmiHeader.biBitCount = 32;
    gBITMAPINFO.bmiHeader.biCompression = BI_RGB;
    gHDC = CreateCompatibleDC(0);

    static HWND window_handle;
    window_handle = CreateWindow(window_class_name, L"Drawing Multiple Pixels", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        500, 500, 640, 480, NULL, NULL, hInstance, NULL);
    if (window_handle == NULL) { return -1; }

    while (!quit) {
        static MSG message = { 0 };
        while (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) { 
            DispatchMessage(&message); 
        }

        static unsigned int i = 0;
        pixelposinfo.pixels[(i++) % (pixelposinfo.width * pixelposinfo.height)] = Rand32();
        pixelposinfo.pixels[Rand32() % (pixelposinfo.width * pixelposinfo.height)] = 0;

        InvalidateRect(window_handle, NULL, FALSE);
        UpdateWindow(window_handle);
    }

    return 0;
}


LRESULT CALLBACK WndProc(HWND window_handle, UINT message, WPARAM wParam, LPARAM lParam) {
    HBITMAP hbitmap = 0;

    switch (message) {
        case WM_QUIT:
        case WM_DESTROY: {
            quit = true;
        } break;

        case WM_PAINT: {
            static PAINTSTRUCT paint;
            static HDC hdc;
            hdc = BeginPaint(window_handle, &paint);
            BitBlt(hdc,
                paint.rcPaint.left, paint.rcPaint.top,
                paint.rcPaint.right - paint.rcPaint.left, paint.rcPaint.bottom - paint.rcPaint.top,
                gHDC,
                paint.rcPaint.left, paint.rcPaint.top,
                SRCCOPY);
            EndPaint(window_handle, &paint);
        } break;

        case WM_SIZE: {
            gBITMAPINFO.bmiHeader.biWidth = LOWORD(lParam);
            gBITMAPINFO.bmiHeader.biHeight = HIWORD(lParam);

            if (hbitmap) {
                DeleteObject(hbitmap);
            }
            hbitmap = CreateDIBSection(NULL, &gBITMAPINFO, DIB_RGB_COLORS, (void**)&pixelposinfo.pixels, 0, 0);
            SelectObject(gHDC, hbitmap);

            pixelposinfo.width = LOWORD(lParam);
            pixelposinfo.height = HIWORD(lParam);
        } break;

        default: {
            return DefWindowProc(window_handle, message, wParam, lParam);
        }
    }

    return 0;
}


/*
run:



*/

 



answered Aug 4, 2024 by avibootz
edited Aug 4, 2024 by avibootz
...