Reputation: 37
I want to draw a rectangle on a canvas and I want to fill it with text. I've tried with the below code, but it only succeeded with right alignment.
Can anyone help me? How can I draw text on a canvas with left alignment?
{Header Table}
SetStyleHuruf(FCanvas, fsBold, 12, clWhite, 'Maiandra GD');
Brush.Color := color;
Rectangle(cx+50, cy-50, cx+370, cy - 30);
TextOut(round(cx + 215), cy-50, Name);`
Upvotes: 1
Views: 2779
Reputation: 21033
Look at the definition of TextOut
:
procedure TextOut(X, Y: Integer; const Text: string); override;
The description in help says:
Writes a string on the canvas, starting at the point (X,Y), ...
So, modify your code to draw the text at an X coordinate that is closer to the left border of the rectangle instead of the right right. For example:
TextOut(round(cx + 215), cy-50, Name);
Outputs the text 5 pixels from the left border of the rectangle
TextOut(round(cx + 55), cy-50, Name);
BTW, assuming cx
is an integer, you dont need to use Round()
Upvotes: 2
Reputation: 203
Instead of textout I would use DrawText function which is explained on the Microsoft site here
The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth). To specify additional formatting options, use the DrawTextEx function.
Here you find a great example how to use it
Upvotes: 1