VB icon

color in win32 console aplication

Email
Submitted on: 1/1/2015 11:03:00 PM
By: Patrick Royer (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: Microsoft Visual C++
Views: 843
 
     This code put colors in win32 Console aplication.

 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :color in win32 console aplication
//**************************************
#include <windows.h>
#include <iostream.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: color in win32 console aplication
// Description:This code put colors in win32 Console aplication.
// By: Patrick Royer (from psc cd)
//
// Inputs:He doesn't take any paramaters...
//
// Returns:He doesn't return anything...
//
// Assumes:I take functions in windows.h headers for put colors. The 3 functions is :
SetConsoleTextAttribute(cxHandle, Color);
//To choose the color
SetConsoleCursorPosition(cxHandle, cxCoord);
//to move the cursor
WriteConsole(cxHandle, Text, strl(Text),&Result, NULL);
//to print the text
Take care do you have the handle of the window and call the function whit it.
(In the example : cxHandle)
//**************************************

	/************************************************
	*The declaration for WORD; COORD; DWORD; HANDLE**
	************************************************/
#include <windows.h>
	
	/***************************************************
	*The declaration for cout; FOREGROUND_; BACKGROUND_*
	***************************************************/
#include <iostream.h>
	/************************************************
	*The declaration of an objet HANDLE*
	************************************************/
HANDLE cxHandle;
	/******************************************************************
	*It's the function to Set posX, posY, the color And Print the Text*
	******************************************************************/
void SetPosXYAndColor(int x, int y, LPSTR Text, WORD Color);
	/************************************************
	* Macro colors of Text*
	************************************************/
#define BLUE FOREGROUND_BLUE
#define LIGHTBLUE FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define RED FOREGROUND_RED
#define LIGHTRED FOREGROUND_RED | FOREGROUND_INTENSITY
#define GREEN FOREGROUND_GREEN
#define LIGHTGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define PURPLE FOREGROUND_BLUE | FOREGROUND_RED
#define LIGHTPURPLE PURPLE | FOREGROUND_INTENSITY
#define BLUEGREEN FOREGROUND_BLUE | FOREGROUND_GREEN
#define LIGHTBLUEGREEN BLUEGREEN | FOREGROUND_INTENSITY 
#define ORANGE FOREGROUND_GREEN | FOREGROUND_RED
#define YELLOW ORANGE | FOREGROUND_INTENSITY
#define INTENSITY FOREGROUND_INTENSITY
	/************************************************
	*Macro colors for background *
	************************************************/
#define BBLUE BACKGROUND_BLUE
#define BLIGHTBLUE BACKGROUND_BLUE | BACKGROUND_INTENSITY
#define BRED BACKGROUND_RED
#define BLIGHTRED BACKGROUND_RED | BACKGROUND_INTENSITY
#define BGREEN BACKGROUND_GREEN
#define BLIGHTGREEN BACKGROUND_GREEN | BACKGROUND_INTENSITY
#define BPURPLE BACKGROUND_BLUE | BACKGROUND_RED
#define BLIGHTPURPLE BPURPLE | BACKGROUND_INTENSITY
#define BBLUEGREEN BACKGROUND_BLUE | BACKGROUND_GREEN
#define BLIGHTBLUEGREEN BBLUEGREEN | BACKGROUND_INTENSITY 
#define BORANGE BACKGROUND_GREEN | BACKGROUND_RED
#define BYELLOW ORANGE | BACKGROUND_INTENSITY
#define BINTENSITY BACKGROUND_INTENSITY
void main()
{
		/************************************************
		* cxHandle Take the HANDLE of the win32 Console *
		************************************************/
	cxHandle = GetStdHandle (STD_OUTPUT_HANDLE);
		/****************************************************
		*Call the function to setup the posX, posY, color *
		*And print the message... *
		*****************************************************/
	SetPosXYAndColor(5,5, "Hello World!", YELLOW);
	
}
/******************************************************************
*It's the function to Set posX, posY, the color And Print the Text*
******************************************************************/
void SetPosXYAndColor(int x, int y, LPSTR Text, WORD Color)
{
		/************************************************************
		*Declaration of an objet to take the return value*
		************************************************************/
	DWORD Result;
		
		/************************************************************
		*Declaration of an objet to take the posX and posY of cursor*
		************************************************************/
	COORD cxCoord;
	cxCoord.X = x;
	cxCoord.Y = y;
	
		/**************************************************
		* Setup the color on the screen by the HANDLE *
		**************************************************/
	SetConsoleTextAttribute(cxHandle, Color);
	
		/*****************************************************
		*Setup the CursorPosition on the screen by the HANDLE*
		*****************************************************/
	SetConsoleCursorPosition(cxHandle, cxCoord);
		/************************************************************
		*Write the text on the screen whit previous settings *
		************************************************************/
	WriteConsole(cxHandle, Text, strlen(Text), &Result, NULL);
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.