c 语言中怎么改变字体颜色,使用C中自带的驱动去改变字体和颜色

本文介绍了如何在C语言中改变字体和颜色,包括文本模式和图形模式下的设置。通过示例代码展示了如何使用settextstyle、textcolor、textbackground等函数来调整字体样式、颜色,并提供了在图形模式下使用setcolor和setbkcolor改变屏幕颜色的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

分类:计算机等级

|

更新时间:2016-07-08|

来源:转载

一.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输出。

二.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函数

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值