赞
踩
#include
void main() { cout<<"Hello, Console!"< } |
#include
#include #include void main() { HANDLE hOut; hout = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口信息 GetConsoleScreenBufferInfo(hOut, &bInfo ); // 获取窗口信息 printf("/n/nThe soul selects her own society,/n"); printf("Then shuts the door;/n"); printf("On her devine majority/n"); printf("Obtrude no more./n/n"); _getch(); COORD pos = {0, 0}; FillConsoleOutputCharacter(hOut, ' ', bInfo.dwSize.X * bInfo.dwSize.Y, pos, NULL); // 向窗口中填充字符以获得清屏的效果 CloseHandle(hOut); // 关闭标准输出设备句柄 } |
// 坐标结构体 typedef struct _COORD { SHORT X; SHORT Y; } COORD; // 控制台窗口信息结构体 typedef struct _CONSOLE_SCREEN_BUFFER_INFO { COORD dwSize; // 缓冲区大小 COORD dwCursorPosition; // 当前光标位置 WORD wAttributes; // 字符属性 SMALL_RECT srWindow; // 当前窗口显示的大小和位置 COORD dwMaximumWindowSize; // 最大的窗口缓冲区大小 } CONSOLE_SCREEN_BUFFER_INFO ; |
还需要说明的是,虽然在C++中,iostream.h定义了cin和cout的标准输入和输出流对象。但它们只能实现基本的输入输出操作,对于控制台窗口界面的控制却无能为力,而且不能与stdio.h和conio.h友好相处,因为iostream.h和它们是C++两套不同的输入输出操作方式,使用时要特别注意。
三、控制台窗口操作
此外,还有窗口字体、显示模式等控制函数,这里不再细说。下列举一个示例,程序如下:
需要说明的是,控制台窗口的原点坐标是(0, 0),而最大的坐标是缓冲区大小减1,例如当缓冲区大小为80*25时,其最大的坐标是(79, 24)。 四、文本属性操作 与DOS字符相似,控制台窗口中的字符也有相应的属性。这些属性分为:文本的前景色、背景色和双字节字符集(DBCS)属性三种。事实上,我们最关心是文本颜色,这样可以构造出美观的界面。颜色属性都是一些预定义标识:
与文本属性相关的主要函数有:
另外,获取当前控制台窗口的文本属性是通过调用函数GetConsoleScreenBufferInfo后,在CONSOLE_SCREEN_ BUFFER_INFO结构成员wAttributes中得到。 五、文本输出 文本输出函数有:
可以看出:WriteConsoleOutput函数功能相当于SetConsoleTextAttribute和WriteConsole的功能。而WriteConsoleOutputCharacter函数相当于SetConsoleCursorPosition(设置光标位置)和WriteConsole的功能。不过在具体使用要注意它们的区别。 |
六、文本操作示例
下面看一个示例程序:
#include <windows.h>
HANDLE hOut; void ShadowWindowLine(char *str); // 在具有阴影效果的窗口中显示一行字符,窗口为居中显示 void DrawBox(bool bSingle, SMALL_RECT rc); // 绘制边框 void main() { hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 SetConsoleOutputCP(437); // 设置代码页 ShadowWindowLine("Display a line of words, and center the window with shadow."); CloseHandle(hOut); // 关闭标准输出设备句柄 } void ShadowWindowLine(char *str) { CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口缓冲区信息 GetConsoleScreenBufferInfo( hOut, &bInfo ); // 获取窗口缓冲区信息 // 计算显示窗口大小和位置 int x1, y1, x2, y2, chNum = strlen(str); x1 = (bInfo.dwSize.X - chNum)/2 - 2; y1 = bInfo.dwSize.Y/2 - 2; x2 = x1 + chNum + 4; y2 = y1 + 5; WORD att1 = BACKGROUND_INTENSITY; // 阴影属性 WORD att0 = FOREGROUND_RED |FOREGROUND_GREEN |FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE; // 文本属性 WORD attText = FOREGROUND_RED |FOREGROUND_INTENSITY; // 文本属性 // 设置阴影 COORD posShadow = {x1+1, y1+1}, posText = {x1, y1}; for (int i=0; i<5; i++){ FillConsoleOutputAttribute(hOut, att1, chNum + 4, posShadow, NULL); posShadow.Y++; } // 填充窗口背景 for (i=0; i<5; i++){ FillConsoleOutputAttribute(hOut, att0, chNum + 4, posText, NULL); posText.Y++; } // 写文本和边框 posText.X = x1 + 2; posText.Y = y1 + 2; WriteConsoleOutputCharacter(hOut, str, strlen(str), posText, NULL); SMALL_RECT rc = {x1, y1, x2-1, y2-1}; DrawBox(true, rc); SetConsoleTextAttribute(hOut, bInfo.wAttributes); // 恢复原来的属性 } void DrawBox(bool bSingle, SMALL_RECT rc) { char chBox[6]; if (bSingle) { chBox[0] = (char)0xda; // 左上角点 chBox[1] = (char)0xbf; // 右上角点 chBox[2] = (char)0xc0; // 左下角点 chBox[3] = (char)0xd9; // 右下角点 chBox[4] = (char)0xc4; // 水平 chBox[5] = (char)0xb3; // 坚直 } else { chBox[0] = (char)0xc9; // 左上角点 chBox[1] = (char)0xbb; // 右上角点 chBox[2] = (char)0xc8; // 左下角点 chBox[3] = (char)0xbc; // 右下角点 chBox[4] = (char)0xcd; // 水平 chBox[5] = (char)0xba; // 坚直 } COORD pos = {rc.Left, rc.Top}; WriteConsoleOutputCharacter(hOut, &chBox[0], 1, pos, NULL); for (pos.X = rc.Left + 1; pos.X WriteConsoleOutputCharacter(hOut, &chBox[4], 1, pos, NULL); pos.X = rc.Right; WriteConsoleOutputCharacter(hOut, &chBox[1], 1, pos, NULL); for (pos.Y = rc.Top+1; pos.Y { pos.X = rc.Left; WriteConsoleOutputCharacter(hOut, &chBox[5], 1, pos, NULL); pos.X = rc.Right; WriteConsoleOutputCharacter(hOut, &chBox[5], 1, pos, NULL); } pos.X = rc.Left; pos.Y = rc.Bottom; WriteConsoleOutputCharacter(hOut, &chBox[2], 1, pos, NULL); for (pos.X = rc.Left + 1; pos.X WriteConsoleOutputCharacter(hOut, &chBox[4], 1, pos, NULL); pos.X = rc.Right; WriteConsoleOutputCharacter(hOut, &chBox[3], 1, pos, NULL); } |
代码页(Code page) | 说 明 |
1258 | 越南文 |
1257 | 波罗的海文 |
1256 | 阿拉伯文 |
1255 | 希伯来文 |
1254 | 土耳其语 |
1253 | 希腊文 |
1252 | 拉丁文(ANSI) |
1251 | 斯拉夫文 |
1250 | 中欧文 |
950 | 繁体中文 |
949 | 韩文 |
936 | 简体中文 |
932 | 日文 |
874 | 泰文 |
850 | 使用多种语言(MS-DOS拉丁文) |
437 | MS-DOS美语/英语 |
七、滚动和移动
ScrollConsoleScreenBuffer是实现文本区滚动和移动的API函数。它可以将指定的一块文本区域移动到另一个区域,被移空的那块区域由指定字符填充。函数的原型如下:
BOOL ScrollConsoleScreenBuffer( HANDLE hConsoleOutput, // 句柄 CONST SMALL_RECT* lpScrollRectangle, // 要滚动或移动的区域 CONST SMALL_RECT* lpClipRectangle, // 裁剪区域 COORD dwDestinationOrigin, // 新的位置 CONST CHAR_INFO* lpFill // 填充字符 ); |
#include
#include #include HANDLE hOut; void DeleteLine(int row); // 删除一行 void MoveText(int x, int y, SMALL_RECT rc); // 移动文本块区域 void ClearScreen(void); // 清屏 void main() { hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 WORD att = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE ; // 背景是蓝色,文本颜色是黄色 SetConsoleTextAttribute(hOut, att); ClearScreen(); printf("/n/nThe soul selects her own society,/n"); printf("Then shuts the door;/n"); printf("On her devine majority;/n"); printf("Obtrude no more./n/n"); CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); COORD endPos = {0, bInfo.dwSize.Y - 1}; SetConsoleCursorPosition(hOut, endPos); // 设置光标位置 SMALL_RECT rc = {0, 2, 40, 5}; _getch(); MoveText(10, 5, rc); _getch(); DeleteLine(5); CloseHandle(hOut); // 关闭标准输出设备句柄 } void DeleteLine(int row) { SMALL_RECT rcScroll, rcClip; COORD crDest = {0, row - 1}; CHAR_INFO chFill; CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); rcScroll.Left = 0; rcScroll.Top = row; rcScroll.Right = bInfo.dwSize.X - 1; rcScroll.Bottom = bInfo.dwSize.Y - 1; rcClip = rcScroll; chFill.Attributes = bInfo.wAttributes; chFill.Char.AsciiChar = ' '; ScrollConsoleScreenBuffer(hOut, &rcScroll, &rcClip, crDest, &chFill); } void MoveText(int x, int y, SMALL_RECT rc) { COORD crDest = {x, y}; CHAR_INFO chFill; CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); chFill.Attributes = bInfo.wAttributes; chFill.Char.AsciiChar = ' '; ScrollConsoleScreenBuffer(hOut, &rc, NULL, crDest, &chFill); } void ClearScreen(void) { CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); COORD home = {0, 0}; WORD att = bInfo.wAttributes; unsigned long size = bInfo.dwSize.X * bInfo.dwSize.Y; FillConsoleOutputAttribute(hOut, att, size, home, NULL); FillConsoleOutputCharacter(hOut, ' ', size, home, NULL); } |
八、光标操作
控制台窗口中的光标反映了文本插入的当前位置,通过SetConsoleCursorPosition函数可以改变这个“当前”位置,这样就能控制字符(串)输出。事实上,光标本身的大小和显示或隐藏也可以通过相应的API函数进行设定。例如:
BOOL SetConsoleCursorInfo( // 设置光标信息 HANDLE hConsoleOutput, // 句柄 CONST CONSOLE_CURSOR_INFO *lpConsoleCursorInfo // 光标信息 ); BOOL GetConsoleCursorInfo( // 获取光标信息 HANDLE hConsoleOutput, // 句柄 PCONSOLE_CURSOR_INFO lpConsoleCursorInfo // 返回光标信息 ); |
typedef struct _CONSOLE_CURSOR_INFO { DWORD dwSize; // 光标百分比大小 BOOL bVisible; // 是否可见 } CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO; |
BOOL ReadConsoleInput( HANDLE hConsoleInput, // 输入设备句柄 PINPUT_RECORD lpBuffer, // 返回数据记录 DWORD nLength, // 要读取的记录数 LPDWORD lpNumberOfEventsRead // 返回已读取的记录数 ); |
typedef struct _INPUT_RECORD { WORD EventType; // 事件类型 union { KEY_EVENT_RECORD KeyEvent; MOUSE_EVENT_RECORD MouseEvent; WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent; MENU_EVENT_RECORD MenuEvent; FOCUS_EVENT_RECORD FocusEvent; } Event; } INPUT_RECORD; |
typedef struct _KEY_EVENT_RECORD { BOOL bKeyDown; // TRUE表示键按下,FALSE表示键释放 WORD wRepeatCount; // 按键次数 WORD wVirtualKeyCode; // 虚拟键代码 WORD wVirtualScanCode; // 虚拟键扫描码 union { WCHAR UnicodeChar; // 宽字符 CHAR AsciiChar; // ASCII字符 } uChar; // 字符 DWORD dwControlKeyState; // 控制键状态 } KEY_EVENT_RECORD; |
#include
HANDLE hOut; HANDLE hIn; void DrawBox(bool bSingle, SMALL_RECT rc); void ClearScreen(void); void CharWindow(char ch, SMALL_RECT rc); // 将ch输入到指定的窗口中 void ControlStatus(DWORD state); // 在最后一行显示控制键的状态 void DeleteTopLine(SMALL_RECT rc); // 删除指定窗口中最上面的行并滚动 void main() { hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 hIn = GetStdHandle(STD_INPUT_HANDLE); // 获取标准输入设备句柄 WORD att = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE ; // 背景是蓝色,文本颜色是黄色 SetConsoleTextAttribute(hOut, att); ClearScreen(); // 清屏 INPUT_RECORD keyRec; DWORD state = 0, res; char ch; SMALL_RECT rc = {20, 2, 40, 12}; DrawBox(true, rc); COORD pos = {rc.Left+1, rc.Top+1}; SetConsoleCursorPosition(hOut, pos); // 设置光标位置 for(;;) // 循环 { ReadConsoleInput(hIn, &keyRec, 1, &res); if (state != keyRec.Event.KeyEvent.dwControlKeyState) { state = keyRec.Event.KeyEvent.dwControlKeyState; ControlStatus(state); } if (keyRec.EventType == KEY_EVENT){ if (keyRec.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) break; // 按ESC键退出循环 if (keyRec.Event.KeyEvent.bKeyDown) { ch = keyRec.Event.KeyEvent.uChar.AsciiChar; CharWindow(ch, rc); } } } pos.X = 0; pos.Y = 0; SetConsoleCursorPosition(hOut, pos); // 设置光标位置 CloseHandle(hOut); // 关闭标准输出设备句柄 CloseHandle(hIn); // 关闭标准输入设备句柄 } void CharWindow(char ch, SMALL_RECT rc) // 将ch输入到指定的窗口中 { static COORD chPos = {rc.Left+1, rc.Top+1}; SetConsoleCursorPosition(hOut, chPos); // 设置光标位置 if ((ch<0x20)||(ch>0x7e)) return; WriteConsoleOutputCharacter(hOut, &ch, 1, chPos, NULL); if (chPos.X>=(rc.Right-1)) { chPos.X = rc.Left; chPos.Y++; } if (chPos.Y>(rc.Bottom-1)) { DeleteTopLine(rc); chPos.Y = rc.Bottom-1; } chPos.X++; SetConsoleCursorPosition(hOut, chPos); // 设置光标位置 } void ControlStatus(DWORD state) // 在最后一行显示控制键的状态 { CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); COORD home = {0, bInfo.dwSize.Y-1}; WORD att0 = BACKGROUND_INTENSITY ; WORD att1 = FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_RED; FillConsoleOutputAttribute(hOut, att0, bInfo.dwSize.X, home, NULL); FillConsoleOutputCharacter(hOut, ' ', bInfo.dwSize.X, home, NULL); SetConsoleTextAttribute(hOut, att1); COORD staPos = {bInfo.dwSize.X-16,bInfo.dwSize.Y-1}; SetConsoleCursorPosition(hOut, staPos); if (state & NUMLOCK_ON) WriteConsole(hOut, "NUM", 3, NULL, NULL); staPos.X += 4; SetConsoleCursorPosition(hOut, staPos); if (state & CAPSLOCK_ON) WriteConsole(hOut, "CAPS", 4, NULL, NULL); staPos.X += 5; SetConsoleCursorPosition(hOut, staPos); if (state & SCROLLLOCK_ON) WriteConsole(hOut, "SCROLL", 6, NULL, NULL); SetConsoleTextAttribute(hOut, bInfo.wAttributes); // 恢复原来的属性 SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition); // 恢复原来的光标位置 } void DeleteTopLine(SMALL_RECT rc) { COORD crDest; CHAR_INFO chFill; SMALL_RECT rcClip = rc; rcClip.Left++; rcClip.Right--; rcClip.Top++; rcClip.Bottom--; crDest.X = rcClip.Left; crDest.Y = rcClip.Top - 1; CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); chFill.Attributes = bInfo.wAttributes; chFill.Char.AsciiChar = ' '; ScrollConsoleScreenBuffer(hOut, &rcClip, &rcClip, crDest, &chFill); } |
十、读取鼠标信息
与读取键盘信息方法相似,鼠标信息也是通过ReadConsoleInput来获取的,其MOUSE_EVENT_RECORD具有下列定义:
typedef struct _MOUSE_EVENT_RECORD { COORD dwMousePosition; // 当前鼠标位置 DWORD dwButtonState; // 鼠标按钮状态 DWORD dwControlKeyState; // 键盘控制键状态 DWORD dwEventFlags; // 事件状态 } MOUSE_EVENT_RECORD; |
#include <WINDOWS.H> #include <STDIO.H> #include <STRING.H> HANDLE hOut; HANDLE hIn; void ClearScreen(void); void DispMousePos(COORD pos); // 在最后一行显示鼠标位置 void main() { hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 hIn = GetStdHandle(STD_INPUT_HANDLE); // 获取标准输入设备句柄 WORD att = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE ; // 背景是蓝色,文本颜色是黄色 SetConsoleTextAttribute(hOut, att); ClearScreen(); // 清屏 INPUT_RECORD mouseRec; DWORD state = 0, res; COORD pos = {0, 0}; for(;;) // 循环 { ReadConsoleInput(hIn, &mouseRec, 1, &res); if (mouseRec.EventType == MOUSE_EVENT){ if (mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK) break; // 双击鼠标退出循环 pos = mouseRec.Event.MouseEvent.dwMousePosition; DispMousePos(pos); if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) FillConsoleOutputCharacter(hOut, 'A', 1, pos, NULL); } } pos.X = 0; pos.Y = 0; SetConsoleCursorPosition(hOut, pos); // 设置光标位置 CloseHandle(hOut); // 关闭标准输出设备句柄 CloseHandle(hIn); // 关闭标准输入设备句柄 } void DispMousePos(COORD pos) // 在最后一行显示鼠标位置 { CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); COORD home = {0, bInfo.dwSize.Y-1}; WORD att0 = BACKGROUND_INTENSITY ; FillConsoleOutputAttribute(hOut, att0, bInfo.dwSize.X, home, NULL); FillConsoleOutputCharacter(hOut, ' ', bInfo.dwSize.X, home, NULL); char s[20]; sprintf(s,"X = %2lu, Y = %2lu",pos.X, pos.Y); SetConsoleTextAttribute(hOut, att0); SetConsoleCursorPosition(hOut, home); WriteConsole(hOut, s, strlen(s), NULL, NULL); SetConsoleTextAttribute(hOut, bInfo.wAttributes); // 恢复原来的属性 SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition); // 恢复原来的光标位置 } void ClearScreen(void) { CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); COORD home = {0, 0}; unsigned long size = bInfo.dwSize.X * bInfo.dwSize.Y; FillConsoleOutputAttribute(hOut, bInfo.wAttributes, size, home, NULL); FillConsoleOutputCharacter(hOut, ' ', size, home, NULL); } |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。