C语言中的屏幕字符输出函数有多个,最常用的有printf、?cprintf?等,其中,printf 是一个基本的输出函数,而?cprintf则带有字符的屏幕显示属性,但需要其他函数的支持。 ??? 显示一行文本,应首先知道该文本的各种属性,如Font, Color , BackStyle等。 ??? 为方便,可采用一个函数来实现文本的输出: SayText(row, col , font, backstyle, string, … …) ??? 以上的方法对于一个简单的应用是完全可行的,但对于一个系统而言,此种做法则欠妥。 ??? 在一个大型的应用系统中,用户可能需要不断的进行文本的数据输出,如果在进行多个文本数据段的输出时,假如其中的文本属性是相同的,则用户是不需要继续设置文本属性的,系统也不应该进行多次的属性设置。 ???? 因此,在进行系统设计时,应事先建立一数据结构存储文本属性即文本属性结构,用以存放当前输出文本属性,使用户对于文本属性的操作直接对应于文本属性结构,系统中文本的输出则直接从文本属性结构中获取数据。这样,将文本的输出分解为两部分:文本属性操作及文本内容的输出。 ?? Struct text ?? { ?? ?? Int SayColor; ????? Int GetColor; ?? }TextProp; ??? 由于在C中,文本的字体及显示背景等在文本方式下采用C提供的函数很难处理,因此我们在定义文本属性时,只定义了文本的显示颜色。 由于文本在进行处理时,有两种方式,一为显示,二为获取,因此定义两种颜色属性。
? 一个完整的字符显示函数 #include <string.h> #include <stdio.h> #include <conio.h> #include <dos.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 MAX??? 80
struct text { ?? int SayColor; ?? int GetColor; };
typedef struct text TextProp;
TextProp pText;
void MoveCursor (int x,int y) { ??? union REGS regs ;
??? regs.h.ah=2 ;??? /* set cursor postion */ ??? regs.h.dh=x ; ??? regs.h.dl=y ; ??? regs.h.bh=0 ;??? /* video page 0 */ ??? int86(0x10,®s,®s); }
void SayCharColor(char cc,int color) { ??? union REGS regs ; ??? regs.h.ah=9 ; ??? regs.x.cx=1 ; ??? regs.h.al=cc ; ??? regs.h.bl=color ; ??? regs.h.bh=0 ; ??? int86(0x10,®s,®s); }
void SayStringRowCol(int row,int col,char *str) { ??? int tLen ; ??? int tIdx ; ??? char cc ;
??? tLen=strlen(str); ??? for(tIdx=1;tIdx<=tLen;tIdx++) ??? { ??????? MoveCursor(row,col+tIdx); ??????? SayCharColor(str[tIdx-1],pText.SayColor); ??? } }
void SetSayColor(int color) { ??? pText.SayColor=color ; }
void ClearScreen() { ??? textattr(pText.SayColor); ??? clrscr(); }
int main(void) { ??? SetSayColor(F_RED|F_INTENSE|B_BLUE|B_GREEN|B_INTENSE); ??? ClearScreen(); ??? SayStringRowCol(15,10,"Hello,World!"); ??? SayStringRowCol(18,10,"你好,世界!"); ??? getch(); ??? return 0 ; }
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。