Windows下使用Scintilla

How to use the Scintilla Edit Control in windows?

This should be a little step by step explanation how to use Scintilla in the windows environment.

How to create Scintilla Edit Control?

First of all, load the Scintilla DLL with something like:

	hmod = LoadLibrary("SciLexer.DLL");
		if (hmod==NULL)
		{
			MessageBox(hwndParent,
			"The Scintilla DLL could not be loaded.",
			"Error loading Scintilla",
			MB_OK | MB_ICONERROR);
		}
		

If the DLL was loaded successfully, then the DLL has registered (yes, by itself) a new window class. The new class called "Scintilla" is the new scintilla edit control.

Now you can use this new control just like any other windows control.

	hwndScintilla = CreateWindowEx(0,
		"Scintilla","", WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPCHILDREN,
		10,10,500,400,hwndParent,(HMENU)GuiID, hInstance,NULL);
		

Note the new window class name: "Scintilla". By reaching this point you actually included a Scintilla Edit Control to your windows program.

How to control the Scintilla Edit Control?

You can control Scintilla by sending commands to the Edit Control. There a 2 ways of doing this. A simple and fast way.

The simple way to control Scintilla

The simple way is just like with any other windows control. You can send messages to the Scintilla Edit Control and receive notifications from the control. (Note that the notifications are sent to the parent window of the Scintilla Edit Control.)

The Scintilla Edit Control knows a special message for each command. To send commands to the Scintilla Edit Control you can use the SendMessage function.

	SendMessage(hwndScintilla,sci_command,wparam,lparam);
			

like:

	SendMessage(hwndScintilla,SCI_CREATEDOCUMENT, 0, 0);
			

Some of the commands will return a value and unused parameters should be set to NULL.

The fast way to control Scintilla

The fast way of controlling the Scintilla Edit Control is to call message handling function by yourself. You can retrieve a pointer to the message handling function of the Scintilla Edit Control and call it directly to execute a command. This way is much more faster than the SendMessage() way.

1st you have to use the SCI_GETDIRECTFUNCTION and SCI_GETDIRECTPOINTER commands to retrieve the pointer to the function and a pointer which must be the first parameter when calling the retrieved function pointer. You have to do this with the SendMessage way :)

The whole thing has to look like this:

	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);
			

with "fn" as the function pointer to the message handling function of the Scintilla Control and "ptr" as the pointer that must be used as 1st parameter. The next parameters are the Scintilla Command with its two (optional) parameters.

How will I receive notifications?

Whenever an event occurs where Scintilla wants to inform you about something, the Scintilla Edit Control will send notification to the parent window. This is done by a WM_NOTITY message. When receiving that message, you have to look in the xxx struct for the actual message.

So in Scintillas parent window message handling function you have to include some code like this:

	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;
		
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FastColoredTextBox 是一个用于 WinForm 开发的开源控件,它提供了一个高性能的文本编辑框,适用于需要在应用程序中嵌入文本编辑功能的场景。FastColoredTextBox 支持语法高亮、文本选中、复制、粘贴和撤销操作等常用编辑功能,并提供了丰富的 API 用于控制文本显示和编辑行为。 Scintilla 是一个跨平台的文本编辑组件,它由 C++ 编写,并以静态库的形式发布。Scintilla 提供了丰富的功能和定制选项,包括语法高亮、自动补全、代码折叠、括号匹配等。Scintilla 还支持多个语言的编辑环境,如 C、C++、C#、Python 等,并且可以通过插件进行扩展。 FastColoredTextBox 和 Scintilla 在功能和用途上类似,都是用于文本编辑的控件。它们都支持语法高亮,但 FastColoredTextBox 更适合 WinForm 应用程序的开发,而 Scintilla 则更适合跨平台的开发,因为它的底层代码是用 C++ 编写的。 FastColoredTextBox 和 Scintilla 在用法上也有一些差异。FastColoredTextBox 使用起来相对简单,只需要把控件拖放到窗体上即可使用,并且提供了一些常用的属性和方法用于控制编辑行为。而 Scintilla使用稍微复杂一些,需要编写一些代码来配置和控制编辑器的行为,但它的定制性更高,可以根据需求进行深度定制。 总之,FastColoredTextBox 和 Scintilla 都是用于文本编辑的控件,具有丰富的功能和定制选项,可根据实际需求选择使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值