CRichEditCtrl派生类 实现上下标,标红,替换(中英文符号转换)


代码片是.h 在上面  不知道怎么传上来,就变成.cpp在上面了。大家凑活看吧。

代码在vs2010和vs2013,编译运行通过。

/******************************************************************************
*
* Copyright (c) 2014,SaiOlive
* Filename:  RchEx.h
* Author:    SaiOlive
* Version:   1.0
* Editdate:  2014.11.12
* Func List: 1.Set Text Red;
*            2.Get Text Superscript\Subscript;
*              Set Text Superscript\Subscript;
*            3.Convert English Symbols to Chinese;
*              Convert Chinese Symbols to English;
*              Delete Space(Include L" ",L"\r",L"\n");
*              Replace
*
* History:   2014.10.23     SaiOlive            Original
*            2014.11.12     SaiOlive            Add comments
*
* Others:
*
******************************************************************************/


#pragma once


#include 
   
   
    
    
#include 
    
    


class CRchEx : public CRichEditCtrl
{

public:

    CRchEx(void);
    virtual ~CRchEx(void);

    /******************************************************************************
    *
    * Function:       Set Text Red
    * Description:    Set text red which is selected
    * Input:
    * Output:
    * Return:
    * Others:         2014.11.12      SaiOlive            Original
    *
    ******************************************************************************/
    void SetTxtRed(void);
    /******************************************************************************
    *
    * Function:       Set Text Red
    * Description:
    * Input:          nBeg            Red text begin location
    *                 nEnd            Red text end location
    * Output:
    * Return:         
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void SetTxtRed(const int nBeg, const int nEnd);
    /******************************************************************************
    *
    * Function:       Set Text Red
    * Description:
    * Input:          vecWord         The words needed to set red
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void SetTxtRed(const std::vector
     
     
      
       &vecWord);


    /******************************************************************************
    *
    * Function:       Set Text Superscript
    * Description:    Set text superscript which is selected
    * Input:          
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void SetTxtSup(void);
    /******************************************************************************
    *
    * Function:       Set Text Subscript
    * Description:    Set text Subscript which is selected
    * Input:
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void SetTxtSub(void);

    /******************************************************************************
    *
    * Function:       Set Superscript\Subscript
    * Description:    Set with the flag "
      
      ","" or "
      
      ",""
    * Input:
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void SetSupSub(void);
    /******************************************************************************
    *
    * Function:       Get Superscript\Subscript
    * Description:    Get with the flag "
      
      ","" or "
      
      ",""
    * Input:
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    CString GetSupSub(void);


    /******************************************************************************
    *
    * Function:       Convert English Symbols to Chinese
    * Description:
    * Input:          
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void Eg2Ch(void);
    /******************************************************************************
    *
    * Function:       Convert Chinese Symbols to English
    * Description:
    * Input:
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void Ch2Eg(void);
    /******************************************************************************
    *
    * Function:       Delete Space
    * Description:    Include L" ",L"\r",L"\n"
    * Input:          
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void DelSpc(void);

    /******************************************************************************
    *
    * Function:       Replace
    * Description:
    * Input:          m_mapReplace    
    * Output:
    * Return:
    * Others:         2014.10.22      SaiOlive            Original
    *                 2014.11.12      SaiOlive            Add comments
    *
    ******************************************************************************/
    void Replace(const std::map
      
      
       
        &m_mapReplace);

protected:

    DECLARE_MESSAGE_MAP()

    void InitMap(void);

private:

    DECLARE_DYNAMIC(CRchEx)

    CHARRANGE m_crRg;
    CHARFORMAT m_chfRed;
    CString m_csTxt;

    std::map
       
       
         m_mapRedLoc; std::map 
        
          ::const_iterator m_mpiRedLoc; std::map 
         
           m_mapEng; std::map 
          
            m_mapChn; std::map 
           
             m_mapSpc; std::map 
            
              m_mapRep; std::map 
             
               ::const_iterator m_mpiRep; void SetCharFormat(void); }; // RchEdtCtrlEx.cpp : 实现文件 // #include "stdafx.h" #include "RchEx.h" // CRchEdtCtrlEx IMPLEMENT_DYNAMIC(CRchEx, CRichEditCtrl) CRchEx::CRchEx(void) { AfxInitRichEdit2(); //使用RICH文本框必须使用这个函数 SetCharFormat(); InitMap(); m_crRg.cpMax = 0; m_crRg.cpMin = 0; } CRchEx::~CRchEx(void) { } BEGIN_MESSAGE_MAP(CRchEx, CRichEditCtrl) END_MESSAGE_MAP() void CRchEx::SetCharFormat(void) { ZeroMemory(&m_chfRed, sizeof(CHARFORMAT)); m_chfRed.cbSize = sizeof(CHARFORMAT); m_chfRed.dwMask = CFM_COLOR; //CFM_BOLD || CFM_ITALIC | CFM_UNDERLINE; m_chfRed.crTextColor = RGB(255, 0, 0); //Red color } void CRchEx::InitMap(void) { m_mapRedLoc.clear(); m_mapRep.clear(); m_mapSpc.clear(); m_mapSpc[L" "] = L""; m_mapSpc[L"\r"] = L""; m_mapSpc[L"\n"] = L""; m_mapEng.clear(); m_mapEng[L","] = L","; m_mapEng[L"."] = L"。"; m_mapEng[L":"] = L":"; m_mapEng[L"?"] = L"?"; m_mapEng[L"!"] = L"!"; m_mapEng[L"\""] = L"“"; //m_mapEng[L"\""] = L"”"; m_mapEng[L"("] = L"("; m_mapEng[L")"] = L")"; m_mapEng[L"'"] = L"‘"; //m_mapEng[L"'"] = L"’"; m_mapEng[L";"] = L";"; m_mapChn.clear(); m_mapChn[L","] = L","; m_mapChn[L"。"] = L"."; m_mapChn[L":"] = L":"; m_mapChn[L"?"] = L"?"; m_mapChn[L"!"] = L"!"; m_mapChn[L"“"] = L"\""; m_mapChn[L"”"] = L"\""; m_mapChn[L"("] = L"("; m_mapChn[L")"] = L")"; m_mapChn[L"‘"] = L"'"; m_mapChn[L"’"] = L"'"; m_mapChn[L";"] = L";"; } void CRchEx::SetTxtRed(void) { GetSel(m_crRg); if (m_crRg.cpMax == m_crRg.cpMin) return; SetTxtRed(m_crRg.cpMin, m_crRg.cpMax); } void CRchEx::SetTxtRed(const int nBeg, const int nEnd) { SetSel(nBeg, nEnd); SetSelectionCharFormat(m_chfRed); } void CRchEx::SetTxtRed(const std::vector 
              
                &vecWord) { GetWindowText(m_csTxt); CString _csTxt(m_csTxt); //_csTxt.Replace(L"\r",L""); //_csTxt.Replace(L"\n",L""); _csTxt.Replace(L"\r\n", L" ");//为了避免换行引起的位置计算错误 std::vector 
               
                 ::const_iterator _vciMisChar; m_mapRedLoc.clear(); for (_vciMisChar = vecWord.begin(); _vciMisChar != vecWord.end(); _vciMisChar++)//将下面的for循环融合进来 { for (int _nLoc(_csTxt.Find(*_vciMisChar)); -1 != _nLoc; _nLoc = _csTxt.Find(*_vciMisChar, _nLoc + (*_vciMisChar).GetLength())) m_mapRedLoc[_nLoc] = _nLoc + (*_vciMisChar).GetLength(); } //设置标红 for (m_mpiRedLoc = m_mapRedLoc.begin(); m_mpiRedLoc != m_mapRedLoc.end(); m_mpiRedLoc++) SetTxtRed(m_mpiRedLoc->first, m_mpiRedLoc->second); } void CRchEx::SetTxtSup(void) { //CHARRANGE 结构就会被用选择范围的开始和结束位置索引所填充 //如果没有当前选定,cpMin和cpMax的值就会是相等的 //cpMin:包含紧挨在范围中的第一个字符之前的字符的位置索引,cpMax:包含紧跟在范围中的最后一个字符之后的字符的位置索引 CHARRANGE _crStr; GetSel(_crStr);//去掉this指针 if (_crStr.cpMax == _crStr.cpMin) return; //如果所选字符已经是下标则将字符大小和位置恢复正常显示文本,否则设置为上标格式 CHARFORMAT2 _cfStr; GetSelectionCharFormat(_cfStr); if (_cfStr.yOffset == 100) { _cfStr.yOffset = 0; _cfStr.yHeight = 240; } else { _cfStr.yOffset = 100; _cfStr.yHeight = 200; } SetSelectionCharFormat(_cfStr); } void CRchEx::SetTxtSub() { //CHARRANGE 结构就会被用选择范围的开始和结束位置索引所填充 //如果没有当前选定,cpMin和cpMax的值就会是相等的 //cpMin:包含紧挨在范围中的第一个字符之前的字符的位置索引,cpMax:包含紧跟在范围中的最后一个字符之后的字符的位置索引 CHARRANGE _crStr; GetSel(_crStr); if (_crStr.cpMax == _crStr.cpMin) return; //如果所选字符已经是下标则将字符大小和位置恢复正常显示文本,否则设置为下标格式 CHARFORMAT2 _cfStr; GetSelectionCharFormat(_cfStr); if (_cfStr.yOffset == -100) { _cfStr.yOffset = 0; _cfStr.yHeight = 240; } else { _cfStr.yOffset = -100; _cfStr.yHeight = 200; } SetSelectionCharFormat(_cfStr); } void CRchEx::SetSupSub() { GetWindowText(m_csTxt); CString _csTxt(m_csTxt); _csTxt.Replace(L"\r\n", L"♊");//为了避免换行引起的位置计算错误 std::vector 
                
                  _vecSubRange, _vecSupRange;//henry//应使用map,并使用迭代器 bool _bSup(false), _bSub(false); for (int i(0); i < _csTxt.GetLength();) /*在字符串中找出所有的上标和下标标注,将开始、结束位置分别存入subRange,supRange中*/ { int _nSupBeg = _csTxt.Find(_T(" 
                 "), i); int _nSupEnd = _csTxt.Find(_T(""), i + 5); int _nSubBeg = _csTxt.Find(_T(" 
                 "), i); int _nSubEnd = _csTxt.Find(_T(""), i + 5); if (_nSupBeg != -1 && _nSupEnd != -1) _bSup = true; else _bSup = false; if (_nSubBeg != -1 && _nSubEnd != -1) _bSub = true; else _bSub = false; if (!_bSup && !_bSub) break; //从前向后处理,避免上下标开始与结束位置配对出错 if ((_bSup && _bSub && _nSupBeg < _nSubBeg) || (_bSup && !_bSub))//处理上标 { _vecSupRange.push_back(_nSupBeg); _vecSupRange.push_back(_nSupEnd - 5); _csTxt = _csTxt.Left(_nSupBeg) + _csTxt.Mid(_nSupBeg + 5, _nSupEnd - _nSupBeg - 5) + _csTxt.Right(_csTxt.GetLength() - _nSupEnd - 6);//应该使用用Mid,减少计算次数 i = _nSupEnd - 5; } if ((_bSup && _bSub && _nSubBeg < _nSupBeg) || (_bSub && !_bSup))//处理下标 { _vecSubRange.push_back(_nSubBeg); _vecSubRange.push_back(_nSubEnd - 5); _csTxt = _csTxt.Left(_nSubBeg) + _csTxt.Mid(_nSubBeg + 5, _nSubEnd - _nSubBeg - 5) + _csTxt.Right(_csTxt.GetLength() - _nSubEnd - 6);//应该使用用Mid,减少计算次数 i = _nSubEnd - 5; } } _csTxt.Replace(L"♊", L"\r\n");//为了避免换行引起的位置计算错误 SetWindowText(_csTxt); /*按supRange中记录的上标范围逐个设置上标格式*/ CHARFORMAT2 _cfStr; for (UINT i = 0; i < _vecSupRange.size(); i += 2) { SetSel(_vecSupRange[i], _vecSupRange[i + 1]); GetSelectionCharFormat(_cfStr); _cfStr.yOffset = 100; _cfStr.yHeight = 200; SetSelectionCharFormat(_cfStr); } /*按subRange中记录的下标范围逐个设置下标格式*/ for (UINT i = 0; i < _vecSubRange.size(); i += 2) { SetSel(_vecSubRange[i], _vecSubRange[i + 1]); GetSelectionCharFormat(_cfStr); _cfStr.yOffset = -100; _cfStr.yHeight = 200; SetSelectionCharFormat(_cfStr); } } //按照编辑框中的文本的显示格式得到数据库中应存储的字符串 CString CRchEx::GetSupSub() { CHARFORMAT _cfStr; m_csTxt = L""; //GetWindowText(m_csTxt); //int _nLen(m_csTxt.GetLength()); int _nLen(GetWindowTextLength()); for (int i(0); i < _nLen;) { SetSel(i, i + 1); GetSelectionCharFormat(_cfStr); if (0 == _cfStr.yOffset)//switch { m_csTxt += GetSelText(); i++; } else if (100 == _cfStr.yOffset) { m_csTxt += _T(" 
                 "); while (100 == _cfStr.yOffset && i < _nLen) { m_csTxt += GetSelText(); i++; SetSel(i, i + 1); GetSelectionCharFormat(_cfStr); } m_csTxt += _T(""); } else if (-100 == _cfStr.yOffset) { m_csTxt += _T(" 
                 "); while (-100 == _cfStr.yOffset && i < _nLen) { m_csTxt = m_csTxt + GetSelText(); i++; SetSel(i, i + 1); GetSelectionCharFormat(_cfStr); } m_csTxt += _T(""); } } return m_csTxt; } void CRchEx::Eg2Ch(void) { Replace(m_mapEng); } void CRchEx::Ch2Eg(void) { Replace(m_mapChn); } void CRchEx::DelSpc(void) { Replace(m_mapSpc); } void CRchEx::Replace(const std::map 
                 
                   & m_mapReplace) { if (m_mapReplace.empty() || !GetWindowTextLength()) return; GetSel(m_crRg); //获得选择区域范围 if (m_crRg.cpMax == m_crRg.cpMin) //Set all selected { m_crRg.cpMin = 0; m_crRg.cpMax = GetWindowTextLength(); } int _nLen = GetWindowTextLength() - m_crRg.cpMax - 2; for (m_mpiRep = m_mapReplace.begin(); m_mpiRep != m_mapReplace.end(); m_mpiRep++) { for (int l(m_crRg.cpMin); l < m_crRg.cpMax; m_crRg.cpMax = GetWindowTextLength() - _nLen) { SetSel(l, l + m_mpiRep->first.GetLength()); if (!GetSelText().Compare(m_mpiRep->first)) { ReplaceSel(m_mpiRep->second); l += m_mpiRep->second.GetLength(); } else l++; } } //SetFocus(); //SetSel(m_crRg); } 
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值