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).
Share
Use the (almost) omnipotent
DrawTextfunction using an initial rectangle, and the flagsDT_WORDBREAK(meaning that the string should be word-wrapped) andDT_CALCRECT:Due to the flag
DT_CALCRECT, the firstDrawTextwill not draw anything, but only alter the height ofrso that it can contain the entire stringS(or reduce the width ofrifShappens to fit on a single line; in addition, ifScontains a word that does not fit on a single line, the width ofrwill be increased). Then you can do whatever you wish withr, and then you can draw the string for real.Try this, for example: