MFC 显示上标、下标文字

我们在实现应用中可能有时要使用下标操作,比如:CO2

下面就是这样一个类:

View Code
  1 // CharStatic.h : header file
  2 //
  3 
  4 #pragma warning(disable : 4786)
  5 #include <iostream>
  6 using namespace std;
  7 #include <map>
  8 
  9 /////////////////////////////////////////////////////////////////////////////
 10 // CCharStatic window
 11 class CCharStatic : public CStatic
 12 {
 13 // Construction
 14 public:
 15  CCharStatic();
 16  void AddSingleChar(int pos,UINT nFormat);
 17  void SetFontSize(UINT fontSize);
 18 // Attributes
 19 public:
 20 
 21 // Operations
 22 public:
 23 
 24 private:
 25  map<int,UINT> m_singleChar; //a map to store the infomation of char to be uo an down
 26  UINT m_fontSize;   //font size of the text
 27 public:
 28  virtual ~CCharStatic();
 29 
 30  // Generated message map functions
 31 protected:
 32 
 33  afx_msg void OnPaint();
 34 
 35  DECLARE_MESSAGE_MAP()
 36 };
 37 
 38 
 39 
 40 
 41 
 42 // CharStatic.cpp : implementation file
 43 //
 44 
 45 #include "stdafx.h"
 46 #include "CharStatic.h"
 47 
 48 /////////////////////////////////////////////////////////////////////////////
 49 // CCharStatic
 50 
 51 CCharStatic::CCharStatic()
 52 {
 53   m_fontSize = 100;
 54 }
 55 
 56 CCharStatic::~CCharStatic()
 57 {
 58 }
 59 
 60 
 61 BEGIN_MESSAGE_MAP(CCharStatic, CStatic)
 62  ON_WM_PAINT()
 63 END_MESSAGE_MAP()
 64 
 65 /////////////////////////////////////////////////////////////////////////////
 66 // CCharStatic message handlers
 67 
 68 void CCharStatic::AddSingleChar(int pos,UINT nFormat)
 69 {
 70  m_singleChar[pos] = nFormat;
 71  Invalidate(FALSE);
 72 }
 73 
 74 void CCharStatic::SetFontSize(UINT fontSize)
 75 {
 76  m_fontSize = fontSize;
 77  Invalidate(FALSE);
 78 }
 79 
 80 void CCharStatic::OnPaint() 
 81 {
 82  CPaintDC dc(this); // device context for painting
 83  
 84  // TODO: Add your message handler code here
 85  dc.SetBkMode(TRANSPARENT);
 86  CRect rect;
 87  GetWindowRect(&rect); //get the size of the control
 88  int xCharPos = 0;
 89  int yCharPos = 0;
 90  CSize fontSize;
 91  CFont *oldFont = NULL;
 92  CFont newFontNormal;
 93  newFontNormal.CreatePointFont(m_fontSize,_T(""),NULL);
 94  CFont newFontSpecial;
 95  newFontSpecial.CreatePointFont(m_fontSize - 10,_T(""),NULL);
 96  CString textVal;
 97  GetWindowText(textVal); //get the text of the control
 98  CString charText;
 99  int charPosition = 0;
100 
101  for (int i = 0; i < textVal.GetLength(); ++i)
102  {
103   
#ifndef _UINCODE
          //
get the english char
104   if (textVal.GetAt(i) > 33 && textVal.GetAt(i) < 127)
#endif
105   {
106    charText = textVal.GetAt(i);
107    ++charPosition;
108   }
#ifndef _UINCODE
109   else
110   {
111    //chinese char
112    charText.Format(_T("%c%c"),textVal.GetAt(i),textVal.GetAt(i+1));
113    ++i;
114    ++charPosition;
115   }
#endif
116   map<int,UINT>::iterator it = m_singleChar.find(charPosition);
117   if (it != m_singleChar.end())
118   {
119    oldFont = dc.SelectObject(&newFontSpecial);
120    fontSize = dc.GetTextExtent(charText);
121    dc.DrawText(charText,
122     CRect(xCharPos,yCharPos + (it->second - 2),rect.Width(),rect.Height()),
123     it->second);
124    xCharPos += fontSize.cx;
125    if (xCharPos > (rect.Width() - fontSize.cx - 3))
126    {
127     xCharPos = 0;
128     yCharPos += fontSize.cy;
129    }
130    dc.SelectObject(oldFont);
131   }
132   else
133   {
134    oldFont = dc.SelectObject(&newFontNormal);
135    fontSize = dc.GetTextExtent(charText);
136    dc.DrawText(charText,
137     CRect(xCharPos,yCharPos,rect.Width(),rect.Height()),
138     DT_LEFT);
139    xCharPos += fontSize.cx;
140    if (xCharPos > (rect.Width() - fontSize.cx - 3))
141    {
142     xCharPos = 0;
143     yCharPos += (fontSize.cy + 2);
144    }
145    dc.SelectObject(oldFont);
146   }
147  }
148  // Do not call CStatic::OnPaint() for painting messages
149 }

下载地址:/Files/pbreak/CharStatic.rar


 

原文地址:https://www.cnblogs.com/pbreak/p/2015428.html