2015-04-11 17 views
7

Sto scrivendo un programma in C. Voglio cambiare il colore del testo e il colore di sfondo nella console. Il mio programma di esempio è -Come modificare il colore del testo e il colore della console in code :: blocks?

#include <stdio.h> 
#include <stdlib.h> 
#include <windows.h> 
#include <dos.h> 
#include <dir.h> 

int main(int argc,char *argv[]) 
{ 
textcolor(25); 
printf("\n \n \t This is dummy program for text color "); 
getch(); 

return 0; 
} 

Quando compilo il codice del programma :: blocchi mi dà un errore - textcolor non definito. Perché è così? Lavoro in un compilatore GCC GNU e Windows Vista. Se non funziona, qual è il duplicato di textcolor. Ad esempio, voglio cambiare il colore di sfondo della console. Il compilatore mi dà lo stesso errore, solo il nome della funzione è diverso. Come cambiare il colore della console e del testo. Per favore aiuto.

Sono d'accordo anche se la risposta è in C++.

+0

Che cos'è 'textcolor'? – haccks

+0

È una funzione utilizzata per modificare il colore del testo. –

+0

Preferisci inserire interruzioni di riga alla fine delle righe ... 'printf (\ tQuesto è un esempio. \ N \ n"); ' – pmg

risposta

6

stessa funzione textcolor lavorato in vecchi compilatori come turbo C e Dev C. Nei compilatori di oggi queste funzioni non funzionerebbero. Ho intenzione di dare due funzioni SetColor e ChangeConsoleToColors. Si copia incolla il codice di queste funzioni nel programma e si eseguono i seguenti passaggi. Il codice che sto dando non funzionerà in alcuni compilatori.

Il codice di SetColor è -

void SetColor(int ForgC) 
{ 
    WORD wColor; 

     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 
     CONSOLE_SCREEN_BUFFER_INFO csbi; 

         //We use csbi for the wAttributes word. 
    if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) 
    { 
       //Mask out all but the background attribute, and add in the forgournd  color 
      wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); 
      SetConsoleTextAttribute(hStdOut, wColor); 
    } 
    return; 
} 

Per utilizzare questa funzione è necessario chiamare dal programma. Per esempio sto prendendo il tuo programma di esempio -

#include <stdio.h> 
#include <stdlib.h> 
#include <windows.h> 
#include <dos.h> 
#include <dir.h> 

int main(void) 
{ 
    SetColor(4); 
    printf("\n \n \t This text is written in Red Color \n "); 
    getch(); 
    return 0; 
} 

void SetColor(int ForgC) 
{ 
WORD wColor; 

    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 
    CONSOLE_SCREEN_BUFFER_INFO csbi; 

         //We use csbi for the wAttributes word. 
if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) 
{ 
       //Mask out all but the background attribute, and add in the forgournd color 
     wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); 
     SetConsoleTextAttribute(hStdOut, wColor); 
} 
return; 
} 

Quando si esegue il programma, si otterrà il colore del testo in ROSSO. Ora sto per darvi il codice di ogni colore -

Name   | Value 
      | 
Black  | 0 
Blue   | 1 
Green  | 2 
Cyan   | 3 
Red   | 4 
Magenta  | 5 
Brown  | 6 
Light Gray | 7 
Dark Gray | 8 
Light Blue | 9 
Light Green | 10 
Light Cyan | 11 
Light Red | 12 
Light Magenta| 13 
Yellow  | 14 
White  | 15 

Ora ho intenzione di dare il codice di ChangeConsoleToColors. Il codice è -

void ClearConsoleToColors(int ForgC, int BackC) 
{ 
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F); 
       //Get the handle to the current output buffer... 
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 
        //This is used to reset the carat/cursor to the top left. 
COORD coord = {0, 0}; 
        //A return value... indicating how many chars were written 
        // not used but we need to capture this since it will be 
         // written anyway (passing NULL causes an access violation). 
    DWORD count; 

           //This is a structure containing all of the console info 
         // it is used here to find the size of the console. 
CONSOLE_SCREEN_BUFFER_INFO csbi; 
       //Here we will set the current color 
SetConsoleTextAttribute(hStdOut, wColor); 
if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) 
{ 
          //This fills the buffer with a given character (in this case 32=space). 
     FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count); 

     FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count); 
          //This will set our cursor position for the next print statement. 
     SetConsoleCursorPosition(hStdOut, coord); 
} 
return; 
} 

