主题:TC3.0环境下显示汉字程序
*hzk12文件大小192kb,如果找不到地方下载可以向本人索要.
*作者:meteor135(流星雨) meteor@mail.biti.edu.cn, smith_135@163.com
*完成时间:2003年8月14日
*/
#include <stdio.h>//for FILE
#include <graphics.h>//for graphics
#define GRAPH_DVR_PATH "F:\\TC\\BGI" //请将此路径改为你本机环境对应的BGI路径
void get_hz(FILE* hzk_p, char incode[],char bytes[]);
void dis_hz(FILE* hzk_p, int x, int y, char code[], int color,int expandBy);
void put_hz(char *s,int x, int y, int color, int wordSpace, int lineSpace, int direction, int expandBy, int lineDes);
/**parameter @s: pointer to the hanzi string to show
* parameter @x: x coordinates the s start showing in screen
* parameter @y: y coordinates the s start showing in screen
* parameter @color: string s's color
* parameter @wordSpace: word space
* parameter @lineSpace: line space
* parameter @direction: the direction at which string s to show, 0:left->right,others: up->down
* parameter @expandBy: the word will be expanded by it as multiple.
* parameter @lineDes: when direction is not 0, lineDes will control the string lines descending direction.
*/
void main()
{
char *s="春眠不觉晓 处处闻啼鸟 夜来风雨声 花落知多少";
int driver = DETECT;
int mode = 0;
initgraph(&driver,&mode,GRAPH_DVR_PATH);
put_hz(s,20,130,WHITE,16,30,0,2,0);
put_hz(s,250,230,GREEN,16,30,1,1,1);
put_hz(s,340,230,YELLOW,16,30,1,1,-1);
getch();
closegraph();
}
void put_hz(char *s,int x, int y, int color, int wordSpace, int lineSpace, int direction, int expandBy,int lineDes)
{
int x0 = x, y0 = y;
FILE *hzk_p;
if(!(hzk_p=fopen("hzk12","rb")))
{
printf("The file HZK12 does not exist! press any key to quit\n");
return;
}
while(*s!=NULL)
{
while(*s!=NULL&&x<640-12*expandBy&&y<480-12*expandBy&&x>=0&&y>=0)
{
if(*s!=32) //ignore space
dis_hz(hzk_p,x,y,s,color,expandBy);
if(direction == 0)
x += wordSpace*expandBy;
else
y += wordSpace*expandBy;
s+=2;
}
if(direction == 0)
{
x = x0;
y += lineSpace*expandBy;
}
else
{
y = y0;
x += lineDes*lineSpace*expandBy;
}
}
}
void get_hz(FILE* hzk_p, char incode[],char bytes[])
{
unsigned char qh, wh;
unsigned long offset;
qh = incode[0] - 0xa0;
wh = incode[1] - 0xa0;
offset = (94*(qh-1)+(wh-1))*24L;
fseek(hzk_p,offset,SEEK_SET);
fread(bytes,24,1,hzk_p);
}
void dis_hz(FILE* hzk_p, int x, int y, char code[], int color, int expandBy)
{
int i, j, m, n;
char mat[24];
get_hz(hzk_p,code,mat);
for(i=0; i< 12; ++i)
{
for(j = 0; j < 12; ++j)
{
if(((128>>j%8)&mat[2*i+j/8])!=NULL)
{
for(m=0; m<expandBy; m ++)
{
for(n=0;n<expandBy; n++)
{
putpixel(x+expandBy*j+m,y+expandBy*i+n,color);
}
}
}
}
}
}