Scintilla 在MFC中的简易使用(动态、静态)
Scintilla是一个开源的源代码编辑组件。在这里记录下它在MFC中的简易使用。
Scintilla 3.24在MFC中的使用(动态、静态) 一文使用的别人封装过的类。至于怎样去封装,没有详细说明。
下面将简单介绍在MFCSDI框架下如何封装Scintilla,提供一个玩具级的演示示例。本文使用的是Scintilla 3.2.5
1. 新建一MFC SDI 项目,如:SdiScintillaMFC
2. (动态、静态) 使用 Scintilla 代码初始化。
在 SdiScintillaMFC 中添加如下代码:
HMODULE m_hSciLexerDll;
#ifdef STATIC_BUILD_SCI
#include "Scintilla.h"
#endif
BOOL CSdiScintillaMFCApp::InitInstance()
{
#ifdef STATIC_BUILD_SCI
Scintilla_RegisterClasses(AfxGetInstanceHandle());
#else
m_hSciLexerDll = NULL;
m_hSciLexerDll = LoadLibrary(_T("SciLexer.dll"));
if (NULL == m_hSciLexerDll)
{
AfxMessageBox(_T("LoadLibrary SciLexer.dll failure..."));
return FALSE;
}
#endif
…
}
int CSdiScintillaMFCAp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
#ifdef STATIC_BUILD_SCI
Scintilla_ReleaseResources();
#else
if (m_hSciLexerDll != NULL)
{
::FreeLibrary(m_hSciLexerDll);
}
#endif
return CWinApp::ExitInstance();
}
3. 增加一Scintilla封装类,继承自CWnd,如ScintillaCtrl
增加如下函数
BOOL ScintillaCtrl::Create(DWORD dwExStyle,const RECT& rect, CWnd* pParentWnd)
{
// TODO: 在此添加专用代码和/或调用基类
BOOL bRet;
bRet= CWnd::CreateEx(dwExStyle,_T("Scintilla"), _T(""),
WS_CHILD | WS_VISIBLE, rect,pParentWnd, IDC_STATIC);
return bRet;
}
/
// @mfunc init the view with reasonable defaults
// @rvalue void | not used
//
void ScintillaCtrl::Init()
{
// clear all text styles
SendMessage(SCI_CLEARDOCUMENTSTYLE, 0, 0);
// set the number of styling bits to 7 - the asp/html views need a lot of styling - default is 5
// If you leave the default you will see twiggle lines instead of ASP code
SendMessage(SCI_SETSTYLEBITS, 7, 0);
// set the display for indetation guides to on - this displays virtical dotted lines from the beginning of
// a code block to the end of the block
SendMessage(SCI_SETINDENTATIONGUIDES, TRUE, 0);
// set tabwidth to 4
SendMessage(SCI_SETTABWIDTH,4,0);
// set indention to 4
SendMessage(SCI_SETINDENT,4,0);
// set the caret blinking time to 400 milliseconds
SendMessage(SCI_SETCARETPERIOD,400,0);
// display fold margins
SetFold();
// hide SelectionMargin
SendMessage( SCI_SETMARGINWIDTHN, 1, 0 );
// set markersymbol for marker type 0 - bookmark
SendMessage(SCI_MARKERDEFINE, 0, SC_MARK_CIRCLE);
// set the forground color for some styles
SendMessage(SCI_