Chức năng như Format làm việc trong các trình biên dịch cũ như tuabin C và Dev 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 SetColor và ChangeConsoleToColors. 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.
'textcolor' là gì? – haccks
Đây là một chức năng được sử dụng để thay đổi màu chữ. –
Ưu tiên đặt dấu ngắt dòng ở cuối dòng ... 'printf (\ tĐây là ví dụ. \ N \ n"); ' – pmg