![]() |
GDI Tutorials: Drawing Text |
To write text on a canvas, you can call the TCanvas::TextOut() method. Its syntax is: void __fastcall TextOut(int X, int Y, const AnsiString Text); The TextOut() method is used to create an display a piece of text on the screen. The X and Y arguments are the point coordinates of the top-left corner of the string being displayed. The Text argument is the text that needs to be displayed. Here is an example: |
//--------------------------------------------------------------------------- void __fastcall TForm1::FormPaint(TObject *Sender) { Canvas->TextOut(10, 10, "Walter Bells"); } //---------------------------------------------------------------------------
To create text that must only fit inside or a rectangle, you can call the TCanvas::TextRect() method Error! Bookmark not defined. Its syntax is: void __fastcall TextRect(const Types::TRect &Rect, int X, int Y, const AnsiString Text); The TextRect() method draws text in a rectangle. A portion of the text that is larger than the allocated rectangle would be hidden. The Rect argument is the rectangle that will contain the text. The X and Y argument are the coordinates of the text, in screen coordinates. This means that, to show the beginning of the text, the value of X must be greater than or equal to the Left member variable of the Rect argument. If you want to show the top section of the text, the value of Y must be greater than or equal to the Top member variable of the Rect argument. The Text argument is the string that needs to be displayed. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::FormPaint(TObject *Sender) { Canvas->TextRect(Rect(40, 20, 120, 60), 40, 20, "Walter Bells"); } //---------------------------------------------------------------------------
Many drawing functions require you to know the dimensions of the string that is being drawn or the string that needs to be drawn. To support this, the VCL provides various functions. To know the width occupied by a string, you can call the TCanvas::TextWidth() method. Its syntax is: int __fastcall TextWidth(const AnsiString Text); The Text argument is the string that is displaying or needs to be shown. After this method has executed, it returns the width of the string. On the other hand, if you want to find out the height of text that is drawn or needs to be drawn, you can call the TCanvas::TextHeight() method whose syntax is: int __fastcall TextHeight(const AnsiString Text); Like TextWidth(), the TextHeight() method takes as argument the string that is displaying or needs to be displayed. TextHeight() returns the height of the string. If you need to find both the width and the height occupied by a string, you can use a single TCanvas method called TextExtent. Its syntax is: TSize __fastcall TextExtent(const AnsiString Text); Like the previous two methods, TextExtent() takes as argument the string that needs to be considered. After this method has executed, it return both the width and the height of the Text string stored in a TSize variable.
The Win32 library provides a function that can be used to draw text that is proportionately centered with regards to either the width or the height of the canvas on which it is positioned. The function used is DrawText() and its syntax is: int DrawText(HDC hDC, LPCTSTR lpString, int nCount, LPRECT lpRect, UINT uFormat); The lpString argument is the string that needs to be drawn. The nCount is the number of characters that compose the string. The string will be positioned in the lpRect rectangle. The uFormat argument specifies how the text will be formatted. |
Home | Copyright © 2004-2016, FunctionX, Inc. | |