Win32 Console with colors

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Win32 Console with colors

Post by Enigma » Wed Jul 20, 2011 4:24 am

Hi
The code below will demonstrate how to print text with colors in a console. :)
Note : This will run only in windows.

Code: Select all

/*
major colors are 
FOREGROUND_RED,FOREGROUND_BLUE,FOREGROUND_GREEN
BACKGROUND_BLUE,BACKGROUND_GREEN and BACKGROUND_RED
you can create new colors by performing the bit-wise operations on these colors 
please note that the MSDOS is not based on RGB color scheme. So it
only supports for 6 colors (as far as I know).

FOREGROUND_INTENSITY - this will increase the intensity of the text in the DOS screen
BACKGROUND_INTENSITY - this will increase the intensity of the background color of the DOS screen

*/


#include <windows.h>
#include <stdio.h>

HANDLE hConsole;
CONSOLE_SCREEN_BUFFER_INFO ScreenBufInfo;

WORD colorWhite =BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED;

//These constants are used to draw the lines 
char line[] = {
						0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,
						0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,
						0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,
						0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0x0A
					};

void cPrintf(const char* szValue,WORD color);

int main()
{

	cPrintf("This is Red Color\n",FOREGROUND_RED);
	cPrintf("This is Blue Color\n",FOREGROUND_BLUE);
	cPrintf("This is Green Color\n",FOREGROUND_GREEN);
	
	printf("\n");
	
	cPrintf("This is Red with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_RED);
	cPrintf("This is Blue with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_BLUE);
	cPrintf("This is Green with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_GREEN);
	cPrintf("This is Yellow with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED);
	cPrintf("This is Green with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
	cPrintf("This is Purple with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_RED);
	
	printf("\n");
	
	cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_RED);
	cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_BLUE);
	cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_GREEN);
	cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED);
	cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
	cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_RED);
	
	printf("\n");
	cPrintf("This is Red Color with Background\n",FOREGROUND_RED|colorWhite);
	cPrintf("This is Blue Color with Background\n",FOREGROUND_BLUE|colorWhite);
	cPrintf("This is Green Color with Background\n",FOREGROUND_GREEN|colorWhite);
	printf("\n\n");
	system("pause");
	return 0;
}


void cPrintf(const char* szValue,WORD color)
{
	// free the memeory for the struct
	ZeroMemory(&ScreenBufInfo,sizeof(ScreenBufInfo)); 
	//geting the handle of the console
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
	// saving the original values of the console buffer
	GetConsoleScreenBufferInfo(hConsole,&ScreenBufInfo);
	//setting the new color
	SetConsoleTextAttribute(hConsole,color);
	//printing the value with new color
	printf(szValue);
	// resting the color to its normal values 
	SetConsoleTextAttribute(hConsole,ScreenBufInfo.wAttributes);
}
colorli.jpg
colorli.jpg (38.9 KiB) Viewed 10597 times
Thanks
Last edited by Enigma on Fri Jul 22, 2011 12:41 pm, edited 3 times in total.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Win32 Console with colors

Post by Nipuna » Wed Jul 20, 2011 7:53 am

Great Man :)
Chathura
Sergeant Major
Sergeant Major
Posts: 33
Joined: Mon Mar 08, 2010 2:42 pm

Re: Win32 Console with colors

Post by Chathura » Wed Jul 20, 2011 10:46 am

coooooooooooooool.............
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Win32 Console with colors

Post by Neo » Wed Jul 20, 2011 12:08 pm

Again an excellent code snippet Enigma.

I have just added synax=c blocks instead of Code blocks for C syntax highlighting. See whether you like it.
You need to maintain indents with tabs/spaces to get it working right so I had to add indents to the code as well.

ROBOT.LK now supports many languages for syntax highlighting through new 'syntax' blocks. See the BBCode guide for more information.

REP+
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: Win32 Console with colors

Post by Enigma » Wed Jul 20, 2011 6:19 pm

