天天看点

使用CRichEditCtrl与正则实现XML高亮编辑器

        最近与XML打交道比较多,为了便于XML格式内容在界面上的编辑,结合正则写了这个简单的XML高亮编辑器,因为是全文解析的,遇到大文件XML时,会慢一些。代码里所使用的正则库是deelx,也可以换成其他的正则库。

派生自CRichEditCtrl的类CKofXMLRichEditCtrl,代码如下:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#pragma once 

#include <tom.h> 

#include <Richole.h> 

#define XML_COLOR_TEXT RGB(0, 0, 0) 

#define XML_COLOR_STRING RGB(163, 21, 21) 

#define XML_COLOR_TAGS RGB(0, 0, 255) 

#define XML_COLOR_COMMENT RGB(0, 128, 0) 

#define XML_COLOR_BACKGROUND RGB(255, 255, 255) 

class CKofXMLRichEditCtrl : public CRichEditCtrl 

    DECLARE_DYNAMIC(CKofXMLRichEditCtrl) 

public: 

    CKofXMLRichEditCtrl(); 

    virtual ~CKofXMLRichEditCtrl(); 

protected: 

    void InitXmlRichEdit(); 

    void ColorXml(); 

    DECLARE_MESSAGE_MAP() 

    virtual void PreSubclassWindow(); 

    afx_msg void OnEnChange(); 

private: 

    IRichEditOle *m_pRichEditOle; 

    ITextDocument *m_pTextDocument;  

}; 

实现文件如下:

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

#include "stdafx.h"  

#include "deelx.h"  

#include "KofXMLRichEditCtrl.h"  

IMPLEMENT_DYNAMIC(CKofXMLRichEditCtrl, CRichEditCtrl)  

CKofXMLRichEditCtrl::CKofXMLRichEditCtrl()  

{  

    m_pRichEditOle = NULL;  

    m_pTextDocument = NULL;  

}  

CKofXMLRichEditCtrl::~CKofXMLRichEditCtrl()  

{  

    if (m_pTextDocument)  

    {  

        m_pTextDocument->Release();  

        m_pTextDocument = NULL;  

    }  

    if (m_pRichEditOle)  

    {  

        m_pRichEditOle->Release();  

        m_pRichEditOle = NULL;  

    }  

}  

BEGIN_MESSAGE_MAP(CKofXMLRichEditCtrl, CRichEditCtrl)  

    ON_CONTROL_REFLECT(EN_CHANGE, &CKofXMLRichEditCtrl::OnEnChange)  

END_MESSAGE_MAP()  

void CKofXMLRichEditCtrl::PreSubclassWindow()  

{  

    m_pRichEditOle = GetIRichEditOle();  

    if (m_pRichEditOle)  

    {  

        if (S_OK != m_pRichEditOle->QueryInterface(__uuidof(ITextDocument), (LPVOID*)&m_pTextDocument))  

        {  

            m_pTextDocument = NULL;  

        }  

    }  

    InitXmlRichEdit();  

    CRichEditCtrl::PreSubclassWindow();  

}  

void CKofXMLRichEditCtrl::InitXmlRichEdit()  

{  

    // 关闭自动字体切换  

    SendMessage(EM_SETLANGOPTIONS, 0, 0);   

    // 设置文本内容变化响应事件掩码  

    SetEventMask(GetEventMask() | ENM_CHANGE);   

    // 设置默认字体  

    CHARFORMAT cf;  

    ZeroMemory(&cf, sizeof(CHARFORMAT));  

    cf.cbSize = sizeof(CHARFORMAT);  

    cf.dwMask = CFM_FACE | CFM_CHARSET | CFM_COLOR;  

    cf.bCharSet = ANSI_CHARSET;  

    cf.dwEffects = 0;  

    cf.crTextColor = XML_COLOR_TEXT;  

    CFileFind fFind;  

    if (fFind.FindFile(_T("C:\\WINDOWS\\Fonts\\CONSOLA.TTF")))  

    {  

        _tcscpy_s(cf.szFaceName, _T("Consolas"));  

    }   

    else  

    {  

        _tcscpy_s(cf.szFaceName, _T("Courier New"));  

    }  

    fFind.Close();  

    SetDefaultCharFormat(cf);  

    // 设置背景色  

    SetBackgroundColor(FALSE, XML_COLOR_BACKGROUND);  

}  

void CKofXMLRichEditCtrl::ColorXml()  

