码迷,mamicode.com
首页 > Windows程序 > 详细

Windows DOS 窗口设置字体颜色

时间:2015-04-21 11:07:53      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

Windows DOS 窗口设置字体颜色

标签:

原文地址:http://www.cnblogs.com/felove2013/p/4443800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!