我承认 我懒了 求 清屏代码啊~诶 怎么都不能清屏
如题 谢谢了 百度 找不到的说
http://topic. 1楼 13楼
#ifndef SCREEN_H #define SCREEN_H #include <string> // 兼容API原来设计采用宏定义 #define FOREGROUND_YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define FOREGROUND_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE #define BACKGROUND_PURPLE BACKGROUND_RED | BACKGROUND_BLUE // 按键常数 #define K_SOH 0x0001 // 标题开始 #define K_STX 0x0002 // 正文开始 #define K_ETX 0x0003 // 正文结束 #define K_EOT 0x0004 // 传输结束 #define K_ENQ 0x0005 // 请求 #define K_ACK 0x0006 // 收到通知 #define K_BEL 0x0007 // 响铃 #define K_BACKSPACE 0x0008 // 退格 #define K_TAB 0x0009 // 水平制表符 #define K_LF 0x000A // 换行 #define K_VT 0x000B // 垂直制表符 #define K_FF 0x000C // 换页符 #define K_ENTER 0x000D // 回车 #define K_SO 0x000E // 不用切换 #define K_SI 0x000F // 启用切换 #define K_DLE 0x0010 // 数据链路转义 #define K_DC1 0x0011 // 设备控制1 #define K_DC2 0x0012 // 设备控制2 #define K_DC3 0x0013 // 设备控制3 #define K_DC4 0x0014 // 设备控制4 #define K_NAK 0x0015 // 拒绝接收 #define K_SYN 0x0016 // 同步空闲 #define K_ETB 0x0017 // 传输块结束 #define K_CAN 0x0018 // 取消/作废 #define K_EM 0x0019 // 介质中断 #define K_SUB 0x001A // 替补 #define K_ESC 0x001B // 溢出 #define K_FS 0x001C // 文件分割符 #define K_GS 0x001D // 分组符 #define K_RS 0x001E // 记录分离符 #define K_US 0x001F // 单元分隔符 #define K_SPACE 0x0020 #define K_CDEL 0x007F // ASCII DEL #define K_UP 0xE048 #define K_DOWN 0xE050 #define K_LEFT 0xE04B #define K_RIGHT 0xE04D #define K_HOME 0xE047 #define K_END 0xE04F #define K_PGUP 0xE049 #define K_PGDN 0xE051 #define K_INS 0xE052 #define K_DEL 0xE053 class Screen { public: Screen(); // 构造函数 SHORT GetRows(VOID) const {return ScreenInfo.dwSize.Y;} // 获取总行数 SHORT GetCols(VOID) const {return ScreenInfo.dwSize.X;} // 获取总列数 WCHAR ReadKey(VOID) const; // 从键盘接收一个按键 VOID SetTitle(const std::wstring& title) const; // 设置控制台标题文字 COORD GetCursorPosition(VOID) const {return ScreenInfo.dwCursorPosition;}; // 获取当前光标位置 Screen& MoveCursor(const SHORT x, const SHORT y); // 将光标定位到指定位置 Screen& MoveCursor(const COORD& position); // 将光标定位到指定位置 WORD GetTextAttribute(VOID) const {return ScreenInfo.wAttributes;}; // 获取当前文本颜色 Screen& SetTextAttribute(const WORD attribute); // 设置文本颜色 VOID WriteText(const WCHAR character); // 在当前光标位置输出单个字符 VOID WriteText(const WCHAR* text); // 在当前光标位置输出字符串 VOID WriteText(const std::wstring& text); // 在当前光标位置输出字符串 VOID Cls(VOID); // 用当前颜色清屏 VOID Cls(const WORD attribute); // 用指定颜色清屏 VOID Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2); // 将指定区域用当前颜色清屏 VOID Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute); // 将指定区域用指定颜色清屏 VOID Box(const SMALL_RECT& rect); // 用当前颜色绘画矩形 VOID Box(const SMALL_RECT& rect, const WORD attribute); // 用指定颜色绘画矩形 VOID Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2); // 用当前颜色绘画矩形 VOID Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute); // 用指定颜色绘画矩形 private: HANDLE hOutput; // 输出设备句柄 CONSOLE_SCREEN_BUFFER_INFO ScreenInfo; // 屏幕缓冲区数据 CONSOLE_CURSOR_INFO CursorInfo; // 光标数据 }; #endif
#include <Windows.h> #include <my_Tools.h> #include <Screen.h> Screen::Screen() { this->hOutput = GetStdHandle(STD_OUTPUT_HANDLE); // 获取控制台句柄 if (hOutput != INVALID_HANDLE_VALUE) { GetConsoleScreenBufferInfo(hOutput, &ScreenInfo); ScreenInfo.dwSize.X = 80; ScreenInfo.dwSize.Y = 25; SetConsoleScreenBufferSize(hOutput, ScreenInfo.dwSize); GetConsoleCursorInfo(hOutput, &CursorInfo); } else { MessageBoxW(NULL, L"获取控制台窗口句柄失败!", L"Screen Class", MB_ICONSTOP | MB_OK); } } WCHAR Screen::ReadKey(VOID) const { WCHAR keycode = _getwch(); if ((keycode & 0xE0) == 0xE0) { keycode = keycode * 256 + _getwch(); } return keycode; } VOID Screen::SetTitle(const std::wstring& title) const { SetConsoleTitleW(title.c_str()); } Screen& Screen::MoveCursor(const SHORT x, const SHORT y) { ScreenInfo.dwCursorPosition.X = x; ScreenInfo.dwCursorPosition.Y = y; SetConsoleCursorPosition(hOutput, ScreenInfo.dwCursorPosition); return *this; } Screen& Screen::MoveCursor(const COORD& position) { SetConsoleCursorPosition(hOutput, position); return *this; } Screen& Screen::SetTextAttribute(const WORD attribute) { ScreenInfo.wAttributes = attribute; SetConsoleTextAttribute(hOutput, ScreenInfo.wAttributes); return *this; } VOID Screen::WriteText(const WCHAR character) { DWORD number; WriteConsoleW(hOutput, &character, 1, &number, NULL); if (IsAscii(character)) { ++ScreenInfo.dwCursorPosition.X; } else { ScreenInfo.dwCursorPosition.X += 2; } MoveCursor(ScreenInfo.dwCursorPosition); } VOID Screen::WriteText(const WCHAR* text) { while (*text) { WriteText(*text); ++text; } } VOID Screen::WriteText(const std::wstring& text) { for (std::wstring::const_iterator index(text.begin()); index != text.end(); ++index) { WriteText(*index); } } VOID Screen::Cls(VOID) { COORD pos = {ScreenInfo.dwCursorPosition.X, ScreenInfo.dwCursorPosition.Y}; for (SHORT y = 0; y != ScreenInfo.dwSize.Y; ++y) { for (SHORT x = 0; x != ScreenInfo.dwSize.X; ++x) { MoveCursor(x, y).WriteText(L' '); } } MoveCursor(pos); } VOID Screen::Cls(const WORD attribute) { SetTextAttribute(attribute).Cls(); } VOID Screen::Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2) { COORD old_pos(GetCursorPosition()); for (SHORT y = y1; y <= y2; ++y) { for (SHORT x = x1; x <= x2; ++x) { MoveCursor(x,y).WriteText(L' '); } } MoveCursor(old_pos); } VOID Screen::Clear(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute) { WORD old_attr(GetTextAttribute()); COORD old_pos(GetCursorPosition()); SetTextAttribute(attribute); for (SHORT y = y1; y <= y2; ++y) { for (SHORT x = x1; x <= x2; ++x) { MoveCursor(x,y).WriteText(L' '); } } MoveCursor(old_pos); SetTextAttribute(old_attr); } VOID Screen::Box(const SMALL_RECT& rect) { std::wstring text; text = L'┌'; for (SHORT i = rect.Left + 2; i != rect.Right; ++i) { text += L'─'; } text += L'┐'; MoveCursor(rect.Left, rect.Top).WriteText(text); for (SHORT j = rect.Top + 1; j < rect.Bottom; ++j) { text = L'│'; for (SHORT i = rect.Left + 2; i != rect.Right; ++i) { text += L" "; } text += L'│'; MoveCursor(rect.Left, j).WriteText(text); } text = L'└'; for (SHORT i = rect.Left + 2; i != rect.Right; ++i) { text += L'─'; } text += L'┘'; MoveCursor(rect.Left, rect.Bottom).WriteText(text); } VOID Screen::Box(const SMALL_RECT& rect, const WORD attribute) { WORD old_attr(ScreenInfo.wAttributes); SetTextAttribute(attribute).Box(rect); SetTextAttribute(old_attr); } VOID Screen::Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2) { SMALL_RECT rect; rect.Left = x1; rect.Top = y1; rect.Right = x2; rect.Bottom = y2; Box(rect); } VOID Screen::Box(const SHORT x1, const SHORT y1, const SHORT x2, const SHORT y2, const WORD attribute) { WORD old_attr(ScreenInfo.wAttributes); SetTextAttribute(attribute).Box(x1, y1, x2, y2); SetTextAttribute(old_attr); }