In Delphi, you can draw text on the screen using the Canvas
property of a visual control, such as a form or a custom-painted control. The Canvas
property provides methods for drawing various shapes, including text.
Here’s a simple example of how to draw text on a form in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
unit MainForm; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs; type TForm1 = class(TForm) procedure FormPaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.FormPaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF); var TextToDraw: string; begin // Set the text to be drawn TextToDraw := ‘Hello, Delphi!’; // Set font properties (optional) Canvas.Font.Size := 20; Canvas.Font.Family := ‘Arial’; Canvas.Font.Style := [TFontStyle.fsBold]; // Set text color (optional) Canvas.Fill.Color := TAlphaColors.Blue; // Draw the text on the form Canvas.FillText( RectF(50, 50, ClientWidth – 50, ClientHeight – 50), TextToDraw, False, 1, [], TTextAlign.Center, TTextAlign.Center ); end; end. |
In this example:
- The
FormPaint
event is triggered whenever the form needs to be repainted. - Inside the
FormPaint
event, you set the text you want to draw and configure font properties and colors as needed. - The
Canvas.FillText
method is used to draw the text on the form. You provide the rectangle where the text should be drawn, the text itself, and additional formatting options.
Adjust the coordinates, font properties, and colors according to your requirements. You can also use this approach in other types of visual controls, such as custom-painted controls or panels.
Leave a Reply