In my earlier tutorials, I took you through how to change text color and text background color in Code::Blocks. In this tutorial, I will take you through “How to Change Console Windows Background Color in CodeBlocks”. This is pretty simple if you’ve already gone through my previous posts.
To change the console window background color, simply copy the source code provided here, and compile it in Code::Blocks IDE. The code is written in C language, and I have included comments in the code to help you understand it better.
The source code consists of two header files, windows.h, the header file for windows, and stdio.h, the C standard library header file. The function void ClearConsoleToColors (int ForgC, int BackC) takes two integer values, and is responsible for the change in the background color of CodeBlocks console window.
Source Code:
Currently the console window background color displayed is Red color. You can change your desired console windows color by passing integer values (in the range of 0 to 256) in the ClearConsoleToColors (int ForgC, int BackC) function. Without using this function, the background color in console would’ve been default black.
//How to Change Console Windows Background Color in CodeBlocks
#include <windows.h> //This is the windows header file
#include <stdio.h> //C standard library header file
void ClearConsoleToColors(int ForgC, int BackC); //function declaration
int main()
{
ClearConsoleToColors(10,4); //function call to change console background color
Sleep(1000);
return 0;
}
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//This is used to get the handle to current output buffer.
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//This is a return value indicating how many characterss were written
// It is not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all the console info
// It is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Now the current color will be set by this handle
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
//This will set our cursor position for the next print statement
SetConsoleCursorPosition(hStdOut, coord);
}
return;
}
Here’s an output screen of what happened in the console window. As you can see, with the function ClearConsoleToColors (int ForgC, int BackC), the background of the console window changed to red with integer values (10,4).
With this post, I hope you learnt how to change console windows background color in CodeBlocks without using graphics. If you have any queries regarding the source code, bring them up to me from the comments section.