تلوين النص في وحدة التحكم مع C ++
سؤال
كيف يمكنني كتابة نص ملون إلى وحدة التحكم مع C ++؟ هذا هو ، كيف يمكنني كتابة نص مختلف بألوان مختلفة؟
المحلول
أضف القليل من اللون إلى نص وحدة التحكم الخاصة بك
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// you can loop k higher to see more color choices
for(int k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}
سمات الشخصيةإليكم كيف يتم تفسير قيمة "K".
نصائح أخرى
ليس لدى C ++ القياسي أي فكرة عن "الألوان". إذن ما تطلبه يعتمد على نظام التشغيل.
لنظام التشغيل Windows ، يمكنك التحقق من setConsoleTextAttribute وظيفة.
على *nix ، عليك استخدام أنسي تسلسلات الهروب.
رموز ألوان ANSI Escape:
Name BG FG
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47
Bright Black 90 100
Bright Red 91 101
Bright Green 92 102
Bright Yellow 93 103
Bright Blue 94 104
Bright Magenta 95 105
Bright Cyan 96 106
Bright White 97 107
نموذج رمز لـ C/C ++:
#include <iostream>
#include <string>
int main(int argc, char ** argv){
printf("\n");
printf("\x1B[31mTexting\033[0m\t\t");
printf("\x1B[32mTexting\033[0m\t\t");
printf("\x1B[33mTexting\033[0m\t\t");
printf("\x1B[34mTexting\033[0m\t\t");
printf("\x1B[35mTexting\033[0m\n");
printf("\x1B[36mTexting\033[0m\t\t");
printf("\x1B[36mTexting\033[0m\t\t");
printf("\x1B[36mTexting\033[0m\t\t");
printf("\x1B[37mTexting\033[0m\t\t");
printf("\x1B[93mTexting\033[0m\n");
printf("\033[3;42;30mTexting\033[0m\t\t");
printf("\033[3;43;30mTexting\033[0m\t\t");
printf("\033[3;44;30mTexting\033[0m\t\t");
printf("\033[3;104;30mTexting\033[0m\t\t");
printf("\033[3;100;30mTexting\033[0m\n");
printf("\033[3;47;35mTexting\033[0m\t\t");
printf("\033[2;47;35mTexting\033[0m\t\t");
printf("\033[1;47;35mTexting\033[0m\t\t");
printf("\t\t");
printf("\n");
return 0;
}
مجلس التعاون الخليجي :
g++ cpp_interactive_terminal.cpp -o cpp_interactive_terminal.cgi
chmod +x cpp_interactive_terminal.cgi
./cpp_interactive_terminal.cgi
يمكنك كتابة الأساليب والاتصال مثل هذا
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int col=12;
// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
FlushConsoleInputBuffer(hConsole);
SetConsoleTextAttribute(hConsole, col);
cout << "Color Text";
SetConsoleTextAttribute(hConsole, 15); //set back to black background and white text
على افتراض أنك تتحدث عن نافذة وحدة تحكم Windows ، ابحث عن وظائف وحدة التحكم في وثائق مكتبة MSDN.
خلاف ذلك ، أو بشكل عام ، يعتمد على وحدة التحكم. لا يتم دعم الألوان بواسطة مكتبة C ++. لكن مكتبة لمعالجة وحدة التحكم قد تدعم الألوان. على سبيل المثال جوجل "ألوان ncurses".
بالنسبة للمحطات التسلسلية المتصلة والمحاكيات الطرفية ، يمكنك التحكم في الأشياء عن طريق إخراج "تسلسل الهروب". هذه عادة ما تبدأ مع ASCII 27 (شخصية الهروب في ASCII). هناك معيار ANSI والكثير من المخططات المخصصة.
لست متأكدًا مما تريد فعله حقًا ، لكن تخميني هل تريد أن يخرج برنامج C ++ الخاص بك نصًا ملونًا في وحدة التحكم ، أليس كذلك؟ لا أعرف عن Windows ، ولكن على جميع الوحدات (بما في ذلك Mac OS X) ، يمكنك ببساطة استخدامها ANSI هروب تسلسل من أجل هذا.
في Windows ، يمكنك استخدام أي مجموعة من الأخضر الأحمر والأزرق على المقدمة (النص) والخلفية.
/* you can use these constants
FOREGROUND_BLUE
FOREGROUND_GREEN
FOREGROUND_RED
FOREGROUND_INTENSITY
BACKGROUND_BLUE
BACKGROUND_GREEN
BACKGROUND_RED
BACKGROUND_INTENSITY
*/
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "I'm cyan! Who are you?" << std::endl;
في Windows 10 ، يمكنك استخدام تسلسل الهروب بهذه الطريقة:
#ifdef _WIN32
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
// print in red and restore colors default
std::cout << "\033[32m" << "Error!" << "\033[0m" << std::endl;
أبسط طريقة يمكنك القيام بها هي:
#include <stdlib.h>
system("Color F3");
حيث "F" هو رمز لون الخلفية و 3 هو رمز لون النص.
تعبث معها لرؤية مجموعات ألوان أخرى:
system("Color 1A");
cout << "Hello, what is your name?" << endl;
system("Color 3B");
cout << "Hello, what is your name?" << endl;
sytem("Color 4c");
cout << "Hello, what is your name?" << endl;
لا تستخدم "نظام (" اللون ... ")" إذا كنت لا تريد ملء الشاشة بأكملها بالألوان. هذا هو البرنامج النصي اللازم لصنع نص ملون:
#include <iostream>
#include <windows.h>
int main()
{
const WORD colors[] =
{
0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
};
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
WORD index = 0;
SetConsoleTextAttribute(hstdout, colors[index]);
std::cout << "Hello world" << std::endl;
FlushConsoleInputBuffer(hstdin);
return 0;
}
هنا مثال cplusplus هو مثال على كيفية استخدام الألوان في وحدة التحكم.