Board index » delphi » Aligning text with Canvas

Aligning text with Canvas

Hi everybody.
Is there anyone who could help me on these problem:
I'm now programming in Delphi and I am used to have the option in Pascal
do write text up and down on graphics with the command: SETTEXTSTYLE.
It is also possible to justify the text with the command:
SETTEXTJUSTIFY.
Now the problem: I draw my graphics with Canvas on a image and I have
failed to find simmular commands in Delphi.
I would be very pleased if someone could send my a note with solution.
Tanks.
Rafn Yngvi.
r...@post3.tele.dk

 

Re:Aligning text with Canvas


On Wed, 11 Sep 1996 13:24:03 +0200, Rafn Yngvi Rafnsson
<r...@post3.tele.dk> you smacked the keyboard repeatedly to write:

Quote
>Hi everybody.
>Is there anyone who could help me on these problem:
>I'm now programming in Delphi and I am used to have the option in Pascal
>do write text up and down on graphics with the command: SETTEXTSTYLE.
>It is also possible to justify the text with the command:
>SETTEXTJUSTIFY.
>Now the problem: I draw my graphics with Canvas on a image and I have
>failed to find simmular commands in Delphi.
>I would be very pleased if someone could send my a note with solution.
>Tanks.
>Rafn Yngvi.
>r...@post3.tele.dk

Canvas.Font.Name := 'Arial';
Canvas.Font.Sytle := [fsBold];
Canvas.TextOut(X,Y,'My Text Goes Here');

Brien King
bk...@primenet.com

Re:Aligning text with Canvas


Quote
Rafn Yngvi Rafnsson wrote:

> It is also possible to justify the text with the command:
> SETTEXTJUSTIFY.

SetTextAlign, SetTextJustification.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Re:Aligning text with Canvas


In article <3236A153.4...@post3.tele.dk>, r...@post3.tele.dk says...

Quote
>Hi everybody.
>Is there anyone who could help me on these problem:
>I'm now programming in Delphi and I am used to have the option in Pascal
>do write text up and down on graphics with the command: SETTEXTSTYLE.
>It is also possible to justify the text with the command:
>SETTEXTJUSTIFY.
>Now the problem: I draw my graphics with Canvas on a image and I have
>failed to find simmular commands in Delphi.
>I would be very pleased if someone could send my a note with solution.
>Tanks.
>Rafn Yngvi.
>r...@post3.tele.dk

Hello there,
I've found A way - just not sure it's the best one.  You can use the handle of
the canvas to access the basic Windows functions.  In this case, you can use
ExtTextOut, passing it a rectangle in which to justify.  First you need to
call SetTextAlign to set the alignment option.  I've had some problems with
old printers that would not align correctly, so I actually wound up using
GetTextExtent to get the length of the string and changing the y parameter
to start at the right edge minus the width of the text minus 1.  
Anyone have a better way ?
Hope this helps,
Jochen

Re:Aligning text with Canvas


In article <3236A153.4...@post3.tele.dk>,
   Rafn Yngvi Rafnsson <r...@post3.tele.dk> wrote:

Quote
>Hi everybody.
>Is there anyone who could help me on these problem:
>I'm now programming in Delphi and I am used to have the option in Pascal
>do write text up and down on graphics with the command: SETTEXTSTYLE.
>It is also possible to justify the text with the command:
>SETTEXTJUSTIFY.
>Now the problem: I draw my graphics with Canvas on a image and I have
>failed to find simmular commands in Delphi.
>I would be very pleased if someone could send my a note with solution.
>Tanks.
>Rafn Yngvi.
>r...@post3.tele.dk

It's not that difficult (if I understand you correctly).

Have a look at the extracts below:

{-------------------------------------------------------------------------}
const
    LeftTop      = 0;
    LeftCentre   = 1;
    LeftBottom   = 2;
    CentreTop    = 3;
    CentreCentre = 4;
    CentreBottom = 5;
    RightTop     = 6;
    RightCentre  = 7;
    RightBottom  = 8;
{
  .....

Quote
}

{-------------------------------------------------------------------------}
procedure output_text (canvas : Tcanvas; x,y, just : integer; s : string);
{-------------------------------------------------------------------------}
begin
    canvas.pen.color := colour;

    if printing then
        begin
          x := round(x * dpmm);
          y := round(y * dpmm);
        end;

    case just of
        LeftTop       :   { do nothing };
        LeftCentre    :   y := y - canvas.TextHeight(s) div 2;
        LeftBottom    :   y := y - canvas.TextHeight(s);

        CentreTop     :   x := x - canvas.TextWidth(s) div 2;
        CentreCentre  : begin
                          x := x - canvas.TextWidth(s) div 2;
                          y := y - canvas.TextHeight(s) div 2;
                        end;
        CentreBottom  : begin
                          x := x - canvas.TextWidth(s) div 2;
                          y := y - canvas.TextHeight(s);
                        end;

        RightTop      :   x := x - canvas.TextWidth(s);
        RightCentre   : begin
                          x := x - canvas.TextWidth(s);
                          y := y - canvas.TextHeight(s) div 2;
                        end;
        RightBottom   : begin
                          x := x - canvas.TextWidth(s);
                          y := y - canvas.TextHeight(s);
                        end;
      end;

    canvas.TextOut(x,y, s);
end;
{-------------------------------------------------------------------------}

cheers
S.

 _--_|\     Stephen Peter                             School of Architecture
/      \    S.Pe...@unsw.EDU.AU                University of New South Wales
\_.--._/<-------------------------------------------------------------------
      v             http://www.arch.unsw.edu.au/staff/arch/peter/stephen.htm

Re:Aligning text with Canvas


Rafn Yngvi Rafnsson <r...@post3.tele.dk> wrote:

Quote
>Hi everybody.
>Is there anyone who could help me on these problem:
>I'm now programming in Delphi and I am used to have the option in Pascal
>do write text up and down on graphics with the command: SETTEXTSTYLE.
>It is also possible to justify the text with the command:
>SETTEXTJUSTIFY.
>Now the problem: I draw my graphics with Canvas on a image and I have
>failed to find simmular commands in Delphi.
>I would be very pleased if someone could send my a note with solution.
>Tanks.
>Rafn Yngvi.
>r...@post3.tele.dk

See this code:
------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
   S:String;
begin
   S:='CIAO!';
    with Image1.Canvas do
       TextOut(ClipRect.Right-TextWidth(S)-1,ClipRect.Top+2,S);
end;
---------------------
TextOut and TextWidth are fields of TCanvas.

Ciao.

Giuseppe Mosca Via Nilo,20 65016 Montesilvano (PE) Italy
gmo...@rgn.it

Giuseppe Mosca Via Nilo, 20 65016 Montesilvano (PE)
gmo...@rgn.it

Other Threads