Link to home
Start Free TrialLog in
Avatar of jumanji
jumanji

asked on

Text Color in Console

I'm using MSVis C++6.0 on Win2000.  I'm creating a console app, and have 2 functions (gotoxy() and textattr()) that I extracted from conio.h file (from another compiler).  The following is part of it:

FILE*fp;

void gotoxy(int X,int Y)
{
      HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD Coord = {X,Y};
      SetConsoleCursorPosition(StdOut,Coord);
}
void textattr(int newattr)
{
      HANDLE hnd = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(hnd, newattr);
}
void clrscr()
{
      system("CLS");
}

---------
These functions work properly on my machine.  But, once I compiled it, created an exe file, and ran it on a Win95 machine, the colors got messed up.  I think the problem is the endl part.  For example, if I do this:

textattr(31);
clrscr();
textattr(20);
gotoxy(3,2);
cout << "Hello..." << endl;

Win2k and NT didn't have a problem.  It displayed the background as blue, and the "Hello..." part as a different color.  The whole row didn't change color.  But, when I ran the same program on a Win95 machine, the whole ROW (from the "H" to the endl) changed to attr(20).  Is there ANY workaround for this?  I NEED the endl statement, since gotoxy() requires it (somehow...)
Avatar of jumanji
jumanji

ASKER

Simply put, I guess I just need a way to "reset" the gotoxy() function and textattr() functions, without using cout << endl;  If anyone knows a way, it would REALLY be helpful...  Thnx
Don't try to mix the C/C++ standard I/O (cout) with the Windows console I/O (SetConsolecursorPosition()) the results are unpredictable.  if yoiu need to use the windows console functions, that you should only use windows console functions.


Here are some functions you can use to help you output to the console through the windows console functions


enum Color
{
   Red,
   Green,
   Blue,
   Yellow,
   Purple,
   Cyan,  
   White,
   Black
};

void GotoXY(int X,int Y)
{
      HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD Coord = {X,Y};
      SetConsoleCursorPosition(StdOut,Coord);
}
void OutputStr(const char *S)
{
      HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      int StrLen = strlen(S);
      DWORD LenWrt;
      WriteConsole(StdOut,S,StrLen,&LenWrt,NULL);
}
void OutputStr(int X,int Y,const char *S)
{
      GotoXY(X,Y);
      OutputStr(S);
}

void ClearScreen()
{
   HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_SCREEN_BUFFER_INFO BufInf;
   COORD Origin = {0,0};
   DWORD LenWrt;

   GetConsoleScreenBufferInfo(StdOut,&BufInf); // Get screen rows and columns.
 
   int ChrCnt = BufInf.dwSize.X * BufInf.dwSize.Y; // Number of chars on screen.
   
   FillConsoleOutputAttribute(StdOut,0,ChrCnt,Origin,&LenWrt);
   FillConsoleOutputCharacter(StdOut,' ',ChrCnt,Origin,&LenWrt);
}
void SetColor(Color TxtCol,Color BckCol)
{
   HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   WORD   Col    = 0;

   switch (TxtCol)
   {
   case Red:    Col |= FOREGROUND_RED;   break;
   case Green:  Col |= FOREGROUND_GREEN; break;
   case Blue:   Col |= FOREGROUND_BLUE;  break;
   case Yellow: Col |= FOREGROUND_RED | FOREGROUND_GREEN;   break;
   case Purple: Col |= FOREGROUND_RED | FOREGROUND_BLUE;    break;
   case Cyan:   Col |= FOREGROUND_GREEN | FOREGROUND_BLUE;  break;
   case White:  Col |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
   }
   switch (BckCol)
   {
   case Red:    Col |= BACKGROUND_RED;   break;
   case Green:  Col |= BACKGROUND_GREEN; break;
   case Blue:   Col |= BACKGROUND_BLUE;  break;
   case Yellow: Col |= BACKGROUND_RED | BACKGROUND_GREEN;   break;
   case Purple: Col |= BACKGROUND_RED | BACKGROUND_BLUE;    break;
   case Cyan:   Col |= BACKGROUND_GREEN | BACKGROUND_BLUE;  break;
   case White:  Col |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; break;
   }
   SetConsoleTextAttribute(StdOut,Col);
}

Avatar of jumanji

ASKER

I tried using the above method.  While I was able to use clrscr() to make the whole screen blue before.  Now, I can't.  Is there any easy method of making the whole screen blue (for example)?
Avatar of jumanji

ASKER

Adjusted points from 30 to 50
Avatar of jumanji

ASKER

If you could tell me how to make the whole screen one color (blue for example), I'll give you the points.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jumanji

ASKER

Sorry for rejectiong.  Thnx, it works!
No problem, it just tends to cause problems, like what do you do when a 2nd expert comes along and finishes the question.  Then who gets the points?