Solved

Color Printf's

Posted on 1998-05-01
11
499 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

831 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