Solved

Color Printf's

Posted on 1998-05-01
11
495 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now