2020-09-15

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

下面就是这样一个类:

  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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值