Scintilla在WTL中的简易使用
Scintilla是一个开源的源代码编辑组件。在这里记录下它在WTL中的简易使用。
在 WTL 中使用 Scintilla 的一个比较成功的案例是pnotepadProgrammer's Notepad Development
大家可以访问 http://code.google.com/p/pnotepad/ ,不过其现在代码庞大,阅读有一定难度。
现提供一玩具级的演示示例,如下:
下面将简单介绍在WTLSDI框架下如何使用Scintilla。本文使用的是Scintilla 3.2.5。
1. 下载Scintilla源代码(http://www.scintilla.org/ScintillaDownload.html)
编译参照 Scintilla 3.2.5在VC中的编译(动态、静态)
本文使用3.2.5版本
2.新建一WTL SDI项目,如:WtlScintillaSDI
3.(动态、静态) 使用 Scintilla 代码初始化。
在 WtlScintillaSDI.cpp 中添加如下代码:
#ifdef STATIC_BUILD_SCI
#include "Scintilla.h"
#endif
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
#ifdef STATIC_BUILD_SCI
Scintilla_RegisterClasses( hInstance );
#else
HMODULE m_hSciLexerDll = NULL;
m_hSciLexerDll = LoadLibrary(_T("SciLexer.dll"));
if (NULL == m_hSciLexerDll)
{
MessageBox( NULL, _T("LoadLibrary SciLexer.dll failure..."), _T("Error"), MB_ICONSTOP | MB_OK );
return FALSE;
}
#endif
HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to
// make the EXE free threaded. This means that calls come in on a random RPC thread.
// HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
ATLASSERT(SUCCEEDED(hRes));
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
::DefWindowProc(NULL, 0, 0, 0L);
AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES); // add flags to support other controls
hRes = _Module.Init(NULL, hInstance);
ATLASSERT(SUCCEEDED(hRes));
int nRet = Run(lpstrCmdLine, nCmdShow);
_Module.Term();
::CoUninitialize();
#ifdef STATIC_BUILD_SCI
Scintilla_ReleaseResources();
#else
if( m_hSciLexerDll != NULL )
{
::FreeLibrary( m_hSciLexerDll );
}
#endif
return nRet;
}
4. 在 WtlScintillaSDIView.h 增加一 Scintilla 封装类,继承自CWindowImpl,如 CScintillaWnd
#include "Scintilla.h"
#include "SciLexer.h"
class CScintillaWnd : public CWindowImpl<CScintillaWnd>
{
public:
DECLARE_WND_CLASS(NULL)
CScintillaWnd()
{
}
BEGIN_MSG_MAP(CScintillaWnd)
END_MSG_MAP()
HWND Create( HWND hWndParent, const ATL::_U_RECT& rect, DWORD dwExStyle = 0 )
{
// TODO: 在此添加专用代码和/或调用基类
return CWindow::Create( _T("Scintilla"), hWndParent, rect, _T(""), WS_CHILD | WS_VISIBLE, dwExStyle );
}
/
// @mfunc init the view with reasonable defaults
// @rvalue void | not used
//
void Init()
{
// clear all text styles
SendMessage(SCI_CLEARDOCUMENTSTYLE, 0, 0);
// set the numbe