Here’s a tutorial that will guide you through “How to highlight or change text background color in CodeBlocks Console Window”. The source code provided here will change text background color or highlight the text in the console window of Code::Blocks without using graphics. You can simply copy the code, and compile it in Code::Blocks IDE. The source 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 SetColorAndBackground (int ForgC, int BackC) takes two integer values, and is responsible for the change in text background color in the console window.
Source Code:
Currently the text background color displayed is Red color. You can change your desired text color by passing integer values (in the range of 0 to 256) in the SetColorAndBackground (int ForgC, int BackC) function. The source code is short; compile it in Code::Blocks, not Turbo C.
//How to Highlight or Change Text Background Color in CodeBlocks
#include <windows.h> //This is the header file for windows
#include <stdio.h> //C standard library header file
void SetColorAndBackground(int ForgC, int BackC); //function declaration
int main()
{
printf("Text and Background Color");
SetColorAndBackground(10,4); //fuction call to change text background color
printf("Text and Background Color");
Sleep(1000);
return 0;
}
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
return;
}
Here’s an output screen of what happened in the console window. As you can see, previously the text color was white and the background color was black. But after the function call with integer values (10,4) in the function SetColorAndBackground (int ForgC, int BackC), the text background was highlighted in red, i.e, its background color changed to red.
With this post, I hope you learnt how to highlight or change text background color in CodeBlocks console window. Also see, how to change text color and console window background color in Code::Blocks. If you have any queries, bring them up to me from the comments box below.