首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于控制台的通用文本输出函数设计

基于控制台的通用文本输出函数设计

原创
作者头像
鲁郭大侠
修改2020-03-09 11:19:29
5660
修改2020-03-09 11:19:29
举报
文章被收录于专栏:高级程序设计高级程序设计

代码如下:

/* 包含头文件 */

#include <stdio.h>

#include <conio.h>

/* 程序使用颜色定义 */

#define F_BLACK 0

#define F_BLUE 1

#define F_GREEN 2

#define F_RED 4

#define F_INTENSE 8

#define F_WHITE 15

#define B_BLACK 0

#define B_BLUE 16

#define B_GREEN 32

#define B_RED 64

#define B_INTENSE 128

#define B_WHITE 240

/* 数据结构定义 */

#define MaxCharsEachLine 80 /*一行所允许显示的最大字符*/

int CharSayColor; /* 基于显示的文本色 */

int CharGetColor; /* 基于输入的文本色 */

/* 应用函数说明 */

int GetCharSayColor(void); /* 得到当前默认的字符输出色 */

int GetCharSayForeColor(void); /* 得到当前默认的字符输出前景色 */

int GetCharSayBackColor(void); /* 得到当前默认的字符输出背景色 */

int GetCharGetColor(void); /* 得到当前默认的字符输入色 */

int GetCharGetForeColor(void); /* 得到当前默认的字符输入前景色 */

int GetCharGetBackColor(void); /* 得到当前默认的字符输入背景色 */

int SetCharSayColor(int color); /* 设置当前默认的字符输出色 */

int SetCharSayForeColor(int color); /* 设置当前默认的字符输出前景色 */

int SetCharSayBackColor(int color); /* 设置当前默认的字符输出背景色 */

int SetCharGetColor(int color); /* 设置当前默认的字符输入色 */

int SetCharGetForeColor(int color); /* 设置当前默认的字符输入前景色 */

int SetCharGetBackColor(int color); /* 设置当前默认的字符输入背景色 */

void SetColorAttrib(int color); /* 按照当前颜色设置文本输出颜色属性 */

void SetSayColorAttrib(void); /* 设置当前字符输出色为SayColor */

void SetGetColorAttrib(void); /*设置当前字符输出色为GetColor */

int PrintStr(char str); /* 在当前位置输出字符串str */

int PrintStrRow(int row,char *str); /*在row,1中输出字符串str */

int PrintStrRowCol(int row,int col,char *str); /*在row,col输出字符串str */

int PrintStrRowCenter(int row,char *str); /*在row行中间输出字符串str */

/* -------------------- */

/* 应用函数代码 */

/* -------------------- */

/* 得到当前默认的字符输出色 */

int GetCharSayColor(void)

{

return CharSayColor ;

}

/* 得到当前默认的字符输出前景色 */

int GetCharSayForeColor(void)

{

return CharSayColor & 0X0f;

}

/* 得到当前默认的字符输出背景色 */

int GetCharSayBackColor(void)

{

return CharSayColor & 0Xf0;

}

/* 得到当前默认的字符输入色 */

int GetCharGetColor(void)

{

return CharGetColor ;

}

/* 得到当前默认的字符输入前景色 */

int GetCharGetForeColor(void)

{

return CharGetColor & 0X0f;

}

/* 得到当前默认的字符输入背景色 */

int GetCharGetBackColor(void)

{

return CharGetColor & 0Xf0;

}

/* 按照当前颜色设置文本输出颜色属性 */

void SetColorAttrib(int color)

{

textattr(color);

}

/* 设置当前字符输出色为SayColor */

void SetSayColorAttrib(void)

{

SetColorAttrib(CharSayColor);

}

/*设置当前字符输出色为GetColor */

void SetGetColorAttrib(void)

{

SetColorAttrib(CharGetColor);

}

/* 设置当前默认的字符输出色 */

int SetCharSayColor(int color)

{

CharSayColor=color;

SetColorAttrib(CharSayColor);

}

/* 设置当前默认的字符输出前景色 */

int SetCharSayForeColor(int color)

{

CharSayColor= CharSayColor & 0x0f | color;

SetColorAttrib(CharSayColor);

}

/* 设置当前默认的字符输出背景色 */

int SetCharSayBackColor(int color)

{

CharSayColor= CharSayColor & 0xf0 | color;

SetColorAttrib(CharSayColor);

}

/* 设置当前默认的字符输入色 */

int SetCharGetColor(int color)

{

CharGetColor= color;

SetColorAttrib(CharGetColor);

}

/* 设置当前默认的字符输入前景色 */

int SetCharGetForeColor(int color)

{

CharGetColor= CharGetColor & 0x0f | color;

SetColorAttrib(CharGetColor);

}

/* 设置当前默认的字符输入背景色 */

int SetCharGetBackColor(int color)

{

CharGetColor= CharGetColor & 0xf0 | color;

SetColorAttrib(CharGetColor);

}

/* 在当前位置输出字符串str */

int PrintStr(char str)

{

cprintf("%s",str);

}

/*在row,1中输出字符串str */

int PrintStrRow(int row,char *str)

{

gotoxy(1,row);

cprintf("%s",str);

}

/*在row,col输出字符串str */

int PrintStrRowCol(int row,int col,char *str)

{

gotoxy(col,row);

cprintf("%s",str);

}

/*在row行中间输出字符串str */

int PrintStrRowCenter(int row,char *str)

{

int col;

col= (MaxCharsEachLine – strlen(str))/2;

if (col<=0)

col=1;

PrintStrRowCol(row,col,str);

}

/*主函数*/

void main(void)

{

int color;

char str[40]= "Hello,World! ";

color=F_BLUE|B_GREEN;

SetCharGetColor(color);

clrscr();

PrintStr(str);

PrintStrRow(3,str);

PrintStrRowCol(6,10,str);

PrintStrRowCenter(12,str);

Sprintf(str, "Color=%d", GetCharGetColor());

PrintStrRowCenter(14,str);

Sprintf(str, "ForeColor=%d", GetCharGetForeColor());

PrintStrRowCenter(16,str);

Sprintf(str, "BackColor=%d", GetCharGetBackColor());

PrintStrRowCenter(18,str);

}

/*说明*/

以上函数的定义,只是从程序设计的角度进行分析设计,这种设计思路符合多代码行的程序设计。

仅供参考!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码如下:
  • /* 程序使用颜色定义 */
  • /* 数据结构定义 */
  • /* 应用函数说明 */
  • /*主函数*/
  • /*说明*/
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档