使用C中自带的驱动去改变字体和颜色

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

一.C语言中字体的问题

C语言中有两种显示方式,即文本方式和图形方式。就我所知,只能在图形方式下控制字体.

先看一下C中定义的几种字体

名称 索引值 字体说明

DEFAULT_FONT 0 8x8 bit-mapped font

TRIPLEX_FONT 1 Stroked triplex font

SMALL_FONT 2 Stroked small font

SANS_SERIF_FONT 3 Stroked sans-serif font

GOTHIC_FONT 4 Stroked gothic font

(字体说明中的英文解释无须明白,在例子的演示中去看)

请看例子(摘自TC3.0的联机帮助文件)

例一.

#include

#include

#include

#include

/* 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;

}

原代码讲解:

上面的例子中,控制字体用settextstyle函数,style参数是选择字体的,关于其它的参数说明可参考联机帮助。Outtextxy函数用来输出文本。

假如老兄是想显示各种不同字体的汉字话,那得麻烦点。这里不想多说,只提供两种方案

(1) 利用UCDOS的汉字特显技术,用C中的printf函数带上非凡参数即可,具体可参考有关资料(比如《电脑爱好者》中曾讲过)

(2) 在图形模式下,调用字体文件,用C函数putpixel输出。推荐参考书

[1]《C语言最新编程技巧200例》(修订本)鲁沐浴主编 电子工业出版社 1997

二.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;

}

原代码说明:setcolor设定字体颜色,setbkcolor设定背景色

提醒:输出字符用outtextxy函数

小结:这里所叙述的只是一小部分,要学好这方面的内容,预备一本参考手册(可用联机帮助),多上机实践.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航