?? 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 + -