TC图像编程-汉字串显示【原创】
2008/03/09 14:36
/*我把这代码贴出来是学习之用,所以一些说明我也就懒得讲了*/
/*********************************
作者:HungryAnt
姓名:孙麟
E-mail:
[email protected]
QQ:517377100
blog:http://hi.baidu.com/zhongji/blog
*********************************/
#include<stdio.h>
#include<graphics.h>
/*+++++++++++++++++++++++++++汉字串显示+++++++++++++++++++++++++++++++++++++*/
FILE *zh;
int zh_print16(int x,int y,char *pix,int color){/*显示16点阵汉字*/
int i,j,k;
for(i=0;i<16;i++)
for(j=0;j<2;j++)
for(k=0;k<8;k++){
if(pix[i*2+j] & 0x80>>k)putpixel(x+j*8+k,y+i,color);
/*else putpixel(x+j*8+k,y+i,BLUE);*/
}
}
void zh_str(int x,int y,int width,unsigned char *str,int color){/*汉字和英文字串显示,width表示每一行的汉字数*/
int i=0,j=0;
long qm,wm,address;
unsigned char pix[32];/*存放点阵16*16==32*8*/
while(str[i]){
qm=(long)str[i]-0xA0;/*区码*/
i++;
wm=(long)str[i]-0xA0;/*位码*/
i++;
address=((qm-1)*94+(wm-1))*32;/*获取汉字在文件中的位置*/
fseek(zh,address,SEEK_SET);/*文件中偏移位置*/
fread(pix,32,1,zh);/*读取点阵32*8=16*16*/
zh_print16(x,y,pix,color);
x+=16;
j++;
if(j==width){
y+=16;
x-=width*16;
j=0;
}
}
}
void ha_graph(){/*自定义图形检测*/
int gdriver = DETECT, gmode, errorcode;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver, &gmode,"");
errorcode = graphresult();
if (errorcode != grOk){
printf("Graphics error: %s", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
}
/*-----------------------------------------------------------------------*/
int main(){
unsigned char str[]="哈哈,我是孙麟。我晕啊";/*unsigned很重要,不加即错*/
zh=fopen("HZK16","r");/*读汉字库*/
ha_graph();
zh_str(20,20,10,str,YELLOW);
getch();
fclose(zh);
closegraph();
}