wow.Its great to have syntax highlighting. I'm sorry Neo. I will add the necessary syntax blocks next time.

Thanks
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Win32 Console with colors

Post by SemiconductorCat » Tue Aug 23, 2011 3:04 am

great !

anyway there is another OR'ed flag to get blink text as I seen from older guy who
did the same thing.

if you using pure "C" why you need this line ? [line 31].you only need it to silience the compiler.

Code: Select all

void cPrintf(const char* szValue,WORD color);
in C you don't need forward declarations.But In C++ you need it.
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: Win32 Console with colors

Post by Enigma » Tue Aug 23, 2011 10:39 am

Hi
Welcome to the Expertcore. :)
anyway there is another OR'ed flag to get blink text as I seen from older guy who
did the same thing.
Wow I didn't know that. I'll check that. Thanks.
if you using pure "C" why you need this line ? [line 31].you only need it to silience the compiler.

Code: Select all
void cPrintf(const char* szValue,WORD color);



in C you don't need forward declarations.But In C++ you need it.
I always declare the function before I use it or else I can declare the function before the main method. Otherwise compiler will throw an error. What do you mean by silence the compiler ?

Thanks
Enigma
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: Win32 Console with colors

Post by Saman » Tue Aug 23, 2011 11:33 am

anyway there is another OR'ed flag to get blink text as I seen from older guy who
did the same thing.
It's good know too. Can you add the code snippet.
if you using pure "C" why you need this line ? [line 31].you only need it to silience the compiler.
Code: Select all
void cPrintf(const char* szValue,WORD color);

in C you don't need forward declarations.But In C++ you need it.
Since the function is implemented after the calling routine, it is required to define the prototype of it above the first calling line. However in common practice, prototypes are defined in the header so it is not required to define them in the source.

Normal C++ way of writing is quite different since we always tend to use OOP way of writing. So prototype definitions are not required at all since lexical analysis for classes is done is an atomic way.
Welcome to the Expertcore. :)
Ohhh. I didn't notice that Sandun is a new member :D
I would like to warmly welcome you to ROBOT.LK :biggrin:

Could you please write a little introduction about yourself in Members Only ‹ Introduction.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Win32 Console with colors

Post by SemiconductorCat » Tue Aug 23, 2011 8:33 pm

Since the function is implemented after the calling routine, it is required to define the prototype of it above the first calling line.
No No, In C there are differences. you don't need to define prototype before calling.
Just write a simple C program and see yourself.

such like this

Code: Select all

#include <stdio.h> // in C you don't have iostream
#include <stdlib.h>


int main(int argc, char **argv)
{
   f1();

    return 0;
}

void f1()
{
   printf("f1() \n");
   return;
}

Thanks for the warm welcome bro. I heard about this froum from elakiri [dot] com person.
I don't know his name here anyway. finally I got a nice community.

Long Live ROBOT.LK community!
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Win32 Console with colors

Post by SemiconductorCat » Tue Aug 23, 2011 8:48 pm

What do you mean by silence the compiler ?
The warning in compiler process will make lots of noise when building. For a example when we
want to say "Hay Compiler you should ignore the warning between lines 543-676 and should not
ignore other warnings" then , there is a method to do that. Using the pragma directive you can
disable the warnings at line 543 and re-enable it when 676.

google for pragma warnings and read it. I can't submit links yet.

or you can report warning as a error. In that case you can configure your C compiler to be more
strict.

Many C++ only people get confused when they heard that C does not have overloading or
type checking. Another difference between C and C++. There are few differences between
C and C++.

Another thing is the strut thing.In C it's necessary to.

Code: Select all


struct tag_date 
{
  int month;
  int date;
  int year;
} date ,* ptr_date;

struct date d1;
or

Code: Select all

typedef struct {
  int month;
  int date;
  int year;
} date,*ptr_date;

date d1;
But In C++ you don't need to use typedef or struct either.
Post Reply

Return to “C/C++ Programming”