本文主要是介绍如何在 Windows 环境下使用 Scintilla 编辑控件?收藏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如何在 Windows 环境下使用 Scintilla 编辑控件?收藏新一篇: scintilla 中的代码折叠功能的使用 | 旧一篇: 插件及可扩展性
<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>请注意:此文翻译自http://scintilla.sourceforge.net/Steps.html。
Scintilla 控件可以在网站http://scintilla.sourceforge.net/index.html上获取。
如何在 Windows 环境下使用 Scintilla 编辑控件?
下面的将逐步介绍如何在 Windows 环境下使用 Scintilla 控件。
如何创建 Scintilla 编辑控件?
首先载入 Scintilla 控件的动态库,如下:
hmod = LoadLibrary("SciLexer.DLL");
if (hmod == NULL)
{
MessageBox(hwndParent,
"The Scintilla DLL could not be loaded.",
"Error loading Scintilla",
MB_OK | MB_ICONERROR);
}
如果动态库成功载入,该动态库就已经为我们注册了一个新的 window class。
这个名称为 "Scintilla" 的新 window class 就是 Scintilla 编辑控件。
好了,我们已经可以像使用其他的windows控件一样来使用这个控件了:
hwndScintilla = CreateWindowEx(0, "Scintilla", "",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPCHILDREN,
10, 10, 500, 400, hwndParent, (HMENU)GuiID, hInstance, NULL);
请注意这个新的 window class 的名称是 "Scintilla"。到这里,我们实际上已经在我们的 windows 程序中引入了这个 Scintilla 控件。
怎样操作 Scintilla 控件呢?
简单地说,我们可以通过直接向 Scintilla 发送 commands 来操作该控件。有两种方式,一种方式使用起来比较简单而另外一种方式在执行速度上有优势。
操作 Scintilla 控件的简单的方法
这 种方法和我们使用其他的 windows 控件没什么两样。我们可以发送消息给 Scintilla 控件,然后接受来自 Scintilla 控件的通知(notifications )消息(请注意, Scintilla 控件是将这些通知消息都发送到其父窗体上);
Scintilla 能够识别每一个 command,我们可以直接用 SendMessage 函数来向 Scintilla 控件发送消息:
SendMessage(hwndScintilla, sci_command, wparam, lparam);
譬如:
SendMessage(hwndScintilla, SCI_CREATEDOCUMENT, 0, 0);
有些 commands 是有返回值的,而另外一些命令的参数没有用到,这些参数需要设置成 NULL。
操作 Scintilla 控件的快速的方法
这里所说的快速调用的方法,其实就是在我们的代码中直接调用 Scintilla 编辑控件的消息处理函数。
Scintilla 为我们提供了一个消息处理函数,我们可以获取到这个消息处理函数,然后直接调用这个函数函数来执行相应的 commands。这种方式要比使用 SendMessage 发送消息的处理速度要快。
首先,我们可以通过 SCI_GETDIRECTFUNCTION 和 SCI_GETDIRECTPOINTER 这两个命令来获取到上面提到的消息处理函数和该函数所需要的第一个参数。获取这两个指针我们必须通过 SendMessage :)(译者注:新版本的Scintilla 已经在动态库中导出了相应的函数)
整个过程如下:
int (*fn)(void*, int, int, int);
void * ptr;
int canundo;
fn = (int (__cdecl *)(void *, int, int, int))SendMessage(
hwndScintilla, SCI_GETDIRECTFUNCTION, 0, 0);
ptr = (void *)SendMessage(hwndScintilla, SCI_GETDIRECTPOINTER, 0, 0);
canundo = fn(ptr, SCI_CANUNDO,0,0);
上面的 "fn" 就是 Scintilla 控件的消息处理函数, "ptr" 就是我们每次调用消息处理函数必须用到的第一个参数。剩下的几个参数分别是 Scintilla 的 command(消息、命令) 和该 Command 的两个可选参数。
如何接受 Scintilla 的通知消息?
一旦某个事件触发了,而且 Scintilla 控件认为需要通知用户,它就会向该控件的父窗体发 WM_NOTITY 消息。 我们收到这个消息后需要根据该消息的实际数据结构进行处理。
我们可以在 Scintilla 的父窗体的消息处理函数中包含类似下面的代码:
NMHDR *lpnmhdr;
[...]
case WM_NOTIFY:
{
lpnmhdr = (LPNMHDR) lParam;
if(lpnmhdr->hwndFrom == hwndScintilla)
{
switch (lpnmhdr->code)
{
case SCN_CHARADDED:
{
/* Hey, Scintilla just told me that a new */
/* character was added to the Edit Control.*/
/* Now i do something cool with that char. */
}break;
}
}
}break;
Page contributed by Holger Schmidt.. libbyliugang translate it to chinese.:)
这篇关于如何在 Windows 环境下使用 Scintilla 编辑控件?收藏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!