?
Solved

Color Printf's

Posted on 1998-05-01
11
Medium Priority
?
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

771 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