/* the names of the text styles supported */ char *fname[] = { "DEFAULT font", "TRIPLEX font", "SMALL font", "SANS SERIF font", "GOTHIC font" };
int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int style, midx, midy; int size = 1; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { PRintf("Graphics error: %s/n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } midx = getmaxx() / 2; midy = getmaxy() / 2; settextjustify(CENTER_TEXT, CENTER_TEXT); /* loop through the available text styles */ for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++) { cleardevice(); if (style == TRIPLEX_FONT) size = 4; /* select the text style */ settextstyle(style, HORIZ_DIR, size); /* output a message */ outtextxy(midx, midy, fname[style]); getch(); } /* clean up */ closegraph(); return 0; }
二.C中的颜色问题 颜色问题不是一两句话能将清楚的,建议你了解一点计算机是如何处理颜色的。下面的回答主要以TC3.0中的例子为主。 颜色可分为前景色(即字体颜色)和背景颜色。在C中颜色的设置分为文本模式下的设置和图形模式下的设置。下面先将在文本模式下的设置 1.文本模式下的设置 先看文本模式下的色彩设定 7 6 5 4 3 2 1 0 (B b b b f f f f) 上面是一个字节(共有8位),0——3位设定前景色,4——6为设定背景色。第7为控制字符是否闪烁。请看下例
例二.
#include int main(void) { int i, j; clrscr(); for (i=0; i<9; i++) { textcolor(i+1); textbackground(i); for (j=0; j<80; j++) cprintf("C"); cprintf("/r/n"); } return 0; } 原代码解释:textcolor函数用来设定字符颜色,由于它只设定字符颜色,因此参数中(一个整数)只有0——3和第7为有效。textbackground函数用来设定背景色,参数中4——6位有效。
要想用一个函数同时设定前景色和背景色以及是否闪烁,可用textattr函数,看下例 例三.
clude
int main(void) { int i; clrscr(); for (i=0; i<9; i++) { textattr(i + ((i+1) << 4)); cprintf("This is a test/r/n"); } return 0; }
提醒:在文本模式下输出字符用cprintf函数,不用printf函数。
2.图形模式下的设置
例四.
#include #include #include #include
int main(void) { /* select a driver and mode that supports */ /* multiple drawing colors. */ int gdriver = EGA, gmode = EGAHI, errorcode; int color, maxcolor, x, y; char msg[80]; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s/n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ }
/* maximum color index supported */ maxcolor = getmaxcolor();
/* for centering text messages */ settextjustify(CENTER_TEXT, CENTER_TEXT); x = getmaxx() / 2; y = getmaxy() / 2;
/* loop through the available colors */ for (color=1; color<=maxcolor; color++) { /* clear the screen */ cleardevice();
/* select a new background color */ setcolor(color); setbkcolor(color+1); /* output a messsage */ sprintf(msg, "Color: %d", color); outtextxy(x, y, msg); getch(); } /* clean up */ closegraph(); return 0; }