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);

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.
For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>