开发者

delphi textrect with angle and wordwrap and vertically aligned

开发者 https://www.devze.com 2022-12-10 05:09 出处:网络
I want to use Canvas.TextRect to write something on the canvas with 90 de开发者_JAVA技巧gree angle and word wrap capability. I also want the text to be vertically aligned in the rectangle. How do I do

I want to use Canvas.TextRect to write something on the canvas with 90 de开发者_JAVA技巧gree angle and word wrap capability. I also want the text to be vertically aligned in the rectangle. How do I do that?


Here is a sample code to create a vertical font:

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
   ...

Update: Try to render vertical text on a form:

    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;

Update 2: I think it's better to use the DrawTextEx Function which supports text alignment and word wrapping.

My Delphi version doesn't include it in the documentation but you can see the various flags in the above link. Below is a sample code to see how to use it. I have disabled vertical font because it seems that word wrapping doesn't work well with vertical fonts.

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;

Note that when you want to align text in the rectangle you should use the DT_SINGLELINE flag.
For example this combination: DT_CENTER or DT_VCENTER or DT_SINGLELINE, will center the text in the middle of the rectangle.


There is an Orientation property of TFont in Delphi 2006 onwards. Unfortunately the help wasn't updated to include it (like so much of D2006 help).

Delphi 2010 help is here

It is in tenths of a degree, so set to to 90 degress, use 900.

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

You also then need to adjust the rectangle co-ordinates as required.

I've used ths in the past, but can't remember the details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号