Drawing in a Win32 Console on C++?

Yes, it is possible.

Get the HWND of the console window using GetConsoleWindow and then draw in it.

#define _WIN32_WINNT 0x601
#include <windows.h>
#include <stdio.h>

int main() {
    // Get window handle to console, and device context
    HWND console_handle = GetConsoleWindow();
    HDC device_context = GetDC(console_handle);

    //Here's a 5 pixels wide RED line [from initial 0,0] to 300,300
    HPEN pen = CreatePen(PS_SOLID, 5, RGB(255, 0, 0));
    SelectObject(device_context, pen);
    LineTo(device_context, 300, 300);

    ReleaseDC(console_handle, device_context);

    getchar();

    return 0;
}

Note: GetConsoleWindow was introduced in Windows 2000. It’s available when _WIN32_WINNT is set to 0x500 or greater.

Leave a Comment