printf如何输出颜色?在windows下
printf怎么输出颜色?在windows下
[0;36mcurrent function is main [0;32;32m file line is 20
[m[0;32;31mcurrent function is main [0;32;34m file line is 21
[m请按任意键继续. . .
上面的好像只在LINUX下有效?
到底该怎么输出颜色?
------解决方案--------------------
输出颜色不是printf能做的事情,用setcolor函数,函数原型如下
#include <graphics.h>
#include <conio.h>
void setcolor(int color) // 功能:将当前图形屏幕的当前笔画颜色置为color.
------解决方案--------------------
这个是linux的格式,你是在linux上么
------解决方案--------------------
- C/C++ code
#define NONE "\033[m" #define RED "\033[0;32;31m" #define LIGHT_RED "\033[1;31m" #define GREEN "\033[0;32;32m" #define LIGHT_GREEN "\033[1;32m" #define BLUE "\033[0;32;34m" #define LIGHT_BLUE "\033[1;34m" #define DARY_GRAY "\033[1;30m" #define CYAN "\033[0;36m" #define LIGHT_CYAN "\033[1;36m" #define PURPLE "\033[0;35m" #define LIGHT_PURPLE "\033[1;35m" #define BROWN "\033[0;33m" #define YELLOW "\033[1;33m" #define LIGHT_GRAY "\033[0;37m" #define WHITE "\033[1;37m" printf("\033[31m ####----->> \033[32m" "hello\n" "\033[m") int main() { printf( CYAN "current function is %s " GREEN " file line is %d\n" NONE,__FUNCTION__, __LINE__ ); fprintf(stderr, RED "current function is %s " BLUE " file line is %d\n" NONE,__FUNCTION__, __LINE__ ); return 0; }
[0;36mcurrent function is main [0;32;32m file line is 20
[m[0;32;31mcurrent function is main [0;32;34m file line is 21
[m请按任意键继续. . .
上面的好像只在LINUX下有效?
到底该怎么输出颜色?
------解决方案--------------------
输出颜色不是printf能做的事情,用setcolor函数,函数原型如下
#include <graphics.h>
#include <conio.h>
void setcolor(int color) // 功能:将当前图形屏幕的当前笔画颜色置为color.
------解决方案--------------------
这个是linux的格式,你是在linux上么
------解决方案--------------------
- C/C++ code
#include "stdio.h" #include "windows.h" void set_console_color(unsigned short color_index) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_index); } int main() { set_console_color(6); printf("%s", "hello"); set_console_color(9); printf("%s\n", " world"); return 0; }
------解决方案--------------------
啊,又是“字符界面”问题,楼主你用的这个叫转义字符,以前有弄过,
在linux等终端下是可以显示彩色字符的,但是在window下的那个终端就是
显示不了,不支持的,所以此路不通(不要疑问,就是这样的)。
要在"晕倒死"下面显示彩色字符的话
一种:使用老古董“TC”或者"BC",
它有个头文件叫conio.h,然后就可以调用如textcolor、textbackground等函数了,
但是这个头文件在Windows系列中例如(VC6等),虽然也有这个头文件,但是
你若是打开看看的话,里面是不支持这些函数;
二种:使用如3楼所示的Windows支持的console终端函数,方法多了去了,至于怎么用,
可以参考这个:
http://download.****.net/detail/unnamedterriest/2604163
已将TC下的那个conio.h移植到了VC6下,然后就是你的事了(不好意思,需要积分,
我也不知道怎么将下载需要积分的给取消,实在不行,给我留言,我发给你)。Good luck。
------解决方案--------------------
3L正解
------解决方案--------------------
控制台下好像有个setcolor函数吧,具体不清楚了
------解决方案--------------------
搜ANSI.SYS
另外参考下面:
- C/C++ code
#include <windows.h> #include <stdio.h> void ConPrint(char *CharBuffer, int len); void ConPrintAt(int x, int y, char *CharBuffer, int len); void gotoXY(int x, int y); void ClearConsole(void); void ClearConsoleToColors(int ForgC, int BackC); void SetColorAndBackground(int ForgC, int BackC); void SetColor(int ForgC); void HideTheCursor(void); void ShowTheCursor(void); int main(int argc, char* argv[]) { HideTheCursor(); ClearConsoleToColors(15, 1); ClearConsole(); gotoXY(1, 1); SetColor(14); printf("This is a test...\n"); Sleep(5000); ShowTheCursor(); SetColorAndBackground(15, 12); ConPrint("This is also a test...\n", 23); SetColorAndBackground(1, 7); ConPrintAt(22, 15, "This is also a test...\n", 23); gotoXY(0, 24); SetColorAndBackground(7, 1); return 0; } //This will clear the console while setting the forground and //background colors. 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); } } //This will clear the console. void ClearConsole() { //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 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); } } //This will set the position of the cursor void gotoXY(int x, int y) { //Initialize the coordinates COORD coord = {x, y}; //Set the position SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } //This will set the forground color for printing in a console window. void SetColor(int ForgC) { WORD wColor; //We will need this handle to get the current background attribute 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); } } //This will set the forground and background color for printing in a console window. void SetColorAndBackground(int ForgC, int BackC) { WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor); } //Direct console output void ConPrint(char *CharBuffer, int len) { DWORD count; WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL); } //Direct Console output at a particular coordinate. void ConPrintAt(int x, int y, char *CharBuffer, int len) { DWORD count; COORD coord = {x, y}; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hStdOut, coord); WriteConsole(hStdOut, CharBuffer, len, &count, NULL); } //Hides the console cursor void HideTheCursor() { CONSOLE_CURSOR_INFO cciCursor; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if(GetConsoleCursorInfo(hStdOut, &cciCursor)) { cciCursor.bVisible = FALSE; SetConsoleCursorInfo(hStdOut, &cciCursor); } } //Shows the console cursor void ShowTheCursor() { CONSOLE_CURSOR_INFO cciCursor; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if(GetConsoleCursorInfo(hStdOut, &cciCursor)) { cciCursor.bVisible = TRUE; SetConsoleCursorInfo(hStdOut, &cciCursor); } }