Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to draw a pixel on a window in C Win32 API

1 Answer

0 votes
#define WIN32_LEAN_AND_MEAN

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

#define Assert(s) do { if (!(s)) __debugbreak(); } while (0)

typedef uint32_t u32;

void* memory;
u32 client_width;
u32 client_height;

void ClearScreen(u32 color);
void DrawPixel(u32 x, u32 y, u32 color);

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

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // Create window

    WNDCLASSW wc = {
        .lpszClassName = L"MyWindowClassName",
        .lpfnWndProc = WndProc,
        .hInstance = hInstance,
        .hCursor = LoadCursor(0, IDC_CROSS)
    };

    ATOM atom = RegisterClassW(&wc);
    Assert(atom && "Failed to register a window");

    HWND window = CreateWindowW(wc.lpszClassName,
        L"Draw A Pixel",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL);
    Assert(window && "Failed to create a window");

    ShowWindow(window, nCmdShow);

    // Allocate memory

    RECT rect;
    GetClientRect(window, &rect);
    client_width = rect.right - rect.left;
    client_height = rect.bottom - rect.top;

    // Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process. 
    // Memory allocated by this function is automatically initialized to zero.

    memory = VirtualAlloc(0,
        client_width * client_height * 4,
        MEM_RESERVE | MEM_COMMIT,
        PAGE_READWRITE
    );

    // Create BITMAPINFO struct for StretchDIBits

    BITMAPINFO bitmap_info = {
        .bmiHeader.biSize = sizeof(bitmap_info.bmiHeader),
        .bmiHeader.biWidth = client_width,
        .bmiHeader.biHeight = client_height,
        .bmiHeader.biPlanes = 1,
        .bmiHeader.biBitCount = 32,
        .bmiHeader.biCompression = BI_RGB
    };

    HDC hdc = GetDC(window);

    bool running = true;
    while (running) {
        MSG msg;
        while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT) {
                running = false;
            }
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }

        ClearScreen(0x000000);

        // Draw white pixel at 200, 200 (from bottom left).

        DrawPixel(200, 200, 0xffffff);

        // The StretchDIBits function copies the color data for a rectangle of pixels 
        // in a DIB, JPEG, or PNG image to the specified destination rectangle.

        StretchDIBits(hdc,
            0,
            0,
            client_width,
            client_height,
            0,
            0,
            client_width,
            client_height,
            memory,
            &bitmap_info,
            DIB_RGB_COLORS,
            SRCCOPY
        );
    }

    return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
    LRESULT result = 0;

    switch (msg)
    {
        case WM_KEYDOWN:
        {
            if (wparam == VK_ESCAPE) {
                DestroyWindow(hwnd);
            }
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        default:
            result = DefWindowProcW(hwnd, msg, wparam, lparam);
        }

    return result;
}

void ClearScreen(u32 color) {
    u32* pixel = (u32*)memory;

    for (u32 i = 0; i < client_width * client_height; i++) {
        *pixel++ = color;
    }
}

void DrawPixel(u32 x, u32 y, u32 color) {
    u32* pixel = (u32*)memory;
    pixel += y * client_width + x;
    *pixel = color;
}



/*
run:



*/

 



answered Aug 12, 2024 by avibootz
...