{  

    CString strValue;  

    GetWindowText(strValue);  

    if (strValue.IsEmpty())  

    {  

        return;  

    }  

    //SetSel会把\r\n当成一个字符位  

    strValue.Replace(_T("\r\n"), _T("\n"));  

    //转成Unicode统一当成一个字符  

    CStringW strValueW(strValue);  

    CHARFORMAT cf;  

    ZeroMemory(&cf, sizeof(CHARFORMAT));  

    cf.cbSize = sizeof(CHARFORMAT);  

    cf.dwMask = CFM_COLOR;  

    cf.dwEffects = 0;  

    CStringW strText; 

    long lBegin, lEnd; 

    int iHorzPos, iVertPos; 

    GetSel(lBegin, lEnd); 

    iHorzPos = GetScrollPos(SB_HORZ); 

    iVertPos = GetScrollPos(SB_VERT); 

    SetRedraw(FALSE);  

    m_pTextDocument->Undo(tomSuspend, NULL);  

    SetSel(0, -1);  

    cf.crTextColor = XML_COLOR_TEXT;  

    SetSelectionCharFormat(cf);  

    static CRegexpT<wchar_t> regexpTags(L"<([^>]*)>");            //找寻<>标签  

    static CRegexpT<wchar_t> regexpString(L"\"([^\"]*)\"");        //找寻""字符串  

    static CRegexpT<wchar_t> regexpComment(L"<!--((?s).*?)-->");//找寻<!-- -->注释  

    MatchResult resultTags = regexpTags.Match(strValueW);   

    while (resultTags.IsMatched())  

    {   

        strText = strValueW.Mid(resultTags.GetStart(), resultTags.GetEnd() - resultTags.GetStart());              

        SetSel(resultTags.GetStart(), resultTags.GetEnd());  

        cf.crTextColor = XML_COLOR_TAGS;  

        SetSelectionCharFormat(cf);  

        MatchResult resultString = regexpString.Match(strText);  

        while (resultString.IsMatched())  

        {  

            SetSel(resultTags.GetStart() + resultString.GetStart(),  

                resultTags.GetStart() + resultString.GetEnd());  

            cf.crTextColor = XML_COLOR_STRING;  

            SetSelectionCharFormat(cf);  

            resultString = regexpString.Match(strText,  resultString.GetEnd());   

        }  

        resultTags = regexpTags.Match(strValueW, resultTags.GetEnd());   

    }  

    MatchResult resultComment = regexpComment.Match(strValueW);  

    while (resultComment.IsMatched())  

    {  

        SetSel(resultComment.GetStart(), resultComment.GetEnd());  

        cf.crTextColor = XML_COLOR_COMMENT;  

        SetSelectionCharFormat(cf);  

        resultComment = regexpComment.Match(strValueW, resultComment.GetEnd());  

    }  

    SetSel(lBegin, lEnd);  

    SendMessage(WM_HSCROLL, MAKELONG(SB_THUMBPOSITION, iHorzPos), 0); 

    SendMessage(WM_VSCROLL, MAKELONG(SB_THUMBPOSITION, iVertPos), 0); 

    SetRedraw(TRUE);  

    RedrawWindow();  

    m_pTextDocument->Undo(tomResume, NULL);  

}  

void CKofXMLRichEditCtrl::OnEnChange()  

{  

    if (m_pTextDocument)  

    {  

        ColorXml();  

    }  

}  

步骤如下:

1.打开VC2008,新建基于对话框工程,名称: MyXmlViewer;

2.在对话框上放置一个RichEdit 2.0控件,ID号改为: IDC_RICHEDIT_XML,设置其Multiline属性、Horizontal Scroll属性、Vertical Scroll属性、Want Return属性;

3.在应用程序类实现文件,添加以下代码:

1
    AfxInitRichEdit2(); 

4.包含"kofxmlricheditctrl.h"头文件,然后关联IDC_RICHEDIT_XML的变量即可,如下:

01

02

03

04

05

06

07

08

09

10

11

12

13

14

#include "kofxmlricheditctrl.h" 

class CMyXmlViewerDlg : public CDialog 

    //... 

public: 

    CKofXMLRichEditCtrl m_RichEdit; 

}; 

void CMyXmlViewerDlg::DoDataExchange(CDataExchange* pDX) 

    CDialog::DoDataExchange(pDX); 

    DDX_Control(pDX, IDC_RICHEDIT_XML, m_RichEdit); 

5.效果如下所示:

使用CRichEditCtrl与正则实现XML高亮编辑器