我已用如下的程序将文本的字体和长度、高度进行了转换,但还是不行,为什么?
procedure SetFont(DrawCanvas:TCanvas; const FontName:String; FontStyle:TFontStyles; FontSize:Integer; PreviewState:Boolean; PixelsPerInchX,PixelsPerInchY: Integer);
begin
try
if PreviewState=true then
FontSize:=Carry(1.0*FontSize*(PixelsPerInchY/GetDeviceCaps(PreviewForm.Canvas.Handle,logPixelsY)))
else
FontSize:=Carry(1.1*FontSize*(PixelsPerInchY/GetDeviceCaps(Printer.Canvas.Handle,logPixelsY)));
DrawCanvas.Font.Name:=FontName;
DrawCanvas.Font.Style:=FontStyle;
DrawCanvas.Font.Size:=FontSize;
except
Application.MessageBox(PChar('设置字体错误!'), szProgramTitle, MB_OK+MB_ICONWARNING);
end;
end;
function GetStrWidthInch(DrawCanvas:TCanvas; str : string; PixelsPerInchX,PixelsPerInchY: Integer):Real;
begin
try
Result:=DrawCanvas.TextWidth(Str);
Result := Result/PixelsPerInchX;
except
Application.MessageBox(PChar('计算文本宽度错误!'), szProgramTitle, MB_OK+MB_ICONWARNING);
end;
end;
function GetStrHeightInch(DrawCanvas:TCanvas; str: string; PixelsPerInchX,PixelsPerInchY: Integer):Real;
begin
try
Result:=DrawCanvas.TextHeight(Str);
Result := Result/PixelsPerInchY;
except
Application.MessageBox(PChar('计算文本高度错误!'), szProgramTitle, MB_OK+MB_ICONWARNING);
end;
end;
procedure TextAtInch(DrawCanvas:TCanvas;X1,Y1,X2,Y2:Real;const Str:String; PixelsPerInchX,PixelsPerInchY: Integer; Alignment:Alignment);
var
Px,Py:Integer;
begin
try
px:=0;
case Alignment of
taLeft:
Px:=Round(X1*PixelsPerInchX);
taCenter:
Px:=Round((X1+(X2-X1-GetStrWidthInch(DrawCanvas, str, PixelsPerInchX, PixelsPerInchY))/2)*PixelsPerInchX);
taRight:
Px:=Round((X1+X2-X1-GetStrWidthInch(DrawCanvas, str, PixelsPerInchX,PixelsPerInchY))*PixelsPerInchX);
end;
Py:=Round((Y1+(Y2-Y1-GetStrHeightInch(DrawCanvas, str, PixelsPerInchX,PixelsPerInchY))/2)*PixelsPerInchY);
DrawCanvas.TextOut(Px,Py,Str);
except
Application.MessageBox(PChar('绘制文本错误!'), szProgramTitle, MB_OK+MB_ICONWARNING);
end;
end;