In questa funzione si passano due numeri. Se vuoi colori normali, metti il ​​primo numero come zero e il secondo numero come colore. Il mio esempio è -

#include <windows.h>   //header file for windows 
#include <stdio.h> 

void ClearConsoleToColors(int ForgC, int BackC); 

int main() 
{ 
ClearConsoleToColors(0,15); 
Sleep(1000); 
return 0; 
} 
void ClearConsoleToColors(int ForgC, int BackC) 
{ 
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F); 
       //Get the handle to the current output buffer... 
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 
        //This is used to reset the carat/cursor to the top left. 
COORD coord = {0, 0}; 
        //A return value... indicating how many chars were written 
        // not used but we need to capture this since it will be 
         // written anyway (passing NULL causes an access violation). 
DWORD count; 

           //This is a structure containing all of the console info 
         // it is used here to find the size of the console. 
CONSOLE_SCREEN_BUFFER_INFO csbi; 
       //Here we will set the current color 
SetConsoleTextAttribute(hStdOut, wColor); 
if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) 
{ 
          //This fills the buffer with a given character (in this case 32=space). 
     FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count); 

     FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count); 
          //This will set our cursor position for the next print statement. 
     SetConsoleCursorPosition(hStdOut, coord); 
} 
return; 
} 

In questo caso ho messo il primo numero zero e il secondo numero a 15 in modo che il colore della console sarà bianca come il codice per il bianco è 15. Questo è il lavoro per me nel codice :: blocchi. Spero che funzioni anche per te.

1

Questa è una funzione in linea, ho creato un file di intestazione con esso, e io uso Setcolor(); invece, spero che questo ha aiutato! Puoi cambiare il colore scegliendo qualsiasi colore nell'intervallo 0-256. :) Purtroppo, credo che CodeBlocks ha una build successiva della biblioteca window.h ...

#include <windows.h>   //This is the header file for windows. 
#include <stdio.h>    //C standard library header file 

void SetColor(int ForgC); 

int main() 
{ 
    printf("Test color");  //Here the text color is white 
    SetColor(30);    //Function call to change the text color 
    printf("Test color");  //Now the text color is green 
    return 0; 
} 

void SetColor(int ForgC) 
{ 
    WORD wColor; 
    //This handle is needed to get the current background attribute 

    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 
    CONSOLE_SCREEN_BUFFER_INFO csbi; 
    //csbi is used for wAttributes word 

    if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) 
    { 
      //To mask out all but the background attribute, and to add the color 
      wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); 
      SetConsoleTextAttribute(hStdOut, wColor); 
    } 
    return; 
} 
-1

è necessario definire la funzione TextColor prima. Poiché textcolor non è una funzione standard in C.

void textcolor(unsigned short color) { 
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hcon,color); 
} 
+0

' GetStdHandle' e 'SetConsoleTextAttribute' non sono funzioni standard anche in C. – pmg

+0

Ma' GetStdHandle 'e' SetConsoleTextAttribute' sono API di Windows – Philokey

+0

Ho usato il tuo esempio per primo e l'ho provato nel mio programma Il colore sta arrivando ma c'è un bordo intorno Non ci si aspetta Invece vedi la mia risposta È meglio –

6

È inoltre possibile utilizzare rlutil:

  • cross platform,
  • solo intestazione (rlutil.h),
  • lavori per C e C++,
  • implementa setColor(), cls(), getch(), gotoxy(), ecc
  • Licenza: WTFPL

Il tuo codice sarebbe diventato qualcosa di simile:

#include <stdio.h> 

#include "rlutil.h" 

int main(int argc, char* argv[]) 
{ 
    setColor(BLUE); 
    printf("\n \n \t This is dummy program for text color "); 
    getch(); 

    return 0; 
} 

Dai un'occhiata alla example.c e test.cpp per gli esempi C e C++.

+0

Questo è una piccola libreria piuttosto carina, mi ci sono voluti solo 5 minuti per adattarla alla modalità Unicode di Visual Studio e funziona alla grande. – Neutrino

0

un approccio facile ...

system("Color F0"); 

lettera rappresenta il colore di sfondo, mentre il numero rappresenta il colore del testo.

0 = Nero

1 = Blu

2 = verde

3 = Aqua

4 = Rosso

5 = Viola

6 = Giallo

7 = Bianco

8 = Grigio

9 = Light Blue

A = Luce Verde

B = Luce Aqua

C = Red Light

D = Viola chiaro

E = Li ght Yellow

F = Bright White

Problemi correlati