2015-04-11 32 views
7

Piszę program w C. Chcę zmienić kolor tekstu i kolor tła w konsoli. Mój program jest próbka -Jak zmienić kolor tekstu i kolor konsoli w kodzie :: blokuje?

#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; 
} 

Kiedy skompilować ten kod programu :: blocks daje mi błąd - textColor nie określony. Dlaczego tak jest? Pracuję w kompilatorze GNU GCC i systemie Windows Vista. Jeśli nie zadziała to, co jest duplikatem textcolor. W ten sposób chcę zmienić kolor tła konsoli. Kompilator daje mi ten sam błąd, tylko nazwa funkcji jest inna. Jak zmienić kolor konsoli i tekstu. Proszę pomóż.

Jestem w porządku, nawet jeśli odpowiedź jest w C++.

+0

Czym jest "textcolor"? – haccks

+0

Jest to funkcja używana do zmiany koloru tekstu. –

+0

Wolę umieszczać podziały wierszy na końcu linii ... 'printf (\ tTo jest przykład. \ N \ n"); ' – pmg

Odpowiedz

6

funkcje, textColor pracował w starych kompilatorów jak turbo C i Dev C. W dzisiejszych kompilatorach te funkcje nie działają. Mam zamiar nadać dwie funkcje: SetColor i ChangeConsoleToColors. Kopiuj wklej te kody funkcji do swojego programu i wykonaj następujące czynności. Podany kod nie działa w niektórych kompilatorach.

Kodeks setColor jest -

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; 
} 

Aby skorzystać z tej funkcji trzeba zadzwonić ze swojego programu. Na przykład Biorę swój program sample -

#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; 
} 

Po uruchomieniu programu dostaniesz kolor tekstu w kolorze czerwonym. Teraz mam zamiar dać Ci kod każdego koloru -

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 

Teraz mam zamiar dać kod ChangeConsoleToColors. Kod to -

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; 
} 

W tej funkcji podaje się dwie liczby. Jeśli chcesz normalne kolory, po prostu wpisz pierwszą liczbę jako zero, a drugą numer jako kolor. Mój przykład jest -

#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; 
} 

W tym przypadku Włożyłem pierwszy numer jako zero, a druga liczba tak jak 15 kolor konsola będzie biały jak kod dla bieli jest 15. To działa na mnie w kodzie ::Bloki. Mam nadzieję, że to działa również dla ciebie.

1

To jest funkcja online, utworzyłem plik nagłówkowy z nią i używam Setcolor(); zamiast tego, mam nadzieję, że pomogło! Możesz zmienić kolor, wybierając dowolny kolor w zakresie 0-256. :) Niestety, wierzę, że CodeBlocks ma późniejszą kompilację biblioteki 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

Powinieneś wcześniej zdefiniować funkcję textcolor. Textcolor ponieważ nie jest standardowa funkcja w C.

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

' GetStdHandle' i 'SetConsoleTextAttribute' nie są standardowymi funkcjami w C. albo. – pmg

+0

Ale' GetStdHandle 'i' SetConsoleTextAttribute' to Windows API – Philokey

+0

Najpierw użyłem twojego przykładu i wypróbowałem go w moim programie Kolor nadchodzi, ale jest wokół niego obramowanie Nie jest oczekiwane Zamiast tego widzisz moją odpowiedź To jest lepsze –

6

Można również użyć rlutil:

  • wieloplatformowym,
  • nagłówek tylko (rlutil.h)
  • prace dla C i C++,
  • realizuje setColor(), cls(), getch(), gotoxy(), etc
  • Licencja: WTFPL

Twój kod stanie się coś takiego:

#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; 
} 

Wystarczy popatrzeć na example.c i test.cpp dla przykładów C i C++.

+0

To jest lepsze. całkiem fajna mała biblioteka. Zajęło mi to tylko 5 minut, aby dostosować ją do pracy w trybie Unicode Visual Studio i działa świetnie. – Neutrino

0

Easy podejście ...

system("Color F0"); 

List Reprezentuje kolor tła podczas gdy liczba reprezentuje kolor tekstu.

0 = czarny

1 = niebieski

2 = zielony

3 = Aqua

4 = czerwony

5 = purpurowy

6 = żółty

7 = biały

8 = Czarna

9 = Jasnoniebieski

A = jasnozielony

B = światła Aqua

C = światło czerwone

D = Jasnofioletowy

E = Li ght Żółty

F = Jasny biały