Solved

Wrap text using Image.Picture.Canvas method

Posted on 2006-06-17
3
1,408 Views
Last Modified: 2011-10-03
Hi How can i wrap a paragraph using TextOut Command? or if you have any other good suggestion i'll really appriciate to know.  Here's my procedure below. The line   image.Picture.Bitmap.Canvas.TextOut(112  , 200 , _oTable.fieldbyname('ALLERGIE').asstring); contains a text field which i want to display in a wrap manner.

Thanks in advance

procedure TfDiagnosePatient.DrawPageLabelsPatientRecord2(image:TImage;_oTable:TTable);
var
 x, y  :Integer;
begin
 Try
  x:=0;
  y:=0;

  image.Picture.Bitmap.Canvas.Font.nAME:= 'Arial';
  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold];
  image.Picture.Bitmap.Canvas.Font.Size := 14;
  image.Picture.Bitmap.Canvas.TextOut(16  , 152 , 'More Information');

  image.Picture.Bitmap.Canvas.Font.Size := 10;
  image.Picture.Bitmap.Canvas.Font.Style:= [];
  image.Picture.Bitmap.Canvas.TextOut(32  , 184 , 'Blood Type');
  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold, fsUnderLine];
  image.Picture.Bitmap.Canvas.TextOut(112  , 184 , _oTable.fieldbyname('BLOODTYP').asstring);

  image.Picture.Bitmap.Canvas.Font.nAME:= 'Arial';
  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold];
  image.Picture.Bitmap.Canvas.Font.Size := 12;
  image.Picture.Bitmap.Canvas.TextOut(16  , 224 , 'Allergies');
  image.Picture.Bitmap.Canvas.Font.Style:= [];
  image.Picture.Bitmap.Canvas.Font.Size := 10;
  image.Picture.Bitmap.Canvas.TextOut(112  , 200 , _oTable.fieldbyname('ALLERGIE').asstring);


  image.Picture.Bitmap.Canvas.Font.Style:= [fsBold,fsItalic];
  image.Picture.Bitmap.Canvas.Font.nAME:= 'MS Sans Serif';
  image.Picture.Bitmap.Canvas.Font.Size := 8;
  image.Picture.Bitmap.Canvas.TextOut(16  , 712 , 'Printed on '+formatdatetime('MMMM DD, YYYY  HH:MM:SS am/pm',NOW));
end;
0
Comment
Question by:ryan_sabarre
  • 2
3 Comments
 

Author Comment

by:ryan_sabarre
ID: 16927081
Thanks in advance
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 50 total points
ID: 16931527
Youcan use the "DrawText" method with DT_WORDBREAK
If you don't wish to, or it's too complicated, then here's a simple demo I wrote which does just wordwrapping without using the DrawText method

procedure WordWrapTextOut(canvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
begin
  image1.picture.bitmap.Width := image1.width;
  image1.picture.Bitmap.Height := image1.height;
  image1.picture.Bitmap.Canvas.Brush.color := clBlack;
  image1.picture.Bitmap.Canvas.Fillrect(RECT(0,0,image1.picture.bitmap.Width,image1.picture.bitmap.Height));
  image1.picture.Bitmap.Canvas.Font.Color := clWhite;
end;

{ This will start drawing text from x, y. If it goes past maxwidth, it will drop to next line.
At the end of this routine, x and y will be at the position just past the end of the text}
procedure WordWrapTextOut(canvas: TCanvas; var x, y: integer; S: string; maxwidth, lineheight: integer);
  const
    delims = ' ,.:;?!';
  var
    textw, texth, tempw: integer;
    firststring, secondstring: string;
    i: integer;
  begin
    firststring := s;
    while firststring <> '' do
    begin
      secondstring := '';
      textw := canvas.TextWidth(firststring);
      while (
              (firststring <> '') and  (textw > maxwidth)
            ) do
      begin
        secondstring := firststring[length(firststring)] + secondstring;
        delete(firststring, length(firststring), 1);
        tempw := canvas.TextWidth(firststring);
        if ( (tempw <= maxwidth) and  (pos(firststring[length(firststring)], delims) > 0) ) then
          textw := tempw;
      end;
// searching for delimiter failed. fall back to wrap at last char
      if firststring = '' then
      begin
        firststring := secondstring;
        secondstring := '';
        textw := canvas.TextWidth(firststring);
        while ( (textw > maxwidth) and (length(firststring) > 0) ) do
        begin
          secondstring := firststring[length(firststring)] + secondstring;
          delete(firststring, length(firststring), 1);
          textw := canvas.TextWidth(firststring);
        end;
      end;
// actually draw some text
      canvas.TextOut(x, y, firststring);
      firststring := secondstring;
      if firststring <> '' then
      begin
        y := y + lineheight;
      end;
    end;
// get the position right in case the user wishes to add some more text to this line
    x := x + textw;
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x, y: integer;
  lineheight: integer;
begin
  x := 0;
  y := 0;
  lineheight := image1.picture.Bitmap.Canvas.TextHeight('W') + 1; // 1 space between lines
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, 'short line', image1.picture.bitmap.width, lineheight);
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, ' plus this', image1.picture.bitmap.width, lineheight);
// next line
  x := 0;
  y := y + lineheight;
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, 'this is a bigger line which should word wrap', image1.picture.bitmap.width, lineheight);
// next line
  x := 0;
  y := y + lineheight;
  WordWrapTextOut(image1.picture.Bitmap.Canvas, x, y, memo1.lines.text, 200, lineheight);
end;

0
 

Author Comment

by:ryan_sabarre
ID: 16977618
Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
indy tidtcpclient threading issue 8 63
Convert Jpg /PNG To GIF 5 120
Virtuailstring tree compare node issue 14 105
Base1 Encode/Decode 3 68
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now