Scintilla 3 24在MFC中的使用 动态 静态

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

        Scintilla是一个免费的源代码编辑组件。在这里记录下它在MFC中的使用。

本机环境:Windows XP、Visual Studio 2008 SP1
1.下载Scintilla源代码3.24版本(http://www.scintilla.org/ScintillaDownload.html)。
2.打开VS2008,点击菜单栏"Tools"→"Visual Studio 2008 Command Prompt",cd命令到"..\scintilla\win32"目录,使用以下命令进行编译:

1
nmake -f scintilla.mak
3.编译完成之后,就可以在" ..\scintilla\bin"目录下看到 SciLexer.dllScintilla.dll
4.从naughter网站下载MFC封装类( http://www.naughter.com/scintilla.html)。
5.新建一个MFC多文档应用程序,名称为 TestScintilla
6.将 ScintillaCtrl.h、ScintillaCtrl.cpp、ScintillaDocView.h、ScintillaDocView.cpp拷贝到工程目录下,加入工程。
7.参考ScintillaDemo工程,创建IDD_SCINTILLA_FINDDLGORD和IDD_SCINTILLA_REPLACEDLGORD对话框。
8.在" stdafx.h"文件添加如下代码:
1
2
3
4
#define SCI_NAMESPACE  //use Scintilla via a namespace
#include <platform.h>
#include <scintilla.h>
#include <SciLexer.h>
替换 CTestScintillaView基类为Scintilla::CScintillaView,替换 CTestScintillaDoc基类为Scintilla::CScintillaDoc,在APP应用程序类添加如下变量:
1
HMODULE m_hSciDLL;
InitInstance函数,添加如下代码:
1
2
3
4
5
6
m_hSciDLL = LoadLibrary(_T( "SciLexer.dll"));
if (m_hSciDLL ==  NULL)

    AfxMessageBox(_T( "SciLexer DLL is not installed."));
     return FALSE;
}
重载 ExitInstance函数,添加如下代码:
1
2
3
4
if (m_hSciDLL)
{
    FreeLibrary(m_hSciDLL);
}
9.编译,将 SciLexer.dll放到程序目录下,运行程序,这时可以看到编辑器已经出来了,如下图所示:

10.重载 CTestScintillaViewOnInitialUpdate方法,代码如下:

1
2
3
4
5
6
7
8
9
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
40
41
const TCHAR cppKeyWords[] = 
    _T( "and and_eq asm auto bitand bitor bool break ")
    _T( "case catch char class compl const const_cast continue ")
    _T( "default delete do double dynamic_cast else enum explicit export extern false float for ")
    _T( "friend goto if inline int long mutable namespace new not not_eq ")
    _T( "operator or or_eq private protected public ")
    _T( "register reinterpret_cast return short signed sizeof static static_cast struct switch ")
    _T( "template this throw true try typedef typeid typename union unsigned using ")
    _T( "virtual void volatile wchar_t while xor xor_eq ");

void CTestScintillaView::OnInitialUpdate()
{
    CScintillaView::OnInitialUpdate();

    CScintillaCtrl& rCtrl = GetCtrl();

     //Setup the Lexer
    rCtrl.SetLexer(SCLEX_CPP);
    rCtrl.SetKeyWords( 0, cppKeyWords);

     //Setup styles
    rCtrl.StyleSetFore(STYLE_DEFAULT, RGB( 000));
    rCtrl.StyleSetBack(STYLE_DEFAULT, RGB(0xff, 0xff, 0xff));
    rCtrl.StyleSetSize(STYLE_DEFAULT,  11);
    rCtrl.StyleSetFont(STYLE_DEFAULT,  "Verdana");
    rCtrl.StyleClearAll();
    rCtrl.StyleSetFore(SCE_C_DEFAULT, RGB( 000));
    rCtrl.StyleSetFore(SCE_C_COMMENT, RGB( 0, 0x80,  0));
    rCtrl.StyleSetFore(SCE_C_COMMENTLINE, RGB( 0, 0x80,  0));
    rCtrl.StyleSetFore(SCE_C_COMMENTDOC, RGB( 0, 0x80,  0));
    rCtrl.StyleSetFore(SCE_C_COMMENTLINEDOC, RGB( 0, 0x80,  0));
    rCtrl.StyleSetFore(SCE_C_COMMENTDOCKEYWORD, RGB( 0, 0x80,  0));
    rCtrl.StyleSetFore(SCE_C_COMMENTDOCKEYWORDERROR, RGB( 0, 0x80,  0));
    rCtrl.StyleSetFore(SCE_C_NUMBER, RGB( 0, 0x80, 0x80));
    rCtrl.StyleSetFore(SCE_C_WORD, RGB( 00, 0x80));
    rCtrl.StyleSetBold(SCE_C_WORD,  1);
    rCtrl.StyleSetFore(SCE_C_STRING, RGB(0x80,  0, 0x80));
    rCtrl.StyleSetFore(SCE_C_IDENTIFIER, RGB( 000));
    rCtrl.StyleSetFore(SCE_C_PREPROCESSOR, RGB(0x80,  00));
    rCtrl.StyleSetFore(SCE_C_OPERATOR, RGB(0x80, 0x80,  0));
}

设定C++语法解析器,设定高亮关键字,设定文本风格,再次编译运行,效果如下图所示:

示例代码下载:http://download.csdn.net/detail/akof1314/5069136

静态编译步骤如下:
1.创建一个静态库工程,名称为:ScintillaLib
2.将scintilla源代码文件夹放到工程目录下,将lexers、lexlib、src、win32目录下的文件添加到工程。
3.工程属性,C/C++→General→Additional Include Directories填入.\scintilla\include;.\scintilla\lexlib;.\scintilla\src
C/C++→Preprocessor→Preprocessor Definitions填入STATIC_BUILD;SCI_LEXER
4.注释掉CheckD2D.cxx文件里面的内容。
5.编译完成。
6.使用上面的测试工程,在CTestScintillaAppInitInstance函数,加入以下代码:

1
Scintilla_RegisterClasses(AfxGetInstanceHandle());
ExitInstance函数,加入以下代码:
1
Scintilla_ReleaseResources();
7.工程属性,Linker→Input→Additional Dependencies填入 ScintillaLib.lib IMM32.lib
8.编译运行。
示例代码下载: http://download.csdn.net/detail/akof1314/5069143


排除掉不需要的语言解析器:
1.删除掉"..\scintilla\lexers"目录下不需要的语言解析器对应的Lex*.cxx文件,比如这里只留下LexCPP.cxx文件。
2.打开"..\scintilla\src\Catalogue.cxx"文件,找到如下位置

1
//++Autogenerated -- run src/LexGen.py to regenerate
然后删除 LINK_LEXER(...);使其剩下
1
2
LINK_LEXER(lmCPP);
LINK_LEXER(lmCPPNoCase);
打开" ..\scintilla\win32\scintilla.mak"文件,找到如下位置
1
2
#++Autogenerated -- run src/LexGen.py to regenerate
#**LEXOBJS=\\\n\(\t$(DIR_O)\\\*.obj \\\n\)
删除掉 $(DIR_O)\Lex***.obj \使其剩下
1
2
3
4
5
6
#++Autogenerated -- run src/LexGen.py to regenerate
#**LEXOBJS=\\\n\(\t$(DIR_O)\\\*.obj \\\n\)
LEXOBJS=\
    $(DIR_O)\LexCPP.obj \

#--Autogenerated -- end of automatically generated section
然后再找到如下位置
1
2
#++Autogenerated -- run src/LexGen.py to regenerate
#**\n\($(DIR_O)\\\*.obj: ..\\lexers\\\*.cxx $(LEX_HEADERS)\n\n\)
删除掉 $(DIR_O)\Lex***.obj: ..\lexers\Lex***.cxx $(LEX_HEADERS)使其剩下
1
2
3
4
5
6
7
#++Autogenerated -- run src/LexGen.py to regenerate
#**\n\($(DIR_O)\\\*.obj: ..\\lexers\\\*.cxx $(LEX_HEADERS)\n\n\)

$(DIR_O)\LexCPP.obj: ..\lexers\LexCPP.cxx $(LEX_HEADERS)


#--Autogenerated -- end of automatically generated section
3.保存改动,打开VS2008,点击菜单栏"Tools"→" Visual Studio 2008 Command Prompt",cd命令到" ..\scintilla\win32"目录,使用以下命令进行编译:
1
nmake -f scintilla.mak
4.编译完成之后, SciLexer.dll为348KB,未精简前为649KB大小。


参考资料:
1.Scintilla Documentation  http://www.scintilla.org/ScintillaDoc.html
2.MFC classes to encapsulate the Scintilla edit control  http://www.naughter.com/scintilla.html
3.语法高亮编辑控件Scintilla在MFC中的简单使用  http://www.qingfengju.com/article.asp?id=14
4.用Scintilla让程序支持语法高亮并且编译  http://hi.baidu.com/kxw102/item/b7e701a569d685de5af19138
5.Scintilla开源库使用指南  http://www.cnblogs.com/superanyi/archive/2011/04/07/2008632.html

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jhfyuf/article/details/84058834
今日推荐