printf怎么输出颜色?在windows下
#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;}#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.csdn.net/detail/unnamedterriest/2604163
已将TC下的那个conio.h移植到了VC6下,然后就是你的事了(不好意思,需要积分,
我也不知道怎么将下载需要积分的给取消,实在不行,给我留言,我发给你)。Good luck。
[解决办法]
3L正解
[解决办法]
控制台下好像有个setcolor函数吧,具体不清楚了
[解决办法]
搜ANSI.SYS
另外参考下面:
#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 cursorvoid 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 outputvoid 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 cursorvoid HideTheCursor(){ CONSOLE_CURSOR_INFO cciCursor; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if(GetConsoleCursorInfo(hStdOut, &cciCursor)) { cciCursor.bVisible = FALSE; SetConsoleCursorInfo(hStdOut, &cciCursor); }}//Shows the console cursorvoid ShowTheCursor(){ CONSOLE_CURSOR_INFO cciCursor; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if(GetConsoleCursorInfo(hStdOut, &cciCursor)) { cciCursor.bVisible = TRUE; SetConsoleCursorInfo(hStdOut, &cciCursor); }}
[解决办法]
LZ的用的VT100转义序列是是UNIX/Linux的终端才支持的。
Windows下要显示彩色有三个办法,调用ANSI.SYS:
WINNT核心的命令提示符默认没有加载ANSI,按这个设置:
http://support.microsoft.com/kb/101875
之后就可以用ANSI转义序列了,可以方便切换模式和颜色:
http://en.wikipedia.org/wiki/ANSI_escape_sequence
二是用Turbo C或者Borland C/C++,也可以试试DJGPP,它们有相应的可以调用BIOS显示服务进行高级显示的库,比如前者是conio.h之类的。
第三就是使用Console API,像LS那样,注意最后一种,需要使用VC/LCC-Win32等开发工具建立Win32控制台程序,不能用16位开发工具。