Solved

Wrap text using Image.Picture.Canvas method

Posted on 2006-06-17
3
1,396 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
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, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

21 Experts available now in Live!

Get 1:1 Help Now