代码之家  ›  专栏  ›  技术社区  ›  rahim asgari

delphi textrect,带角度和文字环绕,垂直对齐

  •  1
  • rahim asgari  · 技术社区  · 16 年前

    我想用 Canvas.TextRect

    2 回复  |  直到 5 年前
        1
  •  4
  •   Gerry Coll    16 年前

    Delphi 2010 help is here

    它是十分之一度,所以设置为90度,使用900。

    Canvas.Font.Orientation := 900;
    Canvas.TextRect(....);
    

    然后还需要根据需要调整矩形坐标。

        2
  •  4
  •   Nick Dandoulakis    16 年前

    以下是创建垂直字体的示例代码:

    function MakeVerticalFont(f: TFont): TFont;
    var
        lf : TLogFont;
        tf : TFont;
    begin
         tf := TFont.Create;
    
         tf.Assign( f );
         GetObject(tf.Handle, sizeof(lf), @lf);
         lf.lfEscapement := 900; // <--
         lf.lfOrientation := 900; // <-- here we specify a rotation angle
         tf.Handle := CreateFontIndirect(lf);
    
         result := tf;
    end;
    [...]
    
    var tf: TFont;
    Begin
       ...
       tf := MakeVerticalFont( mycanvas.Font );
       mycanvas.Font.Assign( tf ); // <--- assign the `same` font rotated by 90 degrees
       ...
    

    尝试在窗体上呈现垂直文本:

        var tf : TFont;
            tmpcanvas : TCanvas;
        begin
            tmpcanvas := form1.Canvas;
            tmpcanvas.Font.Name := 'Arial';
            tmpcanvas.Font.Height := 12;
    
            tf := MakeVerticalFont(tmpcanvas.font);
            tmpcanvas.Font.Assign(tf);
    
            tmpcanvas.TextOut(50, 50, 'Am I vertical?');
            tf.free;
    

    更新2: DrawTextEx Function 它支持文本对齐和换行。

    我的Delphi版本没有包含在文档中,但是您可以在上面的链接中看到各种标志。 下面是一个示例代码,以了解如何使用它。我禁用了垂直字体,因为似乎文字换行不适合垂直字体。

    procedure TForm1.Button1Click(Sender: TObject);
    var tf : TFont;
        tmpcanvas : TCanvas;
        rc: TRect;
        s : string;
    begin
        tmpcanvas := form1.Canvas;
        tmpcanvas.Font.Name := 'Arial';
        tmpcanvas.Font.Height := 14;
    
        tf := MakeVerticalFont(tmpcanvas.font);
        //tmpcanvas.Font.Assign(tf); <--- `disabled`
    
        s := 'Hello world! I''m a long string';
        rc := RECT(10, 10, 50, 200);
        windows.DrawTextEx(
            tmpcanvas.Handle,
            PChar(s),
            length(s),
            rc,
            DT_LEFT or DT_WORDBREAK,
            nil);
    
        tf.Free;
    end;
    

    请注意,如果要对齐矩形中的文本,应使用 DT_SINGLELINE
    DT_CENTER or DT_VCENTER or DT_SINGLELINE 将将文本集中在矩形的中间。