Drawing text on screen

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

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

#include <gdi.h>

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

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

  1. // Get smallest possible arial font
  2. _LIT(KMyFontName,"Arial");
  3.  
  4. CFont* myFont;
  5.  
  6. TFontSpec myFontSpec(KMyFontName, TInt(1));
  7.  
  8. CGraphicsDevice* screenDevice = iCoeEnv->ScreenDevice();
  9.  
  10. screenDevice->GetNearestFontInTwips(myFont,myFontSpec);
  11. // Use new font
  12. gc.UseFont(myFont);
  13.  
  14. // Get the width and height of screen
  15. TInt width = aRect.Width();
  16. TInt height = aRect.Height();
  17.  
  18. // Draw a formated text
  19. TBuf<32> text;
  20. _LIT(KMyText, "width=%d, height=%d");
  21. text.Format(KMyText, width, height);
  22. gc.DrawText(text, TPoint(10, height) );
  23.  
  24. // Discard and release the font
  25. gc.DiscardFont();
  26. screenDevice->ReleaseFont(myFont);

Related posts

0 Responses to “Drawing text on screen”


  • No Comments

Leave a Reply




This work by wolfg is licensed under a Attribution-Noncommercial-Share Alike 2.5 China Mainland.