Drawing text on screen

来自SDK中文档Developer Guides >> Programming Games in C++

要加入gdi.h这个头文件,否则编译时会报错unresolved external symbol TFontSpec


#include

同时还要修改mmp文件,加入LIBRARY gdi.lib。我用的IDE是Carbide c++ Express,新建的Project没有生成mmp文件,需要修改Project的build配置,把gdi.lib加到Linker的Libraries列表中。

格式化文本用TBuf::Format()

// Get smallest possible arial font
_LIT(KMyFontName,"Arial");

CFont* myFont;

TFontSpec myFontSpec(KMyFontName, TInt(1));

CGraphicsDevice* screenDevice = iCoeEnv->ScreenDevice();

screenDevice->GetNearestFontInTwips(myFont,myFontSpec);
// Use new font
gc.UseFont(myFont);

// Get the width and height of screen
TInt width = aRect.Width();
TInt height = aRect.Height();

// Draw a formated text
TBuf<32> text;
_LIT(KMyText, "width=%d, height=%d");
text.Format(KMyText, width, height);
gc.DrawText(text, TPoint(10, height) );

// Discard and release the font
gc.DiscardFont();
screenDevice->ReleaseFont(myFont);