Solved

Color Printf's

Posted on 1998-05-01
11
496 Views
Last Modified: 2013-12-14
I would like change the color of several printf statements in a VC++ console app. For example, I would like to have certain strings printed to the user display in
a red color to better distinguish them. Could someone provide an example or point me in the direction of how to
do this?
 
0
Comment
Question by:vd
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 1

Expert Comment

by:zhanghl
ID: 1256743
The function _textcolor is used to specify color number.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1256744
There is no such function in MSVC 4.2.  I think is is probably a 16 bit function.
0
 
LVL 37

Expert Comment

by:Bing CISM / CISSP
ID: 1256745
Yes, it is for 16-bit. Only the 32-bit version you need?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1256746
A "console app" implies Win32.  There was no concept of consoles in Win3.x, only DOS mode applications.
0
 

Author Comment

by:vd
ID: 1256747
No, there doesn't appear to be a _textcolor function!
BBAO/ALEXO:  Yes, it's a Win32 Console App (MS VC++ 4)


Vernon
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Accepted Solution

by:
RONSLOW earned 80 total points
ID: 1256748
Console can have text background and foreground colours
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256749
SetConsoleTextAttribute sets the foreground and background colours for console output.  You can use combinations of FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_INTENSITY, BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_RED, and BACKGROUND_INTENSITY

Write to the console using WriteConsole (printf probably works as well).

You can set up screen buffers which can by scrolled around in the window.  You can directly draw colours and/or characters in rectangular block .. have multiple screen buffer etc. It is quite powerful really.

0
 

Author Comment

by:vd
ID: 1256750
Could you show me how to use WriteConsole(), particularly with
SetConsoleTextAttribute()?  
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256751
Let me just look it up in my little black book :-)

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256752
Look at the "Console" SDK sample

here is a snippet...

  /* set attributes for new writes to the console */
  SetConsoleTextAttribute(hConsole, BACKGROUND_BLUE | FOREGROUND_WHITE);
  /* clear screen to newly set color attribute */
  cls(hConsole);
  /* source code for cls is in console sample */
  coordScreen.X = 0;
  coordScreen.Y = 0;
  SetConsoleCursorPosition(hConsole, coordScreen);
  sprintf(szTemps, "%-3d%s", i + 1, conAPIs[i]);
  myPuts(hConsole, szTemps);
  /* source code for myPuts is in console sample */
  /* it calls WriteConsole */
  /* color the number yellow on blue */
  FillConsoleOutputAttribute(hConsole, FOREGROUND_YELLOW | FOREGROUND_INTENSITY | BACKGROUND_BLUE, 3, coordScreen, &cCharsWritten);
  coordScreen.X = 3;
  /* color text light cyan on blue */
  FillConsoleOutputAttribute(hConsole, FOREGROUND_CYAN | FOREGROUND_INTENSITY | BACKGROUND_BLUE, strlen(conAPIs[i]), coordScreen, &cCharsWritten);


myPuts is:

BOOL myPuts(HANDLE hConsole, PCHAR s) {
  BOOL bSuccess;
  DWORD cCharsWritten;
  const PCHAR crlf = "\n";
  BOOL retflag = TRUE;  
  /* write the string to the console */
  bSuccess = WriteConsole(hConsole, s, strlen(s), &cCharsWritten, NULL);
  retflag = bSuccess;
  /* put a carriage return & line feed after the string */
  bSuccess = WriteConsole(hConsole, crlf, strlen(crlf), &cCharsWritten, NULL);
  if (!bSuccess) retflag = FALSE;
  return(retflag);
}

and cls is

void cls(HANDLE hConsole) {
  COORD coordScreen = { 0, 0 };
  BOOL bSuccess;
  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD dwConSize;
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten);
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  SetConsoleCursorPosition(hConsole, coordScreen);
  return;
}

See the Console sample and on-line help for more details.

0
 

Author Comment

by:vd
ID: 1256753
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now