Windows DOS 窗口设置字体颜色

2015-04-21 10:54:43

 1 #include <windows.h>
 2 #include <iostream>
 3 using namespace std;
 4 #define Color_count    15
 5 
 6 //void SetColor(unsigned short ForeColor = 3, unsigned short BackgroundColor = 0)
 7 void SetColor(int color)
 8 {
 9     if(color < 0 || color >= 15)
10         return;
11 
12     static WORD WinColorFG[Color_count] =
13     {
14         0,                                                  // BLACK
15         FOREGROUND_RED,                                     // RED
16         FOREGROUND_GREEN,                                   // GREEN
17         FOREGROUND_RED | FOREGROUND_GREEN,                  // BROWN
18         FOREGROUND_BLUE,                                    // BLUE
19         FOREGROUND_RED |FOREGROUND_BLUE,                    // MAGENTA
20         FOREGROUND_GREEN | FOREGROUND_BLUE,                 // CYAN
21         FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,// WHITE
22         FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,// YELLOW
23         FOREGROUND_RED | FOREGROUND_INTENSITY,                 // RED_BOLD
24         FOREGROUND_GREEN |  FOREGROUND_INTENSITY,            // GREEN_BOLD
25         FOREGROUND_BLUE | FOREGROUND_INTENSITY,             // BLUE_BOLD
26         FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,// MAGENTA_BOLD
27         FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,// CYAN_BOLD
28         FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY // WHITE_BOLD
29     };
30     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
31     SetConsoleTextAttribute(hConsole, WinColorFG[color]);
32     /*SetConsoleTextAttribute(hConsole, ForeColor | BackgroundColor);*/
33 }
34 
35 int main()
36 {
37     for(int i = 0; i < 15; ++i)
38     {
39         SetColor(i);
40         cout<<"Hello World!"<<endl;
41     }
42     /*SetColor();
43     cout<<"Hello World!"<<endl;
44     SetColor(FOREGROUND_RED);
45     cout<<"Hello World!"<<endl;
46     cout<<"Hello World!"<<endl;*/
47     system("pause");
48     return 0;
49 }

 

posted @ 2015-04-21 10:55  felove  阅读(1299)  评论(0编辑  收藏  举报