Solved

Color Printf's

Posted on 1998-05-01
11
500 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:bbao
ID: 1256745
Yes, it is for 16-bit. Only the 32-bit version you need?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need example 5 122
Change local server setting in php 6 94
smtp c source code 7 85
nested if statement in excel help 4 37
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

809 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