国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用C中自帶的驅(qū)動(dòng)去改變字體和顏色

2019-11-17 05:19:16
字體:
供稿:網(wǎng)友

  一.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

(字體說明中的英文解釋無須明白,在例子的演示中去看)

請(qǐng)看例子(摘自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("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輸出。推薦參考書
[1]《C語言最新編程技巧200例》(修訂本)魯沐浴主編 電子工業(yè)出版社 1997


二.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為控制字符是否閃爍。請(qǐng)看下例

例二.

#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)容,預(yù)備一本參考手冊(cè)(可用聯(lián)機(jī)幫助),多上機(jī)實(shí)踐.


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 正安县| 祁门县| 高邑县| 调兵山市| 通江县| 改则县| 西林县| 固镇县| 平武县| 正镶白旗| 五河县| 武义县| 平远县| 浏阳市| 寿光市| 岳西县| 商南县| 华池县| 承德市| 镇赉县| 三穗县| 朝阳县| 普格县| 鸡泽县| 海阳市| 云南省| 寿宁县| 横峰县| 噶尔县| 汝阳县| 石棉县| 昂仁县| 米脂县| 内乡县| 长寿区| 呼和浩特市| 海兴县| 土默特左旗| 华宁县| 自贡市| 新干县|