在学习编程时,经常拿控制台程序联系,还有在编写算法题时也经常对着黑底白字的控制台窗口,看一连串的分析数据,着实眼疲劳不说,也没有创作的激情,
现在提供一个简单的控制台颜色修改方案:
Console.h
1: //------------------------------------------------------------------------------
2: // Console.h: interface for the Console manipulators.
3: //------------------------------------------------------------------------------
4:
5: #if !defined( CONSOLE_MANIP_H__INCLUDED )
6: #define CONSOLE_MANIP_H__INCLUDED
7:
8: //------------------------------------------------------------------------------
9:
10: //------------------------------------------------------------------"includes"--
11: #include <;iostream>
12: #include <;iomanip>
13: #include <;windows.h>
14:
15: namespace JadedHoboConsole
16: {
17: static const WORD bgMask( BACKGROUND_BLUE |
18: BACKGROUND_GREEN |
19: BACKGROUND_RED |
20: BACKGROUND_INTENSITY );
21: static const WORD fgMask( FOREGROUND_BLUE |
22: FOREGROUND_GREEN |
23: FOREGROUND_RED |
24: FOREGROUND_INTENSITY );
25:
26: static const WORD fgBlack ( 0 );
27: static const WORD fgLoRed ( FOREGROUND_RED );
28: static const WORD fgLoGreen ( FOREGROUND_GREEN );
29: static const WORD fgLoBlue ( FOREGROUND_BLUE );
30: static const WORD fgLoCyan ( fgLoGreen | fgLoBlue );
31: static const WORD fgLoMagenta( fgLoRed | fgLoBlue );
32: static const WORD fgLoYellow ( fgLoRed | fgLoGreen );
33: static const WORD fgLoWhite ( fgLoRed | fgLoGreen | fgLoBlue );
34: static const WORD fgGray ( fgBlack | FOREGROUND_INTENSITY );
35: static const WORD fgHiWhite ( fgLoWhite | FOREGROUND_INTENSITY );
36: static const WORD fgHiBlue ( fgLoBlue | FOREGROUND_INTENSITY );
37: static const WORD fgHiGreen ( fgLoGreen | FOREGROUND_INTENSITY );
38: static const WORD fgHiRed ( fgLoRed | FOREGROUND_INTENSITY );
39: static const WORD fgHiCyan ( fgLoCyan | FOREGROUND_INTENSITY );
40: static const WORD fgHiMagenta( fgLoMagenta | FOREGROUND_INTENSITY );
41: static const WORD fgHiYellow ( fgLoYellow | FOREGROUND_INTENSITY );
42: static const WORD bgBlack ( 0 );
43: static const WORD bgLoRed ( BACKGROUND_RED );
44: static const WORD bgLoGreen ( BACKGROUND_GREEN );
45: static const WORD bgLoBlue ( BACKGROUND_BLUE );
46: static const WORD bgLoCyan ( bgLoGreen | bgLoBlue );
47: static const WORD bgLoMagenta( bgLoRed | bgLoBlue );
48: static const WORD bgLoYellow ( bgLoRed | bgLoGreen );
49: static const WORD bgLoWhite ( bgLoRed | bgLoGreen | bgLoBlue );
50: static const WORD bgGray ( bgBlack | BACKGROUND_INTENSITY );
51: static const WORD bgHiWhite ( bgLoWhite | BACKGROUND_INTENSITY );
52: static const WORD bgHiBlue ( bgLoBlue | BACKGROUND_INTENSITY );
53: static const WORD bgHiGreen ( bgLoGreen | BACKGROUND_INTENSITY );
54: static const WORD bgHiRed ( bgLoRed | BACKGROUND_INTENSITY );
55: static const WORD bgHiCyan ( bgLoCyan | BACKGROUND_INTENSITY );
56: static const WORD bgHiMagenta( bgLoMagenta | BACKGROUND_INTENSITY );
57: static const WORD bgHiYellow ( bgLoYellow | BACKGROUND_INTENSITY );
58:
59: static class con_dev
60: {
61: private:
62: HANDLE hCon;
63: DWORD cCharsWritten;
64: CONSOLE_SCREEN_BUFFER_INFO csbi;
65: DWORD dwConSize;
66:
67: public:
68: con_dev()
69: {
70: hCon = GetStdHandle( STD_OUTPUT_HANDLE );
71: }
72: private:
73: void GetInfo()
74: {
75: GetConsoleScreenBufferInfo( hCon, &;csbi );
76: dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
77: }
78: public:
79: void Clear()
80: {
81: COORD coordScreen = { 0, 0 };
82:
83: GetInfo();
84: FillConsoleOutputCharacter( hCon, TEXT(' '),
85: dwConSize,
86: coordScreen,
87: &;cCharsWritten );
88: GetInfo();
89: FillConsoleOutputAttribute( hCon,
90: csbi.wAttributes,
91: dwConSize,
92: coordScreen,
93: &;cCharsWritten );
94: SetConsoleCursorPosition( hCon, coordScreen );
95: }
96: void SetColor( WORD wRGBI, WORD Mask )
97: {
98: GetInfo();
99: csbi.wAttributes &= Mask;
100: csbi.wAttributes |= wRGBI;
101: SetConsoleTextAttribute( hCon, csbi.wAttributes );
102: }
103: } console;
104:
105: inline std::ostream& clr( std::ostream& os )
106: {
107: os.flush();
108: console.Clear();
109: return os;
110: };
111:
112: inline std::ostream&; fg_red( std::ostream& os )
113: {
114: os.flush();
115: console.SetColor( fgHiRed, bgMask );
116:
117: return os;
118: }
119:
120: inline std::ostream&; fg_green( std::ostream& os )
121: {
122: os.flush();
123: console.SetColor( fgHiGreen, bgMask );
124:
125: return os;
126: }
127:
128: inline std::ostream&; fg_blue( std::ostream& os )
129: {
130: os.flush();
131: console.SetColor( fgHiBlue, bgMask );
132:
133: return os;
134: }
135:
136: inline std::ostream&; fg_white( std::ostream& os )
137: {
138: os.flush();
139: console.SetColor( fgHiWhite, bgMask );
140:
141: return os;
142: }
143:
144: inline std::ostream&; fg_cyan( std::ostream& os )
145: {
146: os.flush();
147: console.SetColor( fgHiCyan, bgMask );
148:
149: return os;
150: }
151:
152: inline std::ostream&; fg_magenta( std::ostream& os )
153: {
154: os.flush();
155: console.SetColor( fgHiMagenta, bgMask );
156:
157: return os;
158: }
159:
160: inline std::ostream&; fg_yellow( std::ostream& os )
161: {
162: os.flush();
163: console.SetColor( fgHiYellow, bgMask );
164:
165: return os;
166: }
167:
168: inline std::ostream&; fg_black( std::ostream& os )
169: {
170: os.flush();
171: console.SetColor( fgBlack, bgMask );
172:
173: return os;
174: }
175:
176: inline std::ostream&; fg_gray( std::ostream& os )
177: {
178: os.flush();
179: console.SetColor( fgGray, bgMask );
180:
181: return os;
182: }
183:
184: inline std::ostream&; bg_red( std::ostream& os )
185: {
186: os.flush();
187: console.SetColor( bgHiRed, fgMask );
188:
189: return os;
190: }
191:
192: inline std::ostream&; bg_green( std::ostream& os )
193: {
194: os.flush();
195: console.SetColor( bgHiGreen, fgMask );
196:
197: return os;
198: }
199:
200: inline std::ostream&; bg_blue( std::ostream& os )
201: {
202: os.flush();
203: console.SetColor( bgHiBlue, fgMask );
204:
205: return os;
206: }
207:
208: inline std::ostream&; bg_white( std::ostream& os )
209: {
210: os.flush();
211: console.SetColor( bgHiWhite, fgMask );
212:
213: return os;
214: }
215:
216: inline std::ostream&; bg_cyan( std::ostream& os )
217: {
218: os.flush();
219: console.SetColor( bgHiCyan, fgMask );
220:
221: return os;
222: }
223:
224: inline std::ostream&; bg_magenta( std::ostream& os )
225: {
226: os.flush();
227: console.SetColor( bgHiMagenta, fgMask );
228:
229: return os;
230: }
231:
232: inline std::ostream&; bg_yellow( std::ostream& os )
233: {
234: os.flush();
235: console.SetColor( bgHiYellow, fgMask );
236:
237: return os;
238: }
239:
240: inline std::ostream&; bg_black( std::ostream& os )
241: {
242: os.flush();
243: console.SetColor( bgBlack, fgMask );
244:
245: return os;
246: }
247:
248: inline std::ostream&; bg_gray( std::ostream& os )
249: {
250: os.flush();
251: console.SetColor( bgGray, fgMask );
252:
253: return os;
254: }
255: }
256:
257: //------------------------------------------------------------------------------
258: #endif //!defined ( CONSOLE_MANIP_H__INCLUDED )
259:
然后,附上使用方法:
1.引用头文件“Console.h” ;
2.命名空间的引用申明;
3.针对输出使用配色,例如:cout << con::fg_gray << “Gray text on black background\n”
main.cpp
1: //------------------------------------------------------------------------------
2: //conColorDemo - main.cpp
3: //
4: // written in December 2004 by a JadedHobo
5: //
6: // This program demonstrates the use of colors
7: // in a Win32 console application.
8: //------------------------------------------------------------------------------
9:
10: //-------------------------------------------------------------------<;include>--
11: #include <;iostream>
12: #include <;conio.h>
13:
14: //-------------------------------------------------------------------"include"--
15: #include "Console.h"
16:
17: //------------------------------------------------------------------------main--
18: int main()
19: {
20: using std::cout;
21: using std::endl;
22: using std::setw;
23: using std::setiosflags;
24:
25: namespace con = JadedHoboConsole;
26:
27: cout << con::clr;
28: cout <;< con::bg_blue << con::fg_white << setw( 40 )
29: <;< setiosflags( std::ios::left )
30: <;< "Funky colors in a console application" << endl;
31:
32:
33: cout <;< con::bg_black << con::fg_white
34: <;< "\nWhite text on black background\n";
35:
36: cout <;< con::fg_gray << "Gray text on black background\n"
37: <;< con::fg_red << "Red text on black background\n"
38: <;< con::fg_green << "Green text on black background\n"
39: <;< con::fg_blue << "Blue text on black background\n"
40: <;< con::fg_cyan << "Cyan text on black background\n"
41: <;< con::fg_magenta << "Magenta text on black background\n"
42: <;< con::fg_yellow << "Yellow text on black background\n";
43:
44: cout <;< con::bg_gray << con::fg_yellow << "Press the 'any' key..."
45: <;< con::bg_black << con::fg_white;
46: int c = _getch();
47: cout << "\nThe character " << con::bg_yellow << con::fg_black
48: <;< static_cast< char >( c ) << con::bg_black << con::fg_white
49: <;< " could be considered "
50: <;< con::fg_green << "OK" << con::fg_white << " or "
51: <;< con::fg_red << "WRONG" << con::fg_white
52: <;< " depending on taste\n" << endl;
53:
54: cout <;< con::bg_gray << "White text on gray background\n"
55: <;< con::bg_red << "White text on red background\n"
56: <;< con::bg_green << "White text on green background\n"
57: <;< con::bg_blue << "White text on blue background\n"
58: <;< con::bg_cyan << "White text on cyan background\n"
59: <;< con::bg_magenta << "White text on magenta background\n"
60: <;< con::bg_yellow << "White text on yellow background\n";
61:
62: cout <;< con::bg_white << con::fg_blue << "Press the 'any' key..."
63: <;< con::bg_black << con::fg_white;
64: _getch();
65: return 0;
66: }