2015-04-11 22 views
7

Tôi đang viết một chương trình bằng C. Tôi muốn thay đổi màu văn bản và màu nền trong bảng điều khiển. Chương trình mẫu của tôi là -Làm cách nào để thay đổi màu chữ và màu bảng điều khiển trong mã :: khối?

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

Khi tôi biên dịch mã chương trình này :: khối cho tôi lỗi - textcolor chưa được xác định. Tại sao cái này rất? Tôi làm việc trong trình biên dịch GNU GCC và Windows Vista. Nếu nó không phải là đi để làm việc những gì là bản sao của textcolor. Như thế tôi muốn thay đổi màu nền của giao diện điều khiển. Trình biên dịch cung cấp cho tôi cùng một lỗi chỉ tên của hàm là khác nhau. Làm thế nào để thay đổi màu sắc của giao diện điều khiển và văn bản. Hãy giúp tôi.

Tôi không sao ngay cả khi câu trả lời có trong C++.

+0

'textcolor' là gì? – haccks

+0

Đây là một chức năng được sử dụng để thay đổi màu chữ. –

+0

Ưu tiên đặt dấu ngắt dòng ở cuối dòng ... 'printf (\ tĐây là ví dụ. \ N \ n"); ' – pmg

Trả lời

6

Chức năng như Format làm việc trong các trình biên dịch cũ như tuabin CDev C. Trong các trình biên dịch ngày nay, các chức năng này sẽ không hoạt động. Tôi sẽ cung cấp cho hai chức năng SetColorChangeConsoleToColors. Bạn sao chép dán các mã chức năng này vào chương trình của bạn và thực hiện các bước sau. Mã tôi đang cung cấp sẽ không hoạt động trong một số trình biên dịch.

Mã của setcolor là -

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

Để sử dụng chức năng này bạn cần phải gọi nó là từ chương trình của bạn. Ví dụ: Tôi đang tham gia chương trình mẫu của bạn -

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

Khi bạn chạy chương trình, bạn sẽ nhận được màu văn bản trong RED. Bây giờ tôi sẽ cung cấp cho bạn mã của mỗi màu -

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 

Bây giờ tôi sẽ cung cấp cho mã của ChangeConsoleToColors. Mã này là -

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

Trong chức năng này, bạn chuyển hai số. Nếu bạn muốn màu bình thường chỉ cần đặt số đầu tiên là số không và số thứ hai là màu. ví dụ của tôi là -

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

Trong trường hợp này tôi đã đưa số đầu tiên là zero và số thứ hai là 15 nên giao diện điều khiển màu sắc sẽ có màu trắng như mã cho trắng là 15. Đây là hoạt động đối với tôi trong mã :: khối. Hi vọng nó có ích cho bạn.

1

Đây là một chức năng trực tuyến, tôi đã tạo một tệp tiêu đề với nó, và tôi sử dụng Setcolor(); thay vào đó, tôi hy vọng điều này đã giúp! Bạn có thể thay đổi màu bằng cách chọn bất kỳ màu nào trong khoảng 0-256. :) Đáng buồn thay, tôi tin rằng CodeBlocks có một phiên bản sau của thư viện 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

Bạn nên xác định hàm textcolor trước. Bởi vì Format không phải là một chức năng tiêu chuẩn trong C.

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

' GetStdHandle' và 'SetConsoleTextAttribute' không phải là hàm chuẩn trong C. – pmg

+0

Nhưng' GetStdHandle 'và' SetConsoleTextAttribute' là Windows API – Philokey

+0

Tôi đã sử dụng ví dụ của bạn trước tiên và thử nó trong chương trình của tôi.Độ màu đang đến nhưng có một đường viền xung quanh nó.Không được mong đợi, thay vào đó bạn sẽ thấy câu trả lời của tôi, tốt hơn là –

6

Bạn cũng có thể sử dụng rlutil:

  • đa nền tảng,
  • tiêu đề chỉ (rlutil.h),
  • công trình cho C và C++,
  • thực hiện setColor(), cls(), getch(), gotoxy(), vv .
  • Giấy phép: WTFPL

Mã của bạn sẽ trở thành một cái gì đó như thế này:

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

Có một cái nhìn tại example.ctest.cpp cho ví dụ C và C++.

+0

. thư viện khá đẹp, chỉ mất 5 phút để tinh chỉnh nó để làm việc với chế độ unicode của Visual Studio và hoạt động tốt. – Neutrino

0

thiệu một cảch dễ tiếp cận ...

system("Color F0"); 

Thư Đại diện cho màu nền trong khi số đại diện cho màu chữ.

0 = Đen

1 = Xanh

2 = Xanh

3 = Aqua

4 = Red

5 = Tím

6 = Vàng

7 = Trắng

8 = Grey

9 = Light Blue

A = Ánh sáng xanh

B = Ánh sáng Aqua

C = Ánh sáng đỏ

D = Màu tím nhạt

E = Li ght Yellow

F = Màu trắng sáng

Các vấn đề liên quan