function  RenderTxt(text:string;                 //将要写在位图画布(CANVAS)上的文字串
                    bmp:Tbitmap;                 //目标位图
                    x1,y1,x2,y2:integer;         //目标区域坐标(注意必须X1<X2,Y1<y2,X坐标正方向向右,Y坐标正方向向下)
                    LineStep:integer;            //行距(注意坐标和行距都以象素为单位)
                    fontname:string;             //所用字体名
                    fontsize:integer;            //字体尺寸
                    fontcolor:Tcolor):integer;   //字体颜色(注意颜色最好要用全红/全绿/全黄)
Var                                              //返回:<=0表示有错误,>0表示目标区域最大能容纳的字符数。
   Tx,Ty,i,L,fw,fh:integer;
   s:string[2];
begin
   result:=-1;   try
      with bmp.Canvas do
      begin
         font.Name:=fontname;
         font.Size:=fontsize;
         font.Color:=fontcolor;
      end;      fh:=bmp.Canvas.TextHeight('0123456789ABCxyz_Text Sample');      Tx:=x1;Ty:=y1;
      i:=1;L:=length(text);
      while(i<=L) do
      begin
         s:=copy(text,i,1);
         if ord(s[1])>$A0 then s:=copy(text,i,2);
         fw:=bmp.Canvas.TextWidth(s);
         if Tx+fw>x2 then
         begin
            Tx:=x1;Ty:=Ty+fh+LineStep;
            if Ty>y2 then break;
         end;
         Bmp.Canvas.TextOut(Tx,Ty,s);
         Tx:=Tx+fw;i:=i+Length(s);
      end;
      result:=i;
   except
   else
   end;end;

解决方案 »

  1.   


    function  RenderTxt(text as string,
                        bmp as form,x1 as integer,y1 as integer,x2 as integer ,y2 as integer,LineStep as integer,fontname as string,fontsize as integer,fontcolor as ole_color) as integerdim    Tx as integer,Ty as integer ,i as integer ,L as integer ,fw as integer,fh as integer
      dim s as string
    dim result as long
       result=-1  dim font as stdfont
          
             bmp.font.Name=fontname
             bmp.font.Size=fontsize
    '         bmp.foreColor=fontcolor
    dim fh as integer
          fh=bmp.textheight
          Tx=x1:Ty=y1
          i=1:L=len(text)
          do while(i<=L)
             s=mid(text,i,1)
             'if ord(s[1])>$A0 then s:=copy(text,i,2);'这里是取得一个字符,如果是大于&H80就取两字节,在vb里不需要这么做。
             fw=bmp.textwidth
             if Tx+fw>x2 then
                Tx=x1
                Ty=Ty+fh+LineStep
                if Ty>y2 then exit for
             end if
             bmp.currentx=tx*15'这里我也不知道delphi是不是采用的pixels,vb用的是twips,1 pixels一般等于15 twips,所以这里转换了一下
             bmp.print s
             Tx=Tx+fw
             i=i+Len(s)
          loop
          result=iend function
    ------------------
    我这里没装vb,所以没法调试,简单的翻译了一下,你自己调试一下。再不行,自己修改一下应该可以了。关键的地方都改好了。第二个参数,传的是form,也可以改成picturebox等等。
      

  2.   

    function  RenderTxt(text as string,
                        bmp as form,x1 as integer,y1 as integer,x2 as integer ,y2 as integer,LineStep as integer,fontname as string,fontsize as integer,fontcolor as ole_color) as integerdim    Tx as integer,Ty as integer ,i as integer ,L as integer ,fw as integer,fh as integer
      dim s as string
    dim result as long
       result=-1      
             bmp.font.Name=fontname
             bmp.font.Size=fontsize
             bmp.foreColor=fontcolor
          fh=bmp.textheight
          Tx=x1:Ty=y1
          i=1:L=len(text)
          do while(i<=L)
             s=mid(text,i,1)
             'if ord(s[1])>$A0 then s:=copy(text,i,2);'这里是取得一个字符,如果是大于&H80就取两字节,在vb里不需要这么做。
             fw=bmp.textwidth
             if Tx+fw>x2 then
                Tx=x1
                Ty=Ty+fh+LineStep
                if Ty>y2 then exit for
             end if
             bmp.currentx=tx*15'这里我也不知道delphi是不是采用的pixels,vb用的是twips,1 pixels一般等于15 twips,所以这里转换了一下
             bmp.print s
             Tx=Tx+fw
             i=i+Len(s)
          result=i
          loopend function呵呵,在线修改的,输入框太小,没法看全部代码,稍微检查了下,发现两个问题,这是修改后的代码。估计还会有错,你再改改。
      

  3.   

    fh=bmp.textheight
    ,z这个不对,bmp:Tbitmap; 改为bmp as form对吗
      

  4.   

    我不懂delphi,不知道tbitmap类型是甚么,但看后面的程序里的调用,估计和vb的form差不多,所以就写成as form,你把代码复制到vb里运行起来看看。就知道对不对了。再修改一个地方 Tx=x1*15
    Ty=Ty+(fh+LineStep)*15
      

  5.   

    晕倒,改错了
    应该是
    bmp.currentx=tx*15
    bmp.currenty=ty*15