Rather than Canvas.TextOut, use Canvas.TextRect which supports the TextStyle parameter which can be set up for easy centered painting (otherwise you'd have to measure text width and text height and calculate the text start point).
procedure TForm1.Button1Click(Sender: TObject);
var
ts: TTextStyle;
begin
// With Form1 do // NEVER USE THE FORM INSTANCE IN THE FORM CLASS CODE !!!
begin
canvas.brush.color := clYellow;
canvas.Ellipse(100,100,300,300);
// canvas.MoveTo(100,100); // NOT NEEDED
Canvas.Font.Size:=22;
Canvas.Font.Color:=clRed;
ts := Canvas.TextStyle;
ts.Alignment := taCenter;
ts.Layout := tlCenter;
Canvas.TextRect(Rect(100, 100, 300, 300), 0, 0, 'Hello', ts); // Rect within text is centered, dummy x, dummy y, text, text style
end;
end;
Be warned: You are painting outside the drawing cycle. When after your button click the user drags another window over your form, your form will be erased! and in some operating systems this code will even raise a runtime error.
Always use the OnPaint event for custom drawing:
var
ButtonClicked: Boolean = false;
procedure TForm1.Button1Click(Sender: TObject);
begin
ButtonClicked := true;
Invalidate;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
ts: TTextStyle;
begin
if not ButtonClicked then
exit;
Canvas.brush.color := clYellow; // is the fillcolor for the circle
Ccanvas.Ellipse(100,100,300,300);
Canvas.Font.Size:=22;
Canvas.Font.Color:=clRed;
ts := Canvas.TextStyle;
ts.Alignment := taCenter;
ts.Layout := tlCenter;
Canvas.TextRect(Rect(100, 100, 300, 300), 0, 0, 'Hello', ts);
end;