/* 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("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; }