I am writing a program in C. I want to change the text color and background color in the console. My sample program is -
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dos.h>
#include <dir.h>
int main(int argc,char *argv[])
{
textcolor(25);
printf("\n \n \t This is dummy program for text color ");
getch();
return 0;
}
When I compile this program code::blocks gives me an error - textcolor not defined. Why is this so? I work in a GNU GCC compiler and Windows Vista. If it is not going to work what is the duplicate of textcolor. Like that I want to change the background color of the console. The compiler gives me the same error just the name of the function is different. How to change the color of the console and text. Please help.
I am okay even if the answer is in C++.
This question is related to
c++
c
gcc
colors
codeblocks
textcolor
function is no longer supported in the latest compilers.
This the simplest way to change text color in Code Blocks.
You can use system
function.
To change Text Color :
#include<stdio.h>
#include<stdlib.h> //as system function is in the standard library
int main()
{
system("color 1"); //here 1 represents the text color
printf("This is dummy program for text color");
return 0;
}
If you want to change both the text color & console color you just need to add another color code in system
function
To change Text Color & Console Color :
system("color 41"); //here 4 represents the console color and 1 represents the text color
N.B: Don't use spaces between color code like these
system("color 4 1");
Though if you do it Code Block will show all the color codes. You can use this tricks to know all supported color codes.
You should define the function textcolor before. Because textcolor is not a standard function in C.
void textcolor(unsigned short color) {
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
I Know, I am very late but, May be my answer can help someone. Basically it's very Simple. Here is my Code.
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
HANDLE colors=GetStdHandle(STD_OUTPUT_HANDLE);
string text;
int k;
cout<<" Enter your Text : ";
getline(cin,text);
for(int i=0;i<text.length();i++)
{
k>9 ? k=0 : k++;
if(k==0)
{
SetConsoleTextAttribute(colors,1);
}else
{
SetConsoleTextAttribute(colors,k);
}
cout<<text.at(i);
}
}
OUTPUT
This Image will show you how it works
If you want the full tutorial please see my video here: How to change Text color in C++
You can also use rlutil:
rlutil.h
),setColor()
, cls()
, getch()
, gotoxy()
, etc.Your code would become something like this:
#include <stdio.h>
#include "rlutil.h"
int main(int argc, char* argv[])
{
setColor(BLUE);
printf("\n \n \t This is dummy program for text color ");
getch();
return 0;
}
Have a look at example.c and test.cpp for C and C++ examples.
An Easy Approach...
system("Color F0");
Letter Represents Background Color while the number represents the text color.
0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
system("COLOR 0A");
'
where 0A is a combination of background and font color 0
This is a function online, I created a header file with it, and I use Setcolor();
instead, I hope this helped! You can change the color by choosing any color in the range of 0-256. :) Sadly, I believe CodeBlocks has a later build of the window.h library...
#include <windows.h> //This is the header file for windows.
#include <stdio.h> //C standard library header file
void SetColor(int ForgC);
int main()
{
printf("Test color"); //Here the text color is white
SetColor(30); //Function call to change the text color
printf("Test color"); //Now the text color is green
return 0;
}
void SetColor(int ForgC)
{
WORD wColor;
//This handle is needed to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//csbi is used for wAttributes word
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//To mask out all but the background attribute, and to add the color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
Source: Stackoverflow.com