PDA

Visualizza la versione completa : testo colorato in C


Alex'87
03-10-2003, 15:19
in un gioco dos si pu cambiare colore al testo (o cambiare il carattere)?

se si come?

ty

cour
03-10-2003, 19:26
Certo.
ho fatto la stessa domanda in un altro forum una volta e la risposta
stata nella libreria conio.h; nello specifico devi utilizzare la funzione textcolor();
Ciao

Johnny_Depp
04-10-2003, 18:24
il problema che <conio.h> non appartiene alla libreria dello standard C. (quindi potresti non trovarlo nella release di C che stai utilizzando)

Potresti sempre crearti un header ad hoc, da includere nei tuoi progetti C/C++.


//name "format_out.h"

#include <windows.h>

// gotxy() gestisce il cursore

void gotoxy(int x, int y)
{
COORD point;
point.X = x; point.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), point);
}





// setrgb() gestisce il colore bg/fg

void setrgb(int color)
{
switch (color)
{
case 0: // White on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
break;

case 1: // Red on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED);
break;

case 2: // Green on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN);
break;

case 3: // Yellow on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN);
break;

case 4: // Blue on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_BLUE);
break;

case 5: // Magenta on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_BLUE);
break;

case 6: // Cyan on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN |
FOREGROUND_BLUE);
break;

case 7: // Black on Gray
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | BACKGROUND_INTENSITY);
break;

case 8: // Black on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE);
break;

case 9: // Red on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_RED);
break;

case 10: // Green on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_GREEN);
break;

case 11: // Yellow on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_RED |
FOREGROUND_GREEN);
break;

case 12: // Blue on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_BLUE);
break;

case 13: // Magenta on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_RED |
FOREGROUND_BLUE);
break;

case 14: // Cyan on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_GREEN |
FOREGROUND_BLUE);
break;

case 15: // White on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
break;

default : // White on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
}
}










// clrscr() pulisce lo schermo

void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}

Alex'87
05-10-2003, 19:41
mi spieghi meglio cosa devo fare?
quello che ai inserito e' il file che devo slavare in ***.h?

e colorare il testo?

in dev-c++ c CONIO.H...
c
ome funzia textcolor?

ty

Johnny_Depp
06-10-2003, 10:42
salvalo con il nome che preferisci,
basta che poi, quando lo vai ad includere tra gli header
ti ricordi come lo hai chiamato :D (a me piace chiamarlo "format_out.h")

Si, Dev C++ ti permette di utilizzare <conio> (che comprende le stesse funzioni inserite nel file che ho "postato" sopra).

textcolor() si usa nel seguente modo:

#include <iostream>
#include <stdlib.h>
#include <conio.c>
using namespace std;
int main()
{
gotoxy(30,10); // coordinate X Y dello schermo DOS
textcolor(LIGHTGREEN); // verde chiaro == textcolor(10);
cprintf("HELLO WORLD\n\n");
textcolor(RED);
system("PAUSE");
return 0;
}
Questi sono i colori utilizzabili.

BLACK,
BLUE,
GREEN,
CYAN,
RED,
MAGENTA,
BROWN,
LIGHTGRAY,
DARKGRAY,
LIGHTBLUE,
LIGHTGREEN,
LIGHTCYAN,
LIGHTRED,
LIGHTMAGENTA,
YELLOW,
WHITE

Alex'87
06-10-2003, 21:17
in C devo cambiare iostream con stdio e basta?

Johnny_Depp
06-10-2003, 21:19
YES :adhone:

togli <iostream> e anche

using namespace std;

Loading