C Online Compiler
Example: Windows API Background Color Example in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Windows API Background Color Example #include <stdio.h> #include <windows.h> // Required for Windows Console API functions int main() { // Step 1: Get a handle to the standard output device (console) HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Step 2: Define color attributes using a combination of flags // Background colors: // BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_RED, BACKGROUND_INTENSITY // Foreground colors: // FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_INTENSITY // Default attributes (usually white foreground, black background) WORD defaultAttributes; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole, &csbi); defaultAttributes = csbi.wAttributes; // Display text with red background and white foreground SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | BACKGROUND_RED); // White text, Red background printf("This text has a red background and white foreground.\n"); // Reset to default attributes SetConsoleTextAttribute(hConsole, defaultAttributes); printf("This text has the default background and foreground.\n"); // Display text with green background and black foreground SetConsoleTextAttribute(hConsole, FOREGROUND_BLACK | BACKGROUND_GREEN); // Black text, Green background printf("This text has a green background and black foreground.\n"); // Reset to default attributes SetConsoleTextAttribute(hConsole, defaultAttributes); printf("Another line with default colors.\n"); return 0; }
Output
Clear
ADVERTISEMENTS