用C中自带的驱动改变字体和颜色
来源:优易学  2011-2-17 10:36:16   【优易学:中国教育考试门户网】   资料下载   IT书店
  一.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函数
  小结:这里所叙述的只是一小部分,要学好这方面的内容,准备一本参考手册(可用联机帮助),多上机实践.

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训