Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:44:25+00:00 2026-05-26T05:44:25+00:00

I’m using Delphi 7. I’m more than familiar with using a canvas and drawing

  • 0

I’m using Delphi 7. I’m more than familiar with using a canvas and drawing text to a canvas, and also using TCanvas.TextHeight etc. The problem arises when I want to implement Word Wrap. Not only do I need the best way to draw text to a canvas and have it automatically wrap to a given width constraint, but I also need to know how high (or how many lines) it will be after it’s wrapped. I need to prepare another image before I draw the text, an image which needs to be just big enough to place the wrapped text. This is an attempt to replicate how an iPhone displays SMS messages, with a baloon on either side of the screen in a variable height scrolling box (TScrollingWinControl is my base).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:44:26+00:00Added an answer on May 26, 2026 at 5:44 am

    Use the (almost) omnipotent DrawText function using an initial rectangle, and the flags DT_WORDBREAK (meaning that the string should be word-wrapped) and DT_CALCRECT:

    procedure TForm1.FormPaint(Sender: TObject);
    const
      S = 'This is a sample text, I think, is it not?';
    var
      r: TRect;
    begin
      r := Rect(10, 10, 60, 60);
      DrawText(Canvas.Handle,
        PChar(S),
        Length(S),
        r,
        DT_LEFT or DT_WORDBREAK or DT_CALCRECT);
    
      DrawText(Canvas.Handle,
        PChar(S),
        Length(S),
        r,
        DT_LEFT or DT_WORDBREAK);
    end;
    

    Due to the flag DT_CALCRECT, the first DrawText will not draw anything, but only alter the height of r so that it can contain the entire string S (or reduce the width of r if S happens to fit on a single line; in addition, if S contains a word that does not fit on a single line, the width of r will be increased). Then you can do whatever you wish with r, and then you can draw the string for real.

    Try this, for example:

    procedure TForm1.FormPaint(Sender: TObject);
    const
      S: array[0..3] of string = ('Hi! How are you?',
        'I am fine, thanks. How are you? How are your kids?',
        'Fine!',
        'Glad to hear that!');
      Colors: array[boolean] of TColor = (clMoneyGreen, clSkyBlue);
      Aligns: array[boolean] of integer = (DT_RIGHT, DT_LEFT);
    var
      i, y, MaxWidth, RectWidth: integer;
      r, r2: TRect;
    begin
    
      y := 10;
      MaxWidth := ClientWidth div 2;
    
      for i := low(S) to high(S) do
      begin
    
        Canvas.Brush.Color := Colors[Odd(i)];
    
        r := Rect(10, y, MaxWidth, 16);
        DrawText(Canvas.Handle,
          PChar(S[i]),
          Length(S[i]),
          r,
          Aligns[Odd(i)] or DT_WORDBREAK or DT_CALCRECT);
    
        if not Odd(i) then
        begin
          RectWidth := r.Right - r.Left;
          r.Right := ClientWidth - 10;
          r.Left := r.Right - RectWidth;
        end;
    
        r2 := Rect(r.Left - 4, r.Top - 4, r.Right + 4, r.Bottom + 4);
        Canvas.RoundRect(r2, 5, 5);
    
        DrawText(Canvas.Handle,
          PChar(S[i]),
          Length(S[i]),
          r,
          Aligns[Odd(i)] or DT_WORDBREAK);
    
        y := r.Bottom + 10;
    
      end;
    
    end;
    
    procedure TForm1.FormResize(Sender: TObject);
    begin
      Invalidate;
    end;
    

    Screenshot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.