Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Wrap text using Image.Picture.Canvas method

Posted on 2006-06-17
3
Medium Priority
?
1,480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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