Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Wrap text using Image.Picture.Canvas method

Posted on 2006-06-17
3
Medium Priority
?
1,507 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 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

879 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