七、滚动和移动
scrollconsolescreenbuffer是实现文本区滚动和移动的api函数。它可以将指定的一块文本区域移动到另一个区域,被移空的那块区域由指定字符填充。函数的原型如下:
bool scrollconsolescreenbuffer( handle hconsoleoutput, // 句柄 const small_rect* lpscrollrectangle, // 要滚动或移动的区域 const small_rect* lpcliprectangle, // 裁剪区域 coord dwdestinationorigin, // 新的位置 const char_info* lpfill // 填充字符 ); | 利用这个api函数还可以实现删除指定行的操作。下面来举一个例子,程序如下:
#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); }
| 程序中,实现删除行的操作deleteline的基本原理是:首先将裁剪区域和移动区域都设置成指定行row(包括该行)以下的控制台窗口区域,然后将移动的位置指定为(0, row-1)。这样,超出裁剪区域的内容被裁剪掉,从而达到删除行的目的。
需要说明的是,若裁剪区域参数为null,则裁剪区域为整个控制台窗口。
|