传统弱校HFUT的蒟蒻,真相只有一个

黑漆漆的控制台也可以炫起来66666

 

一.最简单的方法就是全局控制,觉得有点傻瓜的方法/(ㄒoㄒ)/~~

注意都需要头文件<windows.h>

..............system("color 04");

第一个数字代表背景色,第二个数字代表前景色。各颜色的代码如下:
0=黑色
1=蓝色
2=绿色
3=湖蓝色
4=红色
5=紫色
6=黄色
7=白色
8=灰色
9=淡蓝色
A=淡绿色
B=淡浅绿色
C=淡红色
D=淡紫色
E=淡黄色
F=亮白色
 
二.用句柄控制嗨起来O(∩_∩)O~之SetConsoleTextAttribute函数
看一个C例子:
 1 #include<stdio.h>
 2 
 3 #include<windows.h>
 4 
 5 int main()
 6 
 7 {
 8 
 9   HANDLE consolehwnd;//创建句柄
10 
11   consolehwnd=GetStdHandle(STD_OUTPUT_HANDLE);//实例化句柄
12 
13   SetConsoleTextAttribute(consolehwnd,FOREGROUND_RED);//设置字体颜色
14 
15   printf("hello");
16 
17   SetConsoleTextAttribute(consolehwnd,FOREGROUND_INTENSITY|FOREGROUND_GREEN);
18 
19   printf("world!\n");
20 
21   getchar();
22 
23   SetConsoleTextAttribute(consolehwnd,BACKGROUND_INTENSITY|BACKGROUND_BLUE);
24 
25   printf("Itisreallybeautiful!\n");
26 
27   return 0;
28 
29 }

FOREGROUND_RED表示设置前景色为红色,即字体的颜色为红色;

FOREGROUND_INTENSITY 表示设置前景色为高亮显示,

FOREGROUND_GREEN表示绿色,两个参数中间加“|”表示前景色为高亮绿色;

BACKGROUND_INTENSITY表示设置背景色为高亮显示,

BACKGROUND_BLUE表示背景色为蓝色,

BACKGROUND_INTENSITY | BACKGROUND_BLUE两个参数就使背景色为高亮蓝色。

要表示红色字体,白色背景可以使用SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY |FOREGROUND_INTENSITY | FOREGROUND_RED|BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);

设置背景与字体都为高显BACKGROUND_INTENSITY |FOREGROUND_INTENSITY,

设置字体颜色为FOREGROUND_RED,背景颜色为BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE。

其实这里把这些数值加起来就是二进制的,11111100,即十六进制0xFC。

所以设置白体红字字也可以写成: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xFC)。

其他颜色可照此类推: White on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

 

Red on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);

 

Green on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);

 

Yellow on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN);

 

Blue on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE);

 

Magenta on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_BLUE);

 

Cyan on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN | FOREGROUND_BLUE);

 

Black on White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);

 

Red on White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |FOREGROUND_RED);

 

.用句柄控制嗨起来O(∩_∩)O~之改改控制台属性

 //获取控制台窗口信息    GetConsoleScreenBufferInfo();    
//获取控制台窗口标题    GetConsoleTitle();   
//更改指定缓冲区大小    SetConsoleScreenBufferSize();   
//设置控制台窗口标题    SetConsoleTitle();   
//设置控制台窗口信息    SetConsoleWindowInfo();

先看一个C例子:

 1 #include <stdio.h>    
 2 #include <stdlib.h>
 3 #include <Windows.h>    
 4 #include <conio.h>    
 5 #define N 255    
 6 int main()    
 7 {    
 8     HANDLE handle_out;  //定义一个句柄    
 9     CONSOLE_SCREEN_BUFFER_INFO scbi;    //定义一个窗口缓冲区信息结构体    
10     COORD size = {80, 25};      //定义一个坐标结构体    
11     char strtitle[N];    
12     handle_out = GetStdHandle(STD_OUTPUT_HANDLE);   //获得标准输出设备句柄    
13     GetConsoleScreenBufferInfo(handle_out, &scbi);  //获得窗口缓冲区信息    
14     GetConsoleTitle(strtitle, N);   //获得当前窗口标题    
15     printf("当前窗口标题为:%sn", strtitle);    
16     _getch();    
17     SetConsoleTitle("控制台窗口操作");     //设置窗口标题为“控制台窗口操作”    
18     GetConsoleTitle(strtitle, N);           //获得当前窗口标题    
19     printf("当前窗口标题为:%sn", strtitle);    
20     _getch();    
21     SetConsoleScreenBufferSize(handle_out, size);   // 重新设置缓冲区大小    
22     _getch();    
23     SMALL_RECT rc = {0, 0, 80-1, 25-1};     // 重置窗口位置和大小    
24     SetConsoleWindowInfo(handle_out, 1, &rc);    
25     CloseHandle(handle_out);    //关闭标准输出设备句柄    
26     return 0;    
27 }
28 
29  

 

其中,SetConsoleScreenBufferSize函数指定新的控制台屏幕缓冲区的大小,以字符列和行为单位。
指定的宽度和高度不能小于控制台屏幕缓冲区窗口的宽度和高度。
指定的大小也不能小于系统允许的最小大小。
这个最低取决于控制台当前的字体大小 (由用户选定)。
另外,GetConsoleTitle函数的具体实现取是否宏定义了UNICODE(与文件编码形式有关)。

 

四.控制台子框阴影效果

  很气人的是网上查得到的范例基本都不能运行一坨奇奇怪怪的错误,好不容易整出一个,vc正常运行没有warning,codeblack一堆warning也能运行,gcc完全死死掉23333,

看来这一块的知识真的是鸟不拉屎。

  1 #include<conio.h>
  2 
  3 #include<stdio.h>
  4 
  5 #include<windows.h>
  6 
  7 HANDLE hOut;
  8 
  9 void ShadowWindowLine(char *str);
 10 
 11 void DrawBox(bool bSingle, SMALL_RECT rc); // 绘制边框
 12 
 13 int main()
 14 
 15 {
 16 
 17     hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄
 18 
 19     SetConsoleOutputCP(437); // 设置代码页,这里如果设置成936(简体中文),
 20 
 21    // 那么程序会怎样?那样的话,将画不出边框。
 22 
 23     ShadowWindowLine("Display a line of words, and center the window with shadow.");
 24 
 25     CloseHandle(hOut); // 关闭标准输出设备句柄
 26 
 27     return 0;
 28 
 29 }
 30 
 31 void ShadowWindowLine(char *str)
 32 
 33 {
 34 
 35     SMALL_RECT rc;
 36 
 37     CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口缓冲区信息
 38 
 39     WORD att0,att1,attText;
 40 
 41     int i, chNum = strlen(str);
 42 
 43     GetConsoleScreenBufferInfo( hOut, &bInfo ); // 获取窗口缓冲区信息
 44 
 45          // 计算显示窗口大小和位置
 46 
 47     rc.Left = (bInfo.dwSize.X - chNum)/2 - 2;
 48 
 49          rc.Top = 8;
 50 
 51          // 原代码段中此处为bInfo.dwSize.Y/2 - 2,但是如果您的
 52 
 53     //DOS屏幕有垂直滚动条的话,还需要把滚动条下拉才能看到,为了方便就把它改为10
 54 
 55     rc.Right = rc.Left + chNum + 4;
 56 
 57     rc.Bottom = rc.Top + 4;
 58 
 59     att0 = BACKGROUND_INTENSITY; // 阴影属性
 60 
 61     att1 = FOREGROUND_RED |FOREGROUND_GREEN |FOREGROUND_BLUE |
 62 
 63            FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE;//文本属性
 64 
 65     attText = FOREGROUND_RED |FOREGROUND_INTENSITY; // 文本属性
 66 
 67          // 设置阴影然后填充
 68 
 69     COORD posShadow = {rc.Left+1, rc.Top+1}, posText = {rc.Left, rc.Top};
 70 
 71     for (i=0; i<5; i++)
 72 
 73     {
 74 
 75         FillConsoleOutputAttribute(hOut, att0, chNum + 4, posShadow, NULL);
 76 
 77         posShadow.Y++;
 78 
 79     }
 80 
 81     for (i=0; i<5; i++)
 82 
 83     {
 84 
 85         FillConsoleOutputAttribute(hOut, att1,chNum + 4, posText, NULL);
 86 
 87         posText.Y++;
 88 
 89     }
 90 
 91          // 写文本和边框
 92 
 93     posText.X = rc.Left + 2;
 94 
 95     posText.Y = rc.Top + 2;
 96 
 97     WriteConsoleOutputCharacter(hOut, str, strlen(str), posText, NULL);
 98 
 99     DrawBox(true, rc);
100 
101     SetConsoleTextAttribute(hOut, bInfo.wAttributes); // 恢复原来的属性
102 
103 }
104 
105 void DrawBox(bool bSingle, SMALL_RECT rc) // 函数功能:画边框
106 
107 {
108 
109     char chBox[6];
110 
111     COORD pos;
112 
113     if (bSingle)
114 
115     {
116 
117         chBox[0] = (char)0xda; // 左上角点
118 
119         chBox[1] = (char)0xbf; // 右上角点
120 
121         chBox[2] = (char)0xc0; // 左下角点
122 
123         chBox[3] = (char)0xd9; // 右下角点
124 
125         chBox[4] = (char)0xc4; // 水平
126 
127         chBox[5] = (char)0xb3; // 坚直
128 
129     }
130 
131     else
132 
133     {
134 
135         chBox[0] = (char)0xc9; // 左上角点
136 
137         chBox[1] = (char)0xbb; // 右上角点
138 
139         chBox[2] = (char)0xc8; // 左下角点
140 
141         chBox[3] = (char)0xbc; // 右下角点
142 
143         chBox[4] = (char)0xcd; // 水平
144 
145         chBox[5] = (char)0xba; // 坚直
146 
147     }
148 
149          // 画边框的上 下边界
150 
151     for(pos.X = rc.Left+1; pos.X<rc.Right-1; pos.X++)
152 
153     {
154 
155         pos.Y = rc.Top;
156 
157          // 画上边界
158 
159         WriteConsoleOutputCharacter(hOut, &chBox[4], 1, pos, NULL);
160 
161          // 画左上角
162 
163         if(pos.X == rc.Left+1)
164 
165         {
166 
167             pos.X--;
168 
169             WriteConsoleOutputCharacter(hOut, &chBox[0],1, pos, NULL);
170 
171             pos.X++;
172 
173         }
174 
175          // 画右上角
176 
177         if(pos.X == rc.Right-2)
178 
179         {
180 
181             pos.X++;
182 
183             WriteConsoleOutputCharacter(hOut, &chBox[1], 1, pos, NULL);
184 
185             pos.X--;
186 
187         }
188 
189         pos.Y = rc.Bottom;
190 
191          // 画下边界
192 
193         WriteConsoleOutputCharacter(hOut, &chBox[4], 1, pos, NULL);
194 
195          // 画左下角
196 
197         if(pos.X == rc.Left+1)
198 
199         {
200 
201             pos.X--;
202 
203             WriteConsoleOutputCharacter(hOut, &chBox[2], 1, pos, NULL);
204 
205             pos.X++;
206 
207         }
208 
209          // 画右下角
210 
211         if(pos.X==rc.Right-2)
212 
213         {
214 
215             pos.X++;
216 
217             WriteConsoleOutputCharacter(hOut, &chBox[3], 1, pos, NULL);
218 
219             pos.X--;
220 
221         }
222 
223     }
224 
225          // 画边框的左右边界
226 
227     for (pos.Y = rc.Top+1; pos.Y<=rc.Bottom-1; pos.Y++)
228 
229     {
230 
231         pos.X = rc.Left;
232 
233          // 画左边界
234 
235         WriteConsoleOutputCharacter(hOut, &chBox[5], 1, pos, NULL);
236 
237         pos.X = rc.Right-1;
238 
239          // 画右边界
240 
241         WriteConsoleOutputCharacter(hOut, &chBox[5], 1, pos, NULL);
242 
243     }
244 
245 }


需要说明的是: 

①在上述例子中,如果调用

DrawBox

函数时,传递的第一个参数不是

true

而是

false,

那么画出来的边框将是双线的。运行结果如下: 

②如果在上述程序无法编译通过,您可以这样修改,即程序中调用

WriteConsoleOutputCharacter和FillConsoleOutputAttribute

函数的时候,最后一个参数不用

NULL,而是先定义一个变量: DWORD written; 

然后把 &written作为最后一个参数。 

③上述程序在不同的字符代码页面(code page)下显示的结果是不同的。例如,中文

Windows操作系统的默认代码页是简体中文(936),在该代码页面下值超过128

的单字符在Windows NT/XP是显示不出来的。

下表列出了可以使用的代码页。 

代码页(Code   page)

说 明

1258

越南文

1257

波罗的海文

1256

阿拉伯文

1255

希伯来文

1254

土耳其语

1253

希腊文

1252

拉丁文(ANSI)

1251

斯拉夫文

1250

中欧文

950

繁体中文

949

韩文

936

简体中文

932

日文

874

泰文

850

使用多种语言(MS-DOS拉丁文)

437

MS-DOS美语/英语


五.控制台窗口中输出各种彩带codeblock通过

  1 #include <windows.h>
  2 #include <stdio.h>
  3 void shuiping();
  4 void chuizhi();
  5 void zuoqingxie();
  6 void youqingxie();
  7 void jiantou();
  8 void SetColor(unsigned short ForeColor,unsigned short BackGroundColor);
  9 int main()
 10 {
 11 
 12     int a;
 13     SMALL_RECT rc = {0,0,20,10};
 14     HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 15     SetConsoleOutputCP(936);
 16     SetColor(14,3);
 17     printf("0:水平彩带,\n1:垂直彩带, \n2:右倾斜彩带,\n3:左倾斜彩带,\n4:箭头状彩带,\n5:水纹状彩带,\n其他输入退出\n");
 18     scanf("%d",&a);
 19     while(a==0||a==1||a==2||a==3||a==4||a==5)
 20     {
 21         if(a==0)//实现水平彩带输出
 22         {
 23             shuiping();
 24             SetColor(14,3); //刷新缓冲区,使字迹可见
 25         }
 26         else if(a==1)//实现垂直彩带输出
 27         {
 28             chuizhi();
 29             SetColor(14,3);
 30         }
 31         else if(a==2)//实现右倾斜彩带输出
 32         {
 33             youqingxie();
 34             SetColor(14,3);
 35         }
 36         else if(a==3)//实现左倾斜彩带输出
 37         {
 38             zuoqingxie();
 39             SetColor(14,3);
 40         }
 41         else if(a==4)//实现箭头状彩带输出
 42         {
 43             jiantou();
 44             SetColor(14,3);
 45         }
 46         else if(a==5)//实现水纹状彩带输出
 47         {
 48             jiantou();
 49             jiantou();
 50             SetColor(14,3);
 51         }
 52         fflush(stdin);
 53        printf("0:水平彩带,\n1:垂直彩带, \n2:右倾斜彩带,\n3:左倾斜彩带,\n4:箭头状彩带,\n5:水纹状彩带,\n其他输入退出\n");
 54         scanf("%d",&a);
 55     }
 56     return 0;
 57 }
 58 
 59 void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)
 60 {
 61     HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
 62     SetConsoleTextAttribute(hCon,ForeColor+BackGroundColor*0x10);
 63 }
 64 //水平彩带函数
 65 void shuiping()
 66 {
 67     int i,j,k;
 68     for(i=0; i<25; ++i)
 69     {
 70         for(j=0; j<=79; ++j)
 71         {
 72             k=i%16;
 73             SetColor(k,k);
 74             putchar('A');
 75         }
 76     }
 77 }
 78 
 79 //垂直彩带函数
 80 void chuizhi()
 81 {
 82     int i,j,k;
 83     for(i=0; i<25; ++i)
 84     {
 85         for(j=0; j<40; ++j)
 86         {
 87             k=j%16;
 88             SetColor(k,k);
 89             putchar('A');
 90             putchar('A');
 91         }
 92 
 93     }
 94 }
 95 
 96 //右倾斜彩带函数
 97 void youqingxie()
 98 {
 99     int i,j,k;
100     for(i=0; i<25; ++i)
101     {
102         for(j=0; j<40; ++j)
103         {
104             if(j-i>=0)
105                 k=(j-i)%16;
106             else
107                 k=(j-i)%16+16;
108             SetColor(k,k);
109             putchar('A');
110             putchar('A');
111         }
112     }
113 }
114 
115 //左倾斜彩带函数
116 void zuoqingxie()
117 {
118     int i,j,k;
119     for(i=0; i<25; ++i)
120     {
121         for(j=0; j<40; ++j)
122         {
123             k=(i+j)%16;
124             SetColor(k,k);
125             putchar('A');
126             putchar('A');
127         }
128     }
129 }
130 
131 //箭头状彩带函数
132 void jiantou()
133 {
134     int i,j,k;
135     for(i=0; i<16; ++i)
136     {
137         for(j=0; j<40; ++j)
138         {
139             k=(i+j)%16;
140             SetColor(k,k);
141             putchar('A');
142             putchar('A');
143         }
144     }
145     for(i=0; i<16; ++i)
146     {
147         for(j=0; j<40; ++j)
148         {
149             if(j-i>=0)
150                 k=(j-i)%16;
151             else
152                 k=(j-i)%16+16;
153             SetColor(k,k);
154             putchar('A');
155             putchar('A');
156         }
157     }
158 }

 

六.控制台窗口中输出各种彩色方阵codeblock通过

  1 #include <windows.h>
  2 #include <stdio.h>
  3 #include <conio.h>
  4 #define getrandom( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
  5 void Init();
  6 void gotoxy(int x, int y);
  7 void regularcolor();
  8 void randomcolor();
  9 void Cls(HANDLE hConsole);
 10 HANDLE hOut;
 11 int forecolor[16];
 12 int backcolor[16];
 13 int main()
 14 {
 15     int i;
 16     int a;
 17     for (i = 0; i < 16; i++)
 18     {
 19         forecolor[i] = i;
 20         backcolor[i] = i << 4;
 21     }
 22     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 23     Init();
 24     while(1)
 25     {
 26         a = getch();
 27         if (a == 48)
 28         {
 29             Cls(hOut);
 30             regularcolor();
 31             getch();
 32         }
 33         else if (a == 49)
 34         {
 35             Cls(hOut);
 36             randomcolor();
 37             getch();
 38         }
 39         else
 40         {
 41             Cls(hOut);
 42             break;
 43         }
 44         Cls(hOut);
 45         Init();
 46     }
 47     CloseHandle(hOut);
 48     return 0;
 49 }
 50 //---------------------------------------------------------------------------
 51 void gotoxy(int x, int y)
 52 {
 53     COORD pos = {x, y};
 54     SetConsoleCursorPosition(hOut, pos);
 55 }
 56 
 57 void regularcolor(void)
 58 {
 59     int i, j, x, y;
 60     int l = 8, t = 5;
 61     for (y = 0; y < 16; y++)
 62     {
 63         gotoxy(l - 3, y + t);
 64         SetConsoleTextAttribute(hOut, forecolor[15]|backcolor[0]);
 65         printf("%d", y);
 66         for (x = 0; x < 16; x++)
 67         {
 68             gotoxy(x * 4 + l, y + t);
 69             SetConsoleTextAttribute(hOut, forecolor[y]|backcolor[x]);
 70             printf("ZZZ");
 71             if (y == 15)
 72             {
 73                 gotoxy(x * 4 + l, 17 + t);
 74                 SetConsoleTextAttribute(hOut, forecolor[15]|backcolor[0]);
 75                 printf("%d", x);
 76             }
 77         }
 78     }
 79 }
 80 
 81 void randomcolor(void)
 82 {
 83     int i, j, x, y;
 84     int l = 8, t = 5;
 85     char s[4] = {"012"};
 86     rand();
 87     for (y = 0; y < 16; y++)
 88     {
 89         for (x = 0; x < 16; x++)
 90         {
 91             s[0] = getrandom(32, 127);
 92             s[1] = getrandom(32, 127);
 93             s[2] = getrandom(32, 127);
 94             gotoxy(x * 4 + l, y + t);
 95             SetConsoleTextAttribute(hOut, forecolor[getrandom(0,
 96                                                     15)]|backcolor[getrandom(0, 15)]);
 97             printf("%c", s[0]);
 98             gotoxy(x * 4 + l + 1, y + t);
 99             SetConsoleTextAttribute(hOut, forecolor[getrandom(0,
100                                                     15)]|backcolor[getrandom(0, 15)]);
101             printf("%c", s[1]);
102             gotoxy(x * 4 + l + 2, y + t);
103             SetConsoleTextAttribute(hOut, forecolor[getrandom(0, 15)]|backcolor[getrandom(0, 15)]);
104             printf("%c", s[2]);
105         }
106     }
107 }
108 
109 void Cls(HANDLE hConsole)
110 {
111     COORD coordScreen = {0, 0};
112     BOOL   bSuccess;
113     DWORD  cCharsWritten;
114     CONSOLE_SCREEN_BUFFER_INFO csbi;
115     DWORD  dwConSize;
116     SetConsoleTextAttribute(hOut, 0x0f|0);
117     bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
118     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
119     bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR) '   ', dwConSize,
120                                           coordScreen, &cCharsWritten);
121     bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
122     bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
123     bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);
124 }
125 
126 void Init(void)
127 {
128     gotoxy(30, 10);
129     printf("0. Regular Color Array");
130     gotoxy(30, 11);
131     printf("1. Random Color Array");
132     gotoxy(30, 12);
133     printf("2. Quit");
134 }

 

posted @ 2015-07-21 11:18  未名亚柳  阅读(363)  评论(0编辑  收藏  举报