tags:

views:

468

answers:

2

hi all,

i am new to delphi programming.

i want to use canvas.textrect to write something on the canvas with 90 degree angle and word wrap capability and i also want the text to be vertically aligned in the rectangle.

any help will be appreciated.

thx in advance.

A: 

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.

Nick D
it didn't work,my code is:tf := MakeVerticalFont( Canvas.Font );Canvas.Font.Assign( tf );Canvas.TextOut(10,10, 'hello world');but it doesn't have any angle.
rahim asgari
@rahim asgari, check out my update. I had to use a temporary TCanvas object on which I assigned the vertical font. If I use the form1.canvas directly the vertical font isn't working, you're right.
Nick D
Thx Nick D, it worked but i cant use it with textrect. i need textrect because i want my text to be wrapped. any suggestion?
rahim asgari
@rahim asgari, what do mean you can't use it with textrect? It works with that function.
Nick D
my code is: TR := Rect(100, 100, 450,350); tmpcanvas.TextRect(TR, TR.Top, TR.Left, 'Hello World');but nothing is displayed on the screen.
rahim asgari
@rahim asgari, the text will be drawn on that corner and upwards. So it will be outside the rect box. Try `tmpcanvas.TextRect(TR, TR.Left, TR.Top+50, 'Hello World');`
Nick D
@Nick D, now i want my string to be wordwrapped in the rectangle.as i know TextRect doesnt support multiline and word wrap.any suggestion?
rahim asgari
@rahim asgari, see my second update.
Nick D
@Nick D, thx for your quick replies.i want my 90 DEGREE ANGLED text to be word wrapped.but i suppose that DrawTextEx doesnt wordwrap angled text.any suggestion?
rahim asgari
@rahim asgari, I don't know if there is a function that wraps vertical text. One possible solution (but not very good) is to use Delphi's `WrapText`. Then manually you can draw each string separated with new line chars with `TextRect`, in a loop. `WrapText` is for fixed-width chars, so the result wont be great.
Nick D
+1  A: 

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.

Gerry
Canvas.Font doesnt have Orientation property.
rahim asgari
What version of Delphi? It does in Delphi 2006 (but not in Delphi 7)
Gerry
If you are using Delphi 7 or earlier, you will need to use the method mentioned by Nick D
Gerry
yah, you are right. i am using delphi 7 which doesnt have that property.
rahim asgari