在VC中直接写屏的方法
为兼容vc6和C,以下代码用纯C旧语法:程序代码:
#include <Windows.h> #include <stdlib.h> #include <malloc.h> #include <string.h> #include <conio.h> int main(void) { HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); const char* str = "Hello, 欢迎你进入编程论坛!"; int length = strlen(str); CHAR_INFO* text = NULL; SMALL_RECT region; COORD bufferSize; COORD bufferCoord; int index; region.Top = 5; region.Left = 5; region.Bottom = region.Top + 4; region.Right = region.Left + 20; bufferSize.X = region.Right - region.Left + 1; bufferSize.Y = region.Bottom - region.Top + 1; bufferCoord.X = 0; bufferCoord.Y = 0; text = (CHAR_INFO*)calloc(bufferSize.X * bufferSize.Y, sizeof(CHAR_INFO)); for (index = 0; index < bufferSize.X * bufferSize.Y; ++index) { if (index < length) { text[index].Char.AsciiChar = str[index]; text[index].Attributes = (8 + (index + 1) % 6) + (4 << 4); } else { text[index].Char.AsciiChar = ' '; text[index].Attributes = 4 << 4; } } WriteConsoleOutputA(output, text, bufferSize, bufferCoord, ®ion); delete[] text; _getch(); return EXIT_SUCCESS; }
运行效果:
请留意:
1.画面中光标是没动的
2.文本有折行
3.中文可以半个字符变色
[ 本帖最后由 TonyDeng 于 2015-7-9 09:38 编辑 ]