旅途

如果想飞得高,就该把地平线忘掉

在C语言中显示汉字

/*此程序为建立汉字库头文件<hzk.h>*/

/*此程序要16乘16汉字库文件<hzk16>*/

#include"stdio.h"

main()

  {   int i,j,k;
     int len,qu,wei;
      long int l;
      unsigned char c;
        unsigned  char *s,*ss;
          FILE*fp,*p;
   if((fp=fopen("hzk.h","w"))==NULL)       /*在当前位置建立汉字字模头文件  ,运行后可打开此文件看看*/
      {printf("error not is <hzk.h>"); getch();}
          s="没有太大或路径名内足存够了新建菜单开始错误找不到文件请输入数字符的串鼠标光退出保存运行:?打能!,"; /*在次添加汉字*/
          ss=s;
          len=strlen(s)/2; /*获得汉字个数*/
          fprintf(fp,"int hz[%d]={\n",len+1); /*定义特征码*/
          i=0;
      ss=s;
    while(i<len)
      {l=(long)((*s)*256+(*(s+1)));/*得到特征码*/

      fprintf(fp,"%u,",l);/*写入特征码*/
                s+=2;i++;
         if(i%8==0&&i!=0){fprintf(fp,"\n");}   /*格式*/

                }
                l=(long)(*(s+1))*256+(*s);                fprintf(fp,"%u};\n\n",0);
             fprintf(fp,"unsigned char hzk_wd[%d][32]={\n",len);/*定义汉字字模*/
                
              if((p=fopen("HZK16","rb"))==NULL)  /*打开16*16汉字库*/
                {printf("error not is <hzk16>"); getch();}
     s=ss;
     j=0;
      while(1)
                { qu=(*s)-0xa0;wei=(*(s+1))-0xa0;
                 l=(long)((qu-1)*94+wei-1);
                  l=(long)(l*32);    /*得到位移量*/
  
                    fseek(p,l,0);
                    fprintf(fp,"{");
                    for(i=0;i<32;++i) /*写入汉字字模*/
                 {c=fgetc(p);           /*读入一个字节*/
                          fprintf(fp,"0x%x,",c);
                      if((i%7 )==0&&i!=0)fprintf(fp,"\n");}
                          fprintf(fp,"},\n\n");
                       j++;if(j>=len)break;
                    s+=2;  /*移到下一个汉字*/
                    }

                    fprintf(fp,"\n};\n\n",c);
    fclose(fp);
    fclose(p);
   }

----------------------------------------------------------------------------------------------------------------------

/*显示汉字函数,S为输入汉字串可有字符,x,y分别为显示位置,color为颜色*/
/*要使用此函数,要包含hzk.h头文件,且工作在图形方式*/
       print(unsigned char *s,int x,int y,int color)
         {int i,j,k;
            int xx,yy;
     long l;
     char c[2];
     unsigned char mark[]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};/*定义屏蔽码*/
     c[1]='\0';
     while(*s)
      { if((*s)>160&&(*s)<255&&(*(s+1))>160&&(*(s+1))<255)/*判断是否为汉字*/
                  {l=(*s)*256+(*(s+1));  /*得到特征码*/
                      i=0;j=0;
                  while(hz[i]!=0)  /*检索特征码*/
                   {if(hz[i]==l)
                                           {j=0;break;}
                                        ++i;}
                                 if(hz[i]!=0)   /*判断是否结束*/
                                     { if(x>623){x=1;y+=18;}/*如果到行末,则换行*/
                       yy=y;
                  for(j=0;j<32;j+=2,yy++)    /*汉字字模的32个字节,加2是每次打印一行*/
                       { for(k=0;k<8;k++)      /*每个字节表示8个点*/
            {
            if((hzk_wd[i][j]&mark[k])!=0)/*判断第一字节的第K位是否为0*/
               putpixel(x+k,yy,color);/*打印点*/
                
          if((hzk_wd[i][j+1]&mark[k])!=0)/*(j+1)判断第二字节的第K位是否为0*/
          putpixel(x+k+8,yy,color);
                                                                                  
                                                            }
                                              }

                                           }
                                 s+=2;  x+=17;/*移向下一个字符:S加2---每个汉字2个字节*/
                                              /*X加为列加,每个汉字十六列*/            
                              }

                  else       /*否则为英文字符*/
                    {c[0]=*s;
                     setcolor(color);
                      if(x>630){x=1;y+=18;}
                      outtextxy(x,y+4,c);/*打印字符*/
                       s++;x+=9;/*移向下一个字符:S加1---每个字符1个字节*/
                                /*X加9--每个字符8列*/

                      }
                   }
              }   

posted on 2007-10-12 01:41 旅途 阅读(497) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理