一.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的聯(lián)機(jī)幫助文件)
例一.
#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函數(shù),style參數(shù)是選擇字體的,關(guān)于其它的參數(shù)說明可參考聯(lián)機(jī)幫助。outtextxy函數(shù)用來輸出文本。
如果老兄是想顯示各種不同字體的漢字話,那得麻煩點(diǎn)。這里不想多說,只提供兩種方案
(1) 利用ucdos的漢字特顯技術(shù),用c中的printf函數(shù)帶上特殊參數(shù)即可,具體可參考有關(guān)資料(比如《電腦愛好者》中曾講過)
(2) 在圖形模式下,調(diào)用字體文件,用c函數(shù)putpixel輸出。
二.c中的顏色問題
顏色問題不是一兩句話能將清楚的,建議你了解一點(diǎn)計(jì)算機(jī)是如何處理顏色的。下面的回答主要以tc3.0中的例子為主。
顏色可分為前景色(即字體顏色)和背景顏色。在c中顏色的設(shè)置分為文本模式下的設(shè)置和圖形模式下的設(shè)置。下面先將在文本模式下的設(shè)置
1.文本模式下的設(shè)置
先看文本模式下的色彩設(shè)定
7 6 5 4 3 2 1 0
(b b b b f f f f)
上面是一個(gè)字節(jié)(共有8位),0——3位設(shè)定前景色,4——6為設(shè)定背景色。第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函數(shù)用來設(shè)定字符顏色,由于它只設(shè)定字符顏色,因此參數(shù)中(一個(gè)整數(shù))只有0——3和第7為有效。textbackground函數(shù)用來設(shè)定背景色,參數(shù)中4——6位有效。
要想用一個(gè)函數(shù)同時(shí)設(shè)定前景色和背景色以及是否閃爍,可用textattr函數(shù),看下例
例三.
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函數(shù),不用printf函數(shù)。
2.圖形模式下的設(shè)置
例四.
#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設(shè)定字體顏色,setbkcolor設(shè)定背景色
提醒:輸出字符用outtextxy函數(shù)
小結(jié):這里所敘述的只是一小部分,要學(xué)好這方面的內(nèi)容,準(zhǔn)備一本參考手冊(可用聯(lián)機(jī)幫助),多上機(jī)實(shí)踐.
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的聯(lián)機(jī)幫助文件)
例一.
#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函數(shù),style參數(shù)是選擇字體的,關(guān)于其它的參數(shù)說明可參考聯(lián)機(jī)幫助。outtextxy函數(shù)用來輸出文本。
如果老兄是想顯示各種不同字體的漢字話,那得麻煩點(diǎn)。這里不想多說,只提供兩種方案
(1) 利用ucdos的漢字特顯技術(shù),用c中的printf函數(shù)帶上特殊參數(shù)即可,具體可參考有關(guān)資料(比如《電腦愛好者》中曾講過)
(2) 在圖形模式下,調(diào)用字體文件,用c函數(shù)putpixel輸出。
二.c中的顏色問題
顏色問題不是一兩句話能將清楚的,建議你了解一點(diǎn)計(jì)算機(jī)是如何處理顏色的。下面的回答主要以tc3.0中的例子為主。
顏色可分為前景色(即字體顏色)和背景顏色。在c中顏色的設(shè)置分為文本模式下的設(shè)置和圖形模式下的設(shè)置。下面先將在文本模式下的設(shè)置
1.文本模式下的設(shè)置
先看文本模式下的色彩設(shè)定
7 6 5 4 3 2 1 0
(b b b b f f f f)
上面是一個(gè)字節(jié)(共有8位),0——3位設(shè)定前景色,4——6為設(shè)定背景色。第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函數(shù)用來設(shè)定字符顏色,由于它只設(shè)定字符顏色,因此參數(shù)中(一個(gè)整數(shù))只有0——3和第7為有效。textbackground函數(shù)用來設(shè)定背景色,參數(shù)中4——6位有效。
要想用一個(gè)函數(shù)同時(shí)設(shè)定前景色和背景色以及是否閃爍,可用textattr函數(shù),看下例
例三.
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函數(shù),不用printf函數(shù)。
2.圖形模式下的設(shè)置
例四.
#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設(shè)定字體顏色,setbkcolor設(shè)定背景色
提醒:輸出字符用outtextxy函數(shù)
小結(jié):這里所敘述的只是一小部分,要學(xué)好這方面的內(nèi)容,準(zhǔn)備一本參考手冊(可用聯(lián)機(jī)幫助),多上機(jī)實(shí)踐.