How to draw text with different font formatting on canvas in delphi right away?

I use DrawText for all my "text in rectangle" queries, but I don’t see a way to draw a string, for example, with any other word in bold or different colors, or, even worse, different backgrounds for the mentioned randomly selected words. There is probably not a single procedure that can do this, as far as I can tell, I could be wrong, which is the main question of this issue. Can someone point out procedures that may be of interest to someone who is trying to achieve such effects? Also, if I'm right and there is no easy way to do this, what would be the recommended way? Drawing each word separately and then trying to glue it all together seems like a nightmare when you start thinking about the problems that may arise because of this on my head: the correct alignment of the text on one horizontal line when you have different fonts or dimensions ...

I have delphi xe3; if someone can improve the wording of my question and / or text, please do so.

+9
delphi
source share
2 answers

You get some help from VCL, as the TCanvas.TextOut method increases the x coordinate of the handle position by the width of the output line:

 procedure TForm1.FormPaint(Sender: TObject); begin Canvas.MoveTo(20, 100); Canvas.Font.Name := 'Segoe UI'; Canvas.Font.Color := clMaroon; Canvas.Font.Style := []; Canvas.Font.Height := 64; Canvas.TextOut(Canvas.PenPos.X, Canvas.PenPos.Y, 'This '); Canvas.Font.Color := clNavy; Canvas.Font.Style := [fsBold]; Canvas.Font.Height := 64; Canvas.TextOut(Canvas.PenPos.X, Canvas.PenPos.Y, 'is '); Canvas.Font.Name := 'Bookman Old Style'; Canvas.Font.Color := clBlack; Canvas.Font.Style := [fsItalic]; Canvas.Font.Height := 64; Canvas.TextOut(Canvas.PenPos.X, Canvas.PenPos.Y, 'a '); Canvas.Font.Name := 'Courier New'; Canvas.Font.Color := clSilver; Canvas.Font.Style := []; Canvas.Font.Height := 64; Canvas.TextOut(Canvas.PenPos.X, Canvas.PenPos.Y, 'test!'); end; 

Screen shot

In any case, if you need more complex text output procedures, why not take a look at DirectWrite ?

+19
source share

Have you considered using Richedit with its rich formatting capabilities? If you need to draw text on a canvas, not in a window, then the EM_FORMATRANGE message allows you to copy a graphical representation of the formatted text.

+2
source share

All Articles