【转载】MiniUtilityFramework(九):CText和TEXT

转载自:http://blog.csdn.net/norains/article/details/3957123

 

CText和微软的Static Control类似,都是用以显示文字信息。最大的不同,可能就在于CText能更方便设置字体的信息,比如:大小,颜色等等。
  
CText的public接口比较多,不过大多数都只是用来设置文字显示的形式:

class CText    
{  
public:  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set all the property for the control  
    //  
    //---------------------------------------------------------------------  
    BOOL SetProperty(const Text::Property &Property);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Get all the property of the control  
    //  
    //---------------------------------------------------------------------  
    Text::Property GetProperty();  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the strikeOut  
    //  
    //---------------------------------------------------------------------  
    void SetStrikeOut(BOOL bStrikeOut);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the underline  
    //  
    //---------------------------------------------------------------------  
    void SetUnderline(BOOL bUnderline);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the italic  
    //  
    //---------------------------------------------------------------------  
    void SetItalic(BOOL bItalic);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Specifies the weight of the font in the range 0 through 1000. For example,   
    //400 is normal and 700 is bold. If this value is zero, a default weight is used.   
    //  
    //---------------------------------------------------------------------  
    BOOL SetWeight(int iWeight);  
  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Get the position as rect  
    //  
    //---------------------------------------------------------------------  
    RECT GetPosition();  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the point size of text  
    //  
    //---------------------------------------------------------------------  
    void SetPointSize(int iPointSize);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set Format.  
    //  
    //Parameters:  
    //  The value you should see the uFormat of DrawText()  
    //--------------------------------------------------------------------  
    void SetFormat(UINT uFormat);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the background color  
    //  
    //--------------------------------------------------------------------  
    void SetBkColor(COLORREF crColor);  
  
      
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the text color  
    //  
    //--------------------------------------------------------------------  
    void SetTextColor(COLORREF crColor);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the background mode.  
    //  
    //Parameters:  
    //  iMode: [in] The value is just like as follow:  
    //      OPAQUE      -- Background is filled with the current background color before the text,   
    //                      hatched brush, or pen is drawn.   
    //      TRANSPARENT -- Background remains untouched.   
  
    //--------------------------------------------------------------------  
    BOOL SetBkMode(int iMode);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Display the text stored.  
    //  
    //--------------------------------------------------------------------  
    void Draw(HDC hdc);  
      
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the text. If you want to display the text ,you should call the Display()  
    //  
    //--------------------------------------------------------------------  
    BOOL SetText(const TSTRING &strText);  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set the control position  
    //  
    //--------------------------------------------------------------------  
    void SetPosition(const RECT &rcWndPos);  
  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Get the text.  
    //  
    //--------------------------------------------------------------------  
    TSTRING GetText();  
  
    //--------------------------------------------------------------------  
    //Description:  
    //  Set font face name  
    //  
    //--------------------------------------------------------------------  
    BOOL SetFaceName(const TSTRING &strFaceName);  
  
public:  
    CText();  
    virtual ~CText();  
  
private:  
    Text::Property m_Property;  

 配置文件可选的字段也不少,但并不复杂:

  TYPE:类型,取值必须为TEXT,标明当前控件为TEXT
  
  RECT_POS:该控件在窗口的位置。
  
  POINT_SIZE:字体的大小。
  
  WEIGHT:粗细,取值为0~900。
  
  FORMAT:格式。取值和DrawText的uFormat参数的一致,有如下类型(取自DrawText的说明文档):
    
    DT_BOTTOM         Justifies the text to the bottom of the rectangle. You must combine this value with DT_SINGLELINE. 
    
    DT_CALCRECT       Determines the width and height of the rectangle. If the rectangle includes multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If the rectangle includes only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text. Before calling DrawText, an application must set the right and bottom members of the RECT structure pointed to by lpRect. These members are updated with the call to DrawText.   
    
    DT_CENTER         Centers text horizontally in the rectangle. 
    
    DT_END_ELLIPSIS   Truncates a text string that is wider than the display rectangle and adds an ellipsis to indicate the truncation. 
    
    DT_EXPANDTABS     Expands tab characters. The default number of characters per tab is eight.  
    
    DT_INTERNAL       Uses the system font to calculate text metrics.  
    
    DT_LEFT           Aligns text to the left. 
    
    DT_NOCLIP         Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used. 
    
    DT_NOPREFIX       Turns off processing of prefix characters. Normally, DrawText interprets the mnemonic-prefix character & as a directive to underscore the character that follows, and the mnemonic-prefix characters && as a directive to print a single &. By specifying DT_NOPREFIX, this processing is turned off. 
    
    DT_RIGHT          Aligns text to the right. 
    
    DT_RTLREADING     Layout in right-to-left reading order for bi-directional text when the font selected into the hdc is a Hebrew or Arabic font. The default reading order for all text is left-to-right. 
    
    DT_SINGLELINE     Displays text on a single line only. Carriage returns and linefeeds do not break the line. 
    
    DT_TABSTOP        Sets tab stops. Bits 8–15, which form the high-order byte of the low-order word, of the uFormat parameter specify the number of characters for each tab. The default number of characters per tab is eight. You cannot use the DT_CALCRECT, DT_EXTERNALLEADING, DT_INTERNAL, DT_NOCLIP, and DT_NOPREFIX values with the DT_TABSTOP value. 
    
    DT_TOP            Top-justifies text. You must combine this value with DT_SINGLELINE. 
    
    DT_VCENTER        Centers text vertically. You must combine this value with DT_SINGLELINE. 
    
    DT_WORD_ELLIPSIS  Truncates any word that does not fit in the display rectangle and adds an ellipsis. 
    
    DT_WORDBREAK      Breaks words. DrawText automatically breaks lines between words if a word would extend past the edge of the rectangle specified by the lpRect parameter. A carriage return-linefeed sequence also breaks the line. 
  
  COLOR:文本的颜色
  
  TRANSPARENT_MODE:决定背景是否透明。取值为TRUE或FALSE。当为FALSE时,背景颜色为TRANSPARENT_COLOR所确定的数值。
  
  STRING:连接的字符串
  
  FONT_FACE_NAME:字体。注意,该字段指的不是字体的文件名,而是字体名。也就是说,该字段取值不会是FONT的KEY值。该数值可以通过CCommon::GetFontNameFromFile读取相应的字体文件来获得。
  
  ITALIC:斜体
  
  UNDERLINE:下划线
  
  STRIKEOUT:删除线
  
  
  配置文件的字段,和Text::Property中的变量基本上是对应:
  
  namespace Text
  {
   struct Property
   {
    RECT rcWndPos; 
    TSTRING strText;
    UINT uFormat;
    int iPointSize;
    int iBkMode;
    COLORREF crTextColor;
    COLORREF crBkColor;
    int iWeight;
    BOOL bItalic;
    BOOL bUnderline;
    BOOL bStrikeOut;
    TSTRING strFaceName;
   };
  }
  
  如果CText的使用仅仅是用来显示相应的信息,并不打算在代码中对显示信息进行变更,可以在配置文件中设置STRING字段的指向,不仅能够方便地显示文字,还能在调用SetCurLanguage后自动变更显示的内容。如果需要在代码中进行操控,我们只需要通过GetText函数获取相应的对象,然后再调用SetText函数即可:
  
  CText *pText = GetText(TEXT("TXT_EXIT"));
  pText->SetText(TEXT("退出"));
  
  这是一个随着语言的不同,显示不同语言的“导航”的例子:
  [TXT_TITLE_NAVI]
  TYPE=TEXT
  RECT_POS=11,8,162,39
  POINT_SIZE=26
  WEIGHT=700
  FORMAT=DT_LEFT|DT_SINGLELINE|DT_VCENTER
  COLOR=0,255,0
  TRANSPARENT_MODE=TRUE
  FONT_FACE_NAME=kaiti_GB2312
  STRING=STR_NAVI
  
  [STR_NAVI]
  TYPE=STRING
  EN="NAVI"
  CHS="导航"
  CHT="導航"

posted @ 2011-12-05 10:45  风雨雪夜  阅读(165)  评论(0编辑  收藏  举报