linux 用c程序写整个控制台
我们知道使用
printf("test \n");
这样只能输出一行。而我们看到过很多程序,启动后程序占满整个控制台窗口,这是怎么做到的呢?
原理就是先清掉整个整个屏幕
system( "clear");
让后画满整个屏幕
这个也是通过
printf()
这个函数实现。
然后
画完一屏之后,
再次调用 system("clear");清屏,然后再画。
这个过程可以在工作线程中实现,主线程读取用户输入,用来控制屏幕画什么样的内容。
直接上程序,下面的程序使用下面的命令,编译
$ gcc draw_console.c -lpthread -o draw_console
运行程序:
$ ./draw_console
按s停止刷新,按r继续刷新,按q退出程序。
代码:
#include
#include
#include // for future trials with wide chars
#include // to set Unicode locale
#include
#include
#include
#include
#include
#define RB "\e(0\x6a\e(B" // 188 Right Bottom corner
#define RT "\e(0\x6b\e(B" // 187 Right Top corner
#define LT "\e(0\x6c\e(B" // 201 Left Top cornet
#define LB "\e(0\x6d\e(B" // 200 Left Bottom corner
#define MC "\e(0\x6e\e(B" // 206 Midle Cross
#define HL "\e(0\x71\e(B" // 205 Horizontal Line
#define LC "\e(0\x74\e(B" // 204 Left Cross
#define RC "\e(0\x75\e(B" // 185 Right Cross
#define BC "\e(0\x76\e(B" // 202 Bottom Cross
#define TC "\e(0\x77\e(B" // 203 Top Cross
#define VL "\e(0\x78\e(B" // 186 Vertical Line
#define SP " " // space string
#define TRUE 1
#define FALSE 0
// Move the whole box to the right 'RIGHT' number of spaces
#define RIGHT 8
// IF DBL_BOX TRUE draw a second box around
#define DBL_BOX TRUE
int g_bUpdate = 1;
/*
*/
//print number of box chars – two string variables
// if different able to print mixed box chars
// eg. first right cross, horizontal, right cross ect
// 'i' times.
void box_print(char *def_str1, char *def_str2, int i);
void print_nl(int i); // print 'i' number of newlines
// print string cgars withi