Windows における Scintilla エディットコントロールの使い方How to use the Scintilla Edit Control in windows?

ここでは Windows 環境で Scintilla の使い方を一歩ずつ理解していきましょう。

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

Scintilla エディットコントロールの作り方How to create Scintilla Edit Control?

まず、Scintilla の DLL を読み出さなくてはなりません。

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

DLL を無事に読み出すことができた時点で、すでに自動的に新しいウィンドウクラスが登録されています。新しいクラスは "Scintilla" という名前のエディットコントロールです。

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,
		10,10,500,400,hwndParent,(HMENU)GuiID, hInstance,NULL);
		

"Scintilla" が新しいクラス名です。この時点で実際に Scintilla エディットコントロールがご自身の Windows 用プログラムに含まれたことになります。

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

Scintilla エディットコントロールの制御How to control the Scintilla Edit Control?

Scintilla はそのエディットコントロールにコマンドを送ることで制御できます。方法は二通りあります。簡単な方法と速い方法です。

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

Scintilla を制御する簡単な方法The simple way to control Scintilla

簡単な方法というのは他の Windows コントロールに対するものと同様の形です。Scintilla エディットコントロールにメッセージを送り、コントロールからは通知を受け取ります。通知は 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.)

Scintilla エディットコントロールは各コマンドごとの特別なメッセージを知っています(訳注:誤訳かも)。SendMessage 関数で Scintilla エディットコントロールにコマンドを送ることができます。

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

いくつかのコマンドは値を返します。使わない引数は NULL にしておくべきです。

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

Scintilla を速く制御する方法The fast way to control Scintilla

Scintilla エディットコントロールを速く制御する方法は、自分でメッセージ処理関数をよびだすというものです。Scintilla エディットコントロールのメッセージ処理関数へのポインタを取得することができますので、コマンドを実行させるときに直接これを呼び出します。SendMessage() による方法よりうんと高速化できます。

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.

まず、SCI_GETDIRECTFUNCTION を用いてその関数へのポインタを取得し、SCI_GETDIRECTPOINTER を用いて必ずその第一引数となるポインタを取得します。この部分は SendMessage を使わなくてはなりません :)

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

"ptr" が必ず第一引数となるポインタです。以降の引数が Scintilla へのコマンドと(あれば)その二つの引数となります。"fn" のほうが Scintilla コントロールのメッセージ処理関数へのポインタとなります。

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.

Scintilla から通知を受ける方法How will I receive notifications?

Scintilla が何らかの情報を提供しようとすると、必ずイベントが発生します。Scintilla エディットコントロールはその親ウィンドウに通知を行います。通知には WM_NOTIFY メッセージが利用されます。そのメッセージを受け取ったら xxx 構造体の中にある実際のメッセージを見る必要があります。

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.

ですから Scintilla の親ウィンドウのメッセージ処理関数の中に次のようなコードが必要となります。

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:
					/* Scintilla がおのれに新しい文字を追加した言うてまんで。
					  なんぞイカした文字にでもしてやろか(訳注:誤訳かも) */
					/* 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;
			

このページは Holger Schmidt さんより寄贈されました。

Page contributed by Holger Schmidt.