环境D2009移植D7的小票打印程序,D7中一切正常
D2009中直接打印出乱码,不知是何情况。上程序uses
...Printers;
.
.
.
procedure TForm1.Button1Click(Sender: TObject);
var
  F_file:TextFile;
begin
          with Printer do
            begin              AssignPRN(f_file);
              Rewrite(f_file);              Canvas.Font.Name:= '黑体';
              Canvas.Font.Size:=15;
              Canvas.Font.Style:=[fsBold];
              Writeln(f_file,'测试文字');
              Writeln(f_file,'');              CloseFile(f_file);            end;//with
end;

解决方案 »

  1.   

    Canvas.Font.Name:= '黑体'; 
      

  2.   

    试试这样看行不行:
    var 
      F_file:TextFile; 
      s:AnsiString;
    begin 
         s:='测试文字';
              with Printer do 
                begin               AssignPRN(f_file); 
                  Rewrite(f_file);               Canvas.Font.Name:= '黑体'; 
                  Canvas.Font.Size:=15; 
                  Canvas.Font.Style:=[fsBold]; 
                  Writeln(f_file,s); 
                  //Writeln(f_file,'');               CloseFile(f_file);           end;//with 
    end;
      

  3.   

    用了3楼方法,仍然是乱码,
    用这种方法无论要不要
                  Canvas.Font.Name:= '黑体'; 
                  Canvas.Font.Size:=15; 
                  Canvas.Font.Style:=[fsBold]; 
    打印出来的均是乱码
    Writeln(f_file,s); 我也感觉是字符集的问题,但不知道怎么处理。
      

  4.   

    是unicode問題,D2009下默認為unicodestring。
    但printer比較特殊,這樣處理:
    { Called when a Write or Writeln is applied to a printer file. The calls
      PrnString to write the text in the buffer to the printer. }
    function PrnOutput(var F: TTextRec): Integer;
    begin
      with F do
      begin
        PrnString(PrnRec(UserData), PAnsiChar(BufPtr), BufPos);
        BufPos := 0;
        Result := 0;
      end;
    end;所以應先聲明一個ansiString類型的變量,來接收字串。或直接強制轉換PANsichar
      

  5.   

    3楼的方法已经定义ansistring类型了,
    用PANsichar()强转我当时就试过了,仍然不成
      

  6.   

    设定 Printer.Canvas.Font.Charset 试试
      

  7.   

      s:WideString;试一下,再不行看看打印驱动程序
      

  8.   

    delphi2009问题不少,个人感觉d2010还不错