Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



How to draw text

The following example show how to draw text and text in a coloured box.


Text

The basic way to draw text is to call CGraphicsContext::DrawText() with a point specifying the bottom left position, as shown below:

Before calling, you must use the graphics context to:

  1. Set the font to use

  2. Set the pen colour for the text

// In this example, we use one of the standard font styles
CFont* fontUsed = iEikonEnv->TitleFont();
gc.UseFont(fontUsed);

gc.SetPenColor(KRgbBlack);

TPoint pos(50,50);
_LIT(KExampleText,"blacktext");
gc.DrawText(KExampleText,pos);

[Top]


Text in a box

To draw text in a box, you must specify:

The following figure show how these fit together:

In addition to the font and pen colour, you can also set the brush for filling the box.

...
// Draw some text left justified in a box,
// Offset so text is just inside top of box

TRect box(20,20,250,100);
TInt baseline = box.Height() /2 + fontUsed->AscentInPixels()/2;

TInt margin=10; // left margin is ten pixels

gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.SetBrushColor(KRgbDarkGray);
gc.SetPenColor(KRgbWhite);

_LIT(KExampleText,"Whitetextleftjustified");
gc.DrawText(KExampleText,
 box,
 baseline,
 CGraphicsContext::ELeft,
 margin);

...