使用GDI和GDI +绘制旋转多行文本

Mar*_*tin 0 delphi text gdi+ gdi multiline

在我的Delphi应用程序中,我需要使用GDI和GDI +绘制多行文本.我有这些物品:

  1. 要绘制的文字;
  2. 旋转角度(文字可能旋转);
  3. 最大宽度(想象一下包含文本的矩形,我有矩形宽度的限制但不是矩形高度);
  4. 字体名称和文字高度;

有没有一种简单的方法可以用GDI和GDI +绘制这个文本?我无法找到关于它的GDI和GDI +功能.

Dav*_*nan 5

TL; 博士

将graphics32与GR32_Text一起使用.


对于GDI,最简单的方法是使用字体的擒纵和方向属性.例如:

procedure TForm1.PaintBox1Paint(Sender: TObject);
const
  Text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do '
    + 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim '
    + 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
    + 'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
    + 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur '
    + 'sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt '
    + 'mollit anim id est laborum.';
var
  angle: Integer;
  Canvas: TCanvas;
  lf: LOGFONT;
  R: TRect;
begin
  Canvas := PaintBox1.Canvas;

  Canvas.Brush.Style := bsClear; // Set the brush style to transparent.
  lf := Default(LOGFONT);
  lf.lfHeight := 20;
  lf.lfCharSet := DEFAULT_CHARSET;
  lf.lfFaceName := 'Times New Roman';

  angle := 15;
  lf.lfEscapement := 10*angle;//lfEscapement measured in 1/10th of degree
  lf.lfOrientation := lf.lfEscapement;
  Canvas.Font.Handle := CreateFontIndirect(lf);
  R := PaintBox1.ClientRect;
  inc(R.Top, 200);
  DrawText(Canvas.Handle, Text, -1, R, DT_NOCLIP or DT_WORDBREAK);
end;
Run Code Online (Sandbox Code Playgroud)

这产生了相当奇怪的布局:

在此输入图像描述

使用SetWorldTransform提供了不同的布局,虽然质量仍然很差:

procedure TForm1.PaintBox1Paint(Sender: TObject);
const
  Text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do '
    + 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim '
    + 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
    + 'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
    + 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur '
    + 'sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt '
    + 'mollit anim id est laborum.';
var
  angle: Integer;
  Transform: TXForm;
  Canvas: TCanvas;
  lf: LOGFONT;
  R: TRect;
begin
  Canvas := PaintBox1.Canvas;

  angle := 15;
  Transform := Default(TXForm);
  SinCos(DegToRad(-angle), Transform.eM12, Transform.eM11);
  Transform.eM22 := Transform.eM11;
  Transform.eM21 := -Transform.eM12;

  SetGraphicsMode(Canvas.Handle, GM_ADVANCED);
  SetWorldTransform(Canvas.Handle, Transform);

  Canvas.Brush.Style := bsClear; // Set the brush style to transparent.
  lf := Default(LOGFONT);
  lf.lfHeight := 20;
  lf.lfCharSet := DEFAULT_CHARSET;
  lf.lfFaceName := 'Times New Roman';
  Canvas.Font.Handle := CreateFontIndirect(lf);
  R := PaintBox1.ClientRect;
  inc(R.Top, 200);
  inc(R.Left, Round(200*Transform.eM12));
  DrawText(Canvas.Handle, Text, -1, R, DT_NOCLIP or DT_WORDBREAK);
end;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

坦率地说,我认为使用这两种方法都不会取得好成绩.如果我是你,我会使用一个很好的库,如graphics32和Angus Johnson的GR32_Text:

在此输入图像描述


对于GDI +,结果与GDI非常相似.示例代码为:

uses
  GDIPAPI, GDIPOBJ;

....

{$TYPEDADDRESS ON}
procedure TForm1.PaintBox1Paint(Sender: TObject);
const
  Text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do '
    + 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim '
    + 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
    + 'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
    + 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur '
    + 'sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt '
    + 'mollit anim id est laborum.';
var
  Graphics: TGPGraphics;
  Font: TGPFont;
  Brush: TGPBrush;
  lf: LOGFONT;
begin
  Graphics := TGPGraphics.Create(PaintBox1.Canvas.Handle);
  try
    Graphics.SetTextRenderingHint(TextRenderingHintSystemDefault);

    lf := Default(LOGFONT);
    lf.lfHeight := 20;
    lf.lfCharSet := DEFAULT_CHARSET;
    lf.lfFaceName := 'Times New Roman';

    Font := TGPFont.Create(PaintBox1.Canvas.Handle, @lf);
    try
      Brush := TGPSolidBrush.Create(MakeColor(0, 0, 0));
      try
        Graphics.RotateTransform(-15);
        Graphics.DrawString(
          Text,
          -1,
          Font,
          MakeRect(0.0, 150.0, 450.0, 600.0),
          nil,
          Brush
        );
      finally
        Brush.Free;
      end;
    finally
      Font.Free;
    end;
  finally
    Graphics.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

并输出:

在此输入图像描述

还是很漂亮地看着我的视线.因此,为了获得最佳质量,我建议使用带有GR32_Text的graphics32.