1 #include <windows.h>
2 #include<time.h>
3 //GetStdHandle和SetConsoleTextAttribute在头文件windows.h中
4 #include <iostream>
5 using namespace std;
6 void SetColor(unsigned short ForeColor=3,unsigned short BackGroundColor=0)
7 //给参数默认值,使它
8 //可以接受0/1/2个参数
9 {
10 HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); //本例以输出为例
11 SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor);
12 }
13 int main()
14 {
15 srand(time(NULL)*100);
16
17 SetColor();
18 std::cout<<"Hello world!"<<endl;
19 SetColor(40,30);
20 std::cout<<"Hello world!"<<endl;
21 SetColor(10,200);
22 std::cout<<"Hello world!"<<endl;
23 for(int i=1;i<=10000;i++)
24 {
25 int a1,a2;
26 a1=rand()%20;
27 a2=rand()%20;
28 SetColor(a1,a2);
29 cout<<a1<<":"<<a2<<endl;
30 std::cout<<"Hello world!"<<endl;
31
32 Sleep(50);
33 }
34 return 0;
35 }