-
Text Function
I've looked at other posts about this. Also, I searched the board. Usally I'll find what I'm looking for. But, almost always I won't understand it =/. Well, I want to know how to write a function so I can easily use text colors without have to write it all out over and over. Or at least tell me a thread that explains it really good. I just don't understand other threads.
- SirCrono6
-
Should we assume you are outputting the text to a console window and your target OS is MS Windows? [maybe you should tell us]
-
Windows ME
Dev-C++ 4.9.8.0
Console
So, yeah :D
- SirCrono6
-
-
No. I mean a function (something I can call) that will make it easier to call colors. Instead of:
Code:
SetConsoleTextAttribute(hOut, FOREGROUND_RED);
something like:
Code:
TextColor(hOut, F_Red);
or something like that. I didn't find other posts of any help. I can't think of a way to do this without like a million if / else if statements.
- SirCrono6
-
If your compiler doesn't provide one, just make one yourself to encapsulate the necessary code.
Code:
void myWriteColoredTextFunction(char *text, int FG, int BG)
{
// Code goes here
}
-
I wrote this class a while ago, it uses windows and colours and stuff. It may be useful to ya or not. Hope it is. It is not standard compliant neither.
Code:
#include <iostream.h>
#include <conio.h>
class TWindow
{
int Left, Top, Right, Bottom, NumWindows, TxtColour, WinColour;
char *Title;
public:
TWindow(int, int, int, int, int, int);
void Draw(void);
void SetWinSize(int, int, int, int);
void SetTitle(char *TitlePtr, int TextCol)
{strcpy(Title = new char[strlen(TitlePtr) + 1], TitlePtr);
TxtColour = TextCol;}
void SetWinColour(int Colour){WinColour = Colour;}
~TWindow(){delete []Title;}
};
TWindow::TWindow(int WinLeft = 1, int WinTop = 1, int WinRight = 80,
int WinBottom = 25 , int FontColour = WHITE,
int BackColour = BLUE)
{
strcpy(Title = new char[20], "Default Window");
Left = WinLeft;
Top = WinTop;
Right = WinRight;
Bottom = WinBottom;
TxtColour = FontColour;
WinColour = BackColour;
}
void TWindow::Draw(void)
{
int Temp;
window(Left, Top, Right, Bottom);
textbackground(WinColour);
textcolor(TxtColour);
for(int i=0; i < Bottom; i++)
for(int j=0; j < Right; j++)
cprintf(" ");
Temp = (((Right - Left) - strlen(Title)) + 1)/ 2 ;
gotoxy(++Temp, 1);
cprintf("%s", Title);
}
void TWindow::SetWinSize(int WinLeft, int WinTop, int WinRight, int WinBottom)
{
Left = WinLeft;
Top = WinTop;
Right = WinRight;
Bottom = WinBottom;
}
int main(void)
{
TWindow Win1;
Win1.Draw();
TWindow Win2(5, 5, 60, 18, YELLOW, GREEN);
Win2.Draw();
TWindow Win3(8, 8, 40, 14);
Win3.Draw();
Win1.SetWinColour(RED);
Win1.SetWinSize(10, 9, 35, 9);
Win1.SetTitle("Red Window", LIGHTGRAY);
Win1.Draw();
getchar();
return 0;
}
-
Here's a nice header written by me.
It allows code of the form:
Code:
using namespace Petter;
cout << RED << "This is red." << BLUE << "This is blue.";
And it's ANSI/ISO compatible. :) But you won't see any color unless compiling for Win32.