📄 console.h
字号:
/*
用于控制台窗口界面设计,版本1.0
2002 - 2003
2006.5
(1) 添加了控制台窗口的字体设计,
(2) 添加了边框型式,
(3) 添加主框架窗口的界面
*/
#include <windows.h>
#include <string.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct CONSOLE_FONT {
DWORD index;
COORD dim;
} *PCONSOLE_FONT;
typedef BOOL (WINAPI *GetConsoleFontInfoFunc)(HANDLE,BOOL,DWORD,PCONSOLE_FONT);
typedef COORD (WINAPI *GetConsoleFontSizeFunc)(HANDLE, DWORD);
typedef BOOL (WINAPI *GetCurrentConsoleFontFunc)(HANDLE, BOOL, PCONSOLE_FONT);
typedef DWORD (WINAPI *GetNumberOfConsoleFontsFunc)();
typedef BOOL (WINAPI *SetConsoleFontFunc)(HANDLE, DWORD);
GetConsoleFontInfoFunc pGetConsoleFontInfo;
GetConsoleFontSizeFunc pGetConsoleFontSize;
GetCurrentConsoleFontFunc pGetCurrentConsoleFont;
GetNumberOfConsoleFontsFunc pGetNumberOfConsoleFonts;
SetConsoleFontFunc pSetConsoleFont;
PCONSOLE_FONT fonts = NULL;
int GetAvailableFonts(HANDLE hCon,PCONSOLE_FONT *fonts) {
int fontcount = pGetNumberOfConsoleFonts();
*fonts = new CONSOLE_FONT[fontcount];
pGetConsoleFontInfo(hCon,0,fontcount,*fonts);
return fontcount;
}
BOOL SetFont(HANDLE hCon,int index) {
if (!pSetConsoleFont(hCon,index)) {
return FALSE;
}
return TRUE;
}
BOOL Init() {
HINSTANCE hLib = NULL;
BOOL bRet = TRUE;
hLib = LoadLibrary("KERNEL32.DLL");
if (hLib == NULL) {
return FALSE;
}
pGetConsoleFontInfo = (GetConsoleFontInfoFunc)GetProcAddress(hLib,"GetConsoleFontInfo");
pGetConsoleFontSize = (GetConsoleFontSizeFunc)GetProcAddress(hLib,"GetConsoleFontSize");
pGetCurrentConsoleFont = (GetCurrentConsoleFontFunc)GetProcAddress(hLib,"GetCurrentConsoleFont");
pGetNumberOfConsoleFonts = (GetNumberOfConsoleFontsFunc)GetProcAddress(hLib,"GetNumberOfConsoleFonts");
pSetConsoleFont = (SetConsoleFontFunc)GetProcAddress(hLib,"SetConsoleFont");
return bRet;
}
class CConsole
{
public:
CConsole();
~CConsole();
void _ClearWindow(void); // 清除当前窗口文本,并将光标移至左上角,即位置(0,0)
void _DefineWindow(int left, int top, int right, int bottom);
// 重新定义一个窗口,使得所有操作都与这个窗口有关
void _GetConwinSize(int *sizex, int *sizey);
// 返回控制台窗口的大小
void _GetWindowSize(int *sizex, int *sizey);
// 返回当前窗口的大小
void _SaveWindow(void); // 将当前窗口内容保存到内存中
void _SaveWindow(CHAR_INFO *buf); // 将当前窗口内容保存到指定内存中
void _PutWindow(int absX, int absY); // 将内存的内容写到指定位置处,(absX,absY)是绝对坐标
void _PutWindow(int absX, int absY, CHAR_INFO *buf); // 将指定内容写到指定位置处,(absX,absY)是绝对坐标
void _DrawBox(int x, int y, int length, int height, int mode = 0);
// 在指定位置(x, y)绘制一个长为length,宽为height的框,
// 当mode为0是单线,1为双线,2为混合,其它为单线
void _FillBox(int x, int y, int length, int height, bool solid = true);
// 填充指定范围的区域,若solid为true则擦除原来区域内容, 否则不擦除
void _DrawCharLine(int x, int y, int length, char ch);
// 在指定位置(x, y)绘制一个长为length字符线条,字符由ch指定
void _SaveSettings(void); // 保存当前的属性:光标、颜色和窗口
void _LoadSettings(void); // 恢复_SaveSettings保存的属性
void _ShowCursor(bool show = true); // 显示/隐藏光标
void _OutText(char *str, int nch = -1); // 在当前光标处输出nch个字符
void _OutTextXY(int x, int y, char *str, int nch = -1); // 在指定位置处输出nch个字符
void _SetCursorPos(int x, int y); // 将光标移动到指定位置
void _GetCursorPos(int *x, int *y); // 获取当前光标的位置
void _SetBackColor(int color); // 设置当前背景色
int _GetBackColor(void); // 获取当前背景色
void _SetForeColor(int color); // 设置当前前景色
int _GetForeColor(void); // 获取当前前景色
// 当color = 0 表示 黑色
// 当color = 1 表示 蓝色
// 当color = 2 表示 绿色
// 当color = 3 表示 青色
// 当color = 4 表示 红色
// 当color = 5 表示 洋红
// 当color = 6 表示 综色
// 当color = 7 表示 淡灰
// 当color = 8 表示 深灰
// 当color = 9 表示 淡蓝
// 当color = 10 表示 淡绿
// 当color = 11 表示 淡青
// 当color = 12 表示 淡红
// 当color = 13 表示 淡洋红
// 当color = 14 表示 黄色
// 当color = 15 表示 白色
unsigned int _GetKeyChar(void);
// 返回用户按键的字符,不等待,没有按键时返回0,
// 若是非打印字符,返回虚拟键码
int _GetMouse(int *mx, int *my, int *state);
// 获取鼠标位置(*mx, *my),鼠标按钮操作状态*state, 1时为单击,2时为双击
// 返回鼠标操作的按钮,5为最右键,1为最左键,2为第2个键,依次类推,一直到4
// 没有鼠标信息或不在当前窗口范围时返回-1,没有按下鼠标按钮,返回0
private:
HANDLE hOut; // 输出句柄
HANDLE hIn; // 输入句柄
SMALL_RECT rcWindow; // 窗口
WORD bkColor[16]; // 背景颜色
WORD foColor[16]; // 前景颜色
int bkColorIndex, foColorIndex; // 颜色索引值
int nSaveColor[2]; // 保存颜色
int nSavePos[2]; // 保存光标位置
bool bSaveShow; // 保存光标是否显示
SMALL_RECT rcSave; // 保存窗口
bool bSaved; // 是否可以调用_LoadSettings
CHAR_INFO charInfo[100*40]; // 保存窗口内容
unsigned int nMaxCols, nMaxRows; // 最大的行和列
};
// CConsole类实现代码
CConsole::CConsole()
{
hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄
hIn = GetStdHandle(STD_INPUT_HANDLE); // 获取标准输入设备句柄
// 设置默认字体
PCONSOLE_FONT fonts = NULL;
HANDLE hConsole = GetStdHandle( STD_ERROR_HANDLE );
Init();
int count = GetAvailableFonts(hConsole,&fonts);
SetFont(hConsole,fonts[count-1].index);
SetConsoleOutputCP(437); // 设置代码页
SMALL_RECT rc = {0,0, 80-1, 25-1};
rcWindow = rc; // 定义默认的窗口大小
COORD size = {80, 25};
SetConsoleScreenBufferSize(hOut,size); // 设置缓冲区大小
SetConsoleWindowInfo(hOut,true ,&rc); // 显示全部控制台窗口
// 定义颜色
foColor[0] = bkColor[0] = 0;
foColor[1] = FOREGROUND_BLUE;
foColor[2] = FOREGROUND_GREEN;
foColor[3] = FOREGROUND_BLUE | FOREGROUND_GREEN;
foColor[4] = FOREGROUND_RED;
foColor[5] = FOREGROUND_RED | FOREGROUND_BLUE;
foColor[6] = FOREGROUND_RED | FOREGROUND_GREEN;
foColor[7] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
bkColor[1] = BACKGROUND_BLUE;
bkColor[2] = BACKGROUND_GREEN;
bkColor[3] = BACKGROUND_BLUE | BACKGROUND_GREEN;
bkColor[4] = BACKGROUND_RED;
bkColor[5] = BACKGROUND_RED | BACKGROUND_BLUE;
bkColor[6] = BACKGROUND_RED | BACKGROUND_GREEN;
bkColor[7] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
for (int i=0; i<=7; i++)
{
foColor[i+8] = foColor[i] + FOREGROUND_INTENSITY;
bkColor[i+8] = bkColor[i] + BACKGROUND_INTENSITY;
}
bkColorIndex = 15; // 白色
foColorIndex = 0; // 黑色
SetConsoleTextAttribute(hOut, bkColor[bkColorIndex]|foColor[foColorIndex]);
_ClearWindow();
_SetCursorPos(0, 0);
bSaved = false;
nMaxCols = nMaxRows = 0;
}
CConsole::~CConsole()
{
CloseHandle(hOut); // 关闭标准输出设备句柄
CloseHandle(hIn); // 关闭标准输入设备句柄
}
void CConsole::_DrawCharLine(int x, int y, int length, char ch)
{
COORD pos = {rcWindow.Left + x, rcWindow.Top + y};
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
WORD att = bInfo.wAttributes;
for (int i = 0; i<length; i++){
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &ch, 1, pos, NULL);
pos.X++;
}
}
void CConsole::_DrawBox(int x, int y, int length, int height, int mode)
{
SMALL_RECT rc;
rc.Left = rcWindow.Left + x;
rc.Top = rcWindow.Top + y;
rc.Right = rc.Left + length;
rc.Bottom = rc.Top + height;
char chBox[7];
if (mode>5) mode = 0;
if (mode == 0) { // 单线
chBox[0] = (char)0xda; // 左上角点
chBox[1] = (char)0xbf; // 右上角点
chBox[2] = (char)0xc0; // 左下角点
chBox[3] = (char)0xd9; // 右下角点
chBox[4] = (char)0xc4; // 水平
chBox[5] = (char)0xc4; // 水平
chBox[6] = (char)0xb3; // 坚直
} else if (mode == 1){ // 双线
chBox[0] = (char)0xc9; // 左上角点
chBox[1] = (char)0xbb; // 右上角点
chBox[2] = (char)0xc8; // 左下角点
chBox[3] = (char)0xbc; // 右下角点
chBox[4] = (char)0xcd; // 水平
chBox[5] = (char)0xcd; // 水平
chBox[6] = (char)0xba; // 坚直
} else if (mode == 2){ // 混合, 上双余单
chBox[0] = (char)0xd5; // 左上角点
chBox[1] = (char)0xb8; // 右上角点
chBox[2] = (char)0xc0; // 左下角点
chBox[3] = (char)0xd9; // 右下角点
chBox[4] = (char)0xcd; // 上水平
chBox[5] = (char)0xc4; // 下水平
chBox[6] = (char)0xb3; // 坚直
} else if (mode == 3){ // 混合, 上单双余
chBox[0] = (char)0xd6; // 左上角点
chBox[1] = (char)0xb7; // 右上角点
chBox[2] = (char)0xc8; // 左下角点
chBox[3] = (char)0xbc; // 右下角点
chBox[4] = (char)0xc4; // 上水平
chBox[5] = (char)0xcd; // 下水平
chBox[6] = (char)0xba; // 坚直
} else if (mode == 4){ // 混合, 下单余双
chBox[0] = (char)0xc9; // 左上角点
chBox[1] = (char)0xbb; // 右上角点
chBox[2] = (char)0xd3; // 左下角点
chBox[3] = (char)0xbd; // 右下角点
chBox[4] = (char)0xcd; // 上水平
chBox[5] = (char)0xc4; // 下水平
chBox[6] = (char)0xba; // 坚直
} else if (mode == 5){ // 混合, 左右双余单
chBox[0] = (char)0xd6; // 左上角点
chBox[1] = (char)0xb7; // 右上角点
chBox[2] = (char)0xd3; // 左下角点
chBox[3] = (char)0xbd; // 右下角点
chBox[4] = (char)0xc4; // 上水平
chBox[5] = (char)0xc4; // 下水平
chBox[6] = (char)0xba; // 坚直
}
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
WORD att = bInfo.wAttributes;
COORD pos = {rc.Left, rc.Top};
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[0], 1, pos, NULL);
for (pos.X = rc.Left + 1; pos.X<rc.Right-1; pos.X++){
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[4], 1, pos, NULL);
}
pos.X = rc.Right-1;
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[1], 1, pos, NULL);
for (pos.Y = rc.Top+1; pos.Y<rc.Bottom-1; pos.Y++)
{
pos.X = rc.Left;
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[6], 1, pos, NULL);
pos.X = rc.Right-1;
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[6], 1, pos, NULL);
}
pos.X = rc.Left; pos.Y = rc.Bottom-1;
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[2], 1, pos, NULL);
for (pos.X = rc.Left + 1; pos.X<rc.Right-1; pos.X++){
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[5], 1, pos, NULL);
}
pos.X = rc.Right-1;
WriteConsoleOutputAttribute(hOut, &att, 1, pos, NULL);
WriteConsoleOutputCharacter(hOut, &chBox[3], 1, pos, NULL);
}
void CConsole::_FillBox(int x, int y, int length, int height, bool solid)
{
SMALL_RECT rc;
rc.Left = rcWindow.Left + x;
rc.Top = rcWindow.Top + y;
rc.Right = rc.Left + length;
rc.Bottom = rc.Top + height;
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
WORD att = bInfo.wAttributes;
COORD pos = {rc.Left, rc.Top};
unsigned long sizeLine = rc.Right - rc.Left;
for (pos.Y=rc.Top; pos.Y<rc.Bottom; pos.Y++)
{
FillConsoleOutputAttribute(hOut, att, sizeLine, pos, NULL);
if (solid)
FillConsoleOutputCharacter(hOut, ' ', sizeLine, pos, NULL);
}
}
void CConsole::_ClearWindow(void)
{
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
WORD att = bInfo.wAttributes;
COORD pos = {rcWindow.Left, rcWindow.Top};
unsigned long sizeLine = rcWindow.Right - rcWindow.Left + 1;
for (pos.Y=rcWindow.Top; pos.Y<=rcWindow.Bottom; pos.Y++)
{
FillConsoleOutputAttribute(hOut, att, sizeLine, pos, NULL);
FillConsoleOutputCharacter(hOut, ' ', sizeLine, pos, NULL);
}
}
void CConsole::_DefineWindow(int left, int top, int right, int bottom)
{
rcWindow.Left = left;
rcWindow.Right = right;
rcWindow.Top = top;
rcWindow.Bottom = bottom;
_SetCursorPos(0, 0);
}
void CConsole::_GetConwinSize(int *sizex, int *sizey)
{
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
*sizex = bInfo.dwSize.X;
*sizey = bInfo.dwSize.Y;
}
void CConsole::_GetWindowSize(int *sizex, int *sizey)
{
*sizex = rcWindow.Right - rcWindow.Left + 1;
*sizey = rcWindow.Bottom - rcWindow.Top + 1;
}
void CConsole::_SetCursorPos(int x, int y)
{
COORD pos = {rcWindow.Left+x, rcWindow.Top+y};
if (pos.X>rcWindow.Right) return;
if (pos.Y>rcWindow.Bottom) return;
SetConsoleCursorPosition(hOut, pos);
}
void CConsole::_GetCursorPos(int *x, int *y)
{
CONSOLE_SCREEN_BUFFER_INFO bInfo;
GetConsoleScreenBufferInfo( hOut, &bInfo );
COORD pos = bInfo.dwCursorPosition;
*x = pos.X - rcWindow.Left;
*y = pos.Y - rcWindow.Top;
}
void CConsole::_SetBackColor(int color)
{
bkColorIndex = color;
SetConsoleTextAttribute(hOut, bkColor[bkColorIndex]|foColor[foColorIndex]);
}
int CConsole::_GetBackColor(void)
{
return bkColorIndex;
}
void CConsole::_SetForeColor(int color)
{
foColorIndex = color;
SetConsoleTextAttribute(hOut, bkColor[bkColorIndex]|foColor[foColorIndex]);
}
int CConsole::_GetForeColor(void)
{
return foColorIndex;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -