📄 common.cpp
字号:
#include "Common.h"
void ClearBuffer(int x,int y,int w,int h,int color)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD rd;
DWORD n;
WORD cs[2] = { color, color };
for(int i=x;i<w+x;i++)
{
for(int j=y;j<h+y;j++)
{
rd.X = i;rd.Y = j;
WriteConsoleOutputCharacterA(hOut," ",1,rd,&n);
WriteConsoleOutputAttribute(hOut, cs, 1, rd, &n );
}
}
DeleteObject(hOut);
}
void SetConsoleInfo()
{
// 设置控制台的标题
SetConsoleTitleA("俄罗斯方块");
// 获取控制台的句柄
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor = {1,false};
// 设置控制台光标的属性
SetConsoleCursorInfo(hOut,&cursor);
SMALL_RECT rect = {0,0,WIDTH-1,HEIGHT-1};
// 设置窗口大小
SetConsoleWindowInfo(hOut,TRUE,&rect);
COORD rd = {WIDTH,HEIGHT};
// 设置控制台缓冲区大小
SetConsoleScreenBufferSize(hOut,rd);
DeleteObject(hOut);
}
//x,y为文本输出坐标,color为文本颜色,str为文本字符串
void DrawText( short x, short y, WORD color,const char* str )//设置输出文本函数
{
HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE); // 获得输出句柄
DWORD n;
COORD pos = {x,y}; // 文本输出坐标
WORD cs[2] = { color, color }; // 颜色
for( const char* p=str;*p!=0; )
{
if( *p > 0 ) //输出单字节字符
{
WriteConsoleOutputCharacterA(hStd, p, 1, pos, &n );
WriteConsoleOutputAttribute(hStd, cs, 1, pos, &n );
++p,++pos.X;
}
else //输出双字节字符
{
WriteConsoleOutputCharacterA(hStd, p, 2, pos, &n );
WriteConsoleOutputAttribute(hStd, cs, 2, pos, &n );
p+=2,pos.X+=2;
}
}
DeleteObject(hStd);
}
// x y左上的坐标 框内宽w(单字节) 框内高h
void DrawFrame(int x,int y,int w,int h,int color)
{
for(int i=y-1;i<y+h;i++)
{
DrawText(x,i,color,"┃");
DrawText(x+w+2,i,color,"┃");
}
for(int i=x+2;i<=x+w;i+=2)
{
DrawText(i,y,color,"━");
DrawText(i,y+h,color,"━");
}
DrawText(x,y,color,"┏");
DrawText(x+w+2,y,color,"┓");
DrawText(x,y+h,color,"┗");
DrawText(x+w+2,y+h,color,"┛");
}
void readbufchar(int x,int y,char* c,WORD* cs)//读取窗口缓冲区的<x,y>的字符
{
DWORD l=2;
COORD pos = {x,y};
HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE);// 获得输出句柄
ReadConsoleOutputCharacterA(hStd,c,l,pos,&l);
if(cs)
{
ReadConsoleOutputAttribute(hStd,cs,l,pos,&l);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -