Click here to Skip to main content
Licence CPOL
First Posted 5 Dec 2006
Views 16,123
Bookmarked 17 times

Coloured Error Messages for Console Applications

By | 5 Dec 2006 | Article
Implementation of a function which will print coloured messages on console window

Introduction

While working with console applications, sometimes we want to print coloured error messages. Here is the implementation of a very small function which will help in accomplishing the task. 

int _teprintf(TCHAR *format, ...) 
{
    HANDLE hStdOut = NULL ;
    TCHAR szBuffer[1024] ;
    va_list arg_list ;
    int nBuf = 0 ;
    va_start(arg_list,format);
    nBuf = _vsntprintf(szBuffer,1024,format,arg_list) ;
    va_end(arg_list) ;
    //get console output handle
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);    
    if(!hStdout) {
        return 0;
    }
    //set red colour
    if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED | 
        FOREGROUND_INTENSITY)) {
        return 0;
    }
    //print 
    nBuf = _tprintf(_T("%s"),szBuffer) ;
    //restore to white.
    if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED |
        FOREGROUND_BLUE | 
        FOREGROUND_GREEN)) {
        return 0;
    }
    return nBuf ;
}

You can use it as follows:

int _tmain(int argc, _TCHAR* argv[])
{
	_teprintf(_T("\nI am Error\n")) ;
	_tprintf(_T("\nI am Normal\n")) ;
	return 0;
}

Though this is trivial, it is fun..!

History

  • 5th December, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Prateek Kumar Dubey

Other
Microsoft
India India

Member

I am currently working with Microsoft at Bangalore (India). My interest lies in areas of generic C++ and windows development. Apart from office hours I try to develop new and useful small tools.
Well, I still feel that I need to be more serious..!
Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalmore perfect idea PinmemberYacha20:52 13 Dec '06  
Generalrestore original colors Pinmemberosy23:03 5 Dec '06  
nice idea!
If a user selected non standard colors you can restore this with the following lines:
 
// save current text colors
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
::GetConsoleScreenBufferInfo( consoleOut_, &csbiInfo );
 
// ...write message
 
// reset colors
::SetConsoleTextAttribute( consoleOut_, csbiInfo.wAttributes );

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 6 Dec 2006
Article Copyright 2006 by Prateek Kumar Dubey
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid