C 的字体与颜色

micheal_wu 2000-03-02 08:50:00
如何使用C中自带的驱动去改变FONT AND COLOR!
...全文
729 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
micheal_wu 2000-03-09
  • 打赏
  • 举报
回复
首先, thanks for your answer.

but:
我要的是可以现实多种font and color?
hxb 2000-03-06
  • 打赏
  • 举报
回复
首先我要声明一下:由于对你的问题理解不是很准确,因此下面的回答不一定满足你。

一.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 <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

/* 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 <conio.h>
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 <conio.h>

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 <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

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

小结:这里所叙述的只是一小部分,要学好这方面的内容,准备一本参考手册(可用联机帮助),多上机实践.
hxb 2000-03-04
  • 打赏
  • 举报
回复
朋友,不用着急,明天我会给你一个答复。同时给你一些建议。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