フォーム上にTEdit(Edit1)とTLabel(Label1)を配置します。
TForm1のprivateに次のメソッドを追加します。
Unit1.h
void __fastcall AppMessage(tagMSG &Msg, bool &Handled);
Unit1.cpp
//---------------------------------------------------------------------------
/**
* 確定された文字の読み仮名をLabel1に表示する
*/
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
if (Msg.message == WM_IME_ENDCOMPOSITION)
{
//IMEのコンテキストを格納
HIMC hImc = ImmGetContext(Handle);
//変換結果の「読み」を取得
char buf[1024];
ZeroMemory(buf, sizeof(buf));
ImmGetCompositionString(hImc,
GCS_RESULTREADSTR,
buf, sizeof(buf));
//IMEのコンテキストを開放する
ImmReleaseContext(Handle, hImc);
this->Label1->Caption = buf;
}
}
Edit1のEnterイベントとExitイベントを追加します。
//---------------------------------------------------------------------------
/**
* 入力フォーカスを受け取ったとき読み仮名機能を有効にする
*/
void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
Application->OnMessage = AppMessage;
}
//---------------------------------------------------------------------------
/**
* 入力フォーカスが失われたとき読み仮名機能を無効にする
*/
void __fastcall TForm1::Edit1Exit(TObject *Sender)
{
Application->OnMessage = NULL;
}