Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Advanced Console Manipulation Techniques in C++ for Windows

Tech May 10 2

Controlling the Windows console beyond basic input and output enables richer terminal-based applications and games. The following techniques rely on the windows.h header and are specific to the Windows platform.

Cursor Control

Hiding the Cursor

To prevent visual distraction during gameplay, the blinking cursor can be hidden:

void HideCursor() {
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 1;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
}

Retrieving Cursor Position

The current cursor coordinates can be obtained from the console screen buffer:

COORD GetCurrentCursorPosition() {
    CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &bufferInfo);
    return bufferInfo.dwCursorPosition;
}

Repositioning the Cursor

Precise cursor placement is essential for rendering dynamic content:

void MoveCursorTo(int x, int y) {
    COORD position = {static_cast<SHORT>(x), static_cast<SHORT>(y)};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}

Window Dimensions

Resizing the Console

The visible console area can be resized using the mode command via system():

void ResizeConsole(int columns, int rows) {
    char command[64];
    std::snprintf(command, sizeof(command), "mode con cols=%d lines=%d", columns, rows);
    std::system(command);
}

Locking Window Size

To prevent user resizing, modify the window style flags:

void LockWindowSize() {
    HWND consoleWindow = GetConsoleWindow();
    LONG_PTR style = GetWindowLongPtrA(consoleWindow, GWL_STYLE);
    style &= ~(WS_SIZEBOX | WS_MAXIMIZEBOX | WS_MINIMIZEBOX);
    SetWindowLongPtrA(consoleWindow, GWL_STYLE, style);
}

Text Coloring

Global Color via system()

The entire console's foreground and background colors can be set with a single command:

std::system("color 0A"); // Black background, bright green text

Attribute-Based Colornig

Individual text attributes can be controlled using SetConsoleTextAttribute:

void SetTextColor(WORD foreground, WORD background = 0) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                            (background << 4) | foreground);
}

Common foreground values: 0–15 (e.g., FOREGROUND_RED, FOREGROUND_INTENSITY).

True Color with ANSI Escape Codes

For RGB-level precision, enable virtual terminal processing and use ANSI sequences:

void EnableVirtualTerminalProcessing() {
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD mode = 0;
    GetConsoleMode(hOut, &mode);
    mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    SetConsoleMode(hOut, mode);
}

// Usage after enabling VT mode:
// wprintf(L"\x1b[38;2;%d;%d;%dmSample Text\x1b[0m", r, g, b);

Note: Include <wchar.h> or <cwchar> for wprintf.

Console Management

Setting the Window Title

Replace the default executable path in the title bar:

SetConsoleTitleA("My Game");

Detaching from Console

A process can disconnect from its associated console:

FreeConsole();

Creatnig a New Console

An application without a console can allocate one dynamical:

AllocConsole();

